From 3920eeae4583fb259b8bc3734df98bd85f1bb0d7 Mon Sep 17 00:00:00 2001 From: scarbajali Date: Wed, 23 Jul 2025 00:29:44 +0200 Subject: [PATCH 1/4] Adding embedded_python variable. --- autoload/ollama/edit.vim | 26 +++++++++++++++++++++++++- autoload/ollama/setup.vim | 34 ++++++++++++++++++++-------------- plugin/ollama.vim | 9 ++++----- 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/autoload/ollama/edit.vim b/autoload/ollama/edit.vim index e1efd7a..135d6c6 100644 --- a/autoload/ollama/edit.vim +++ b/autoload/ollama/edit.vim @@ -5,6 +5,31 @@ let s:buf = -1 " avoid starting a 2nd edit job while one is in progress let g:edit_in_progress = 0 +if !exists('g:ollama_embedded_python') || g:ollama_embedded_python == 0 + function! ollama#edit#EditCodeDone(status) abort + echoerr "OllamaEdit features require Vim compiled with +python3 support." + endfunction + function! ollama#edit#DialogCallback(id, result) abort + echoerr "OllamaEdit features require Vim compiled with +python3 support." + endfunction + function! ollama#edit#UpdateProgress(popup) abort + echoerr "OllamaEdit features require Vim compiled with +python3 support." + endfunction + function! ollama#edit#EditCode(request) abort + echoerr "OllamaEdit features require Vim compiled with +python3 support." + endfunction + function! ollama#edit#EditPrompt() abort + echoerr "OllamaEdit features require Vim compiled with +python3 support." + endfunction + function! ollama#edit#AcceptAll() abort + echoerr "OllamaEdit features require Vim compiled with +python3 support." + endfunction + function! ollama#edit#RejectAll() abort + echoerr "OllamaEdit features require Vim compiled with +python3 support." + endfunction + finish +endif + " Define the VimScript callback function " This will be called from python when to operations is 'done' " or aborted with 'error' @@ -212,4 +237,3 @@ finally: pass EOF endfunction - diff --git a/autoload/ollama/setup.vim b/autoload/ollama/setup.vim index 3771ca2..ef0fb7f 100644 --- a/autoload/ollama/setup.vim +++ b/autoload/ollama/setup.vim @@ -379,6 +379,8 @@ function! ollama#setup#EnsureVenv() abort endfunction " Loads the plugin's python modules + +if g:ollama_embedded_python function! s:LoadPluginPyModules() abort python3 << EOF import os @@ -391,17 +393,19 @@ if plugin_python_path not in sys.path: sys.path.append(plugin_python_path) try: - # Import your CodeEditor module + # Import plugin modules import CodeEditor import VimHelper except ImportError as e: print(f'Error importing CodeEditor module:\n{e}') EOF endfunction +endif " Initializes venv for python. " This must be done before loading the plugin's py modules, " to ensure the plugin's python requirements are available. +if g:ollama_embedded_python function! s:SetupPyVEnv() abort python3 << EOF import os @@ -412,25 +416,23 @@ use_venv = vim.eval('g:ollama_use_venv') or 0 # Should we use a venv? if use_venv: - # Create default venv path venv_path = os.path.join(os.environ['HOME'], '.vim', 'venv', 'ollama') - # Check if the venv path exists if os.path.exists(venv_path): - #print('Found venv:', venv_path) - venv_bin = os.path.join(venv_path, 'bin', 'python3') - venv_site_packages = os.path.join(venv_path, 'lib', f'python{sys.version_info.major}.{sys.version_info.minor}', 'site-packages') - - # Ensure the virtual environment's site-packages is in sys.path + venv_site_packages = os.path.join( + venv_path, + 'lib', f'python{sys.version_info.major}.{sys.version_info.minor}', + 'site-packages' + ) if venv_site_packages not in sys.path: - #print(f'Adding venv site-packages to path: {venv_site_packages}') sys.path.insert(0, venv_site_packages) else: - print('Venv not found: '. venv_path) + print('Venv not found: ' + venv_path) else: print('Venv disabled') EOF endfunction +endif function! ollama#setup#Init() abort let l:ollama_config = expand('$HOME/.vim/config/ollama.vim') @@ -453,21 +455,25 @@ function! ollama#setup#Init() abort endif echon "\n" - if g:ollama_use_venv + if g:ollama_use_venv && g:ollama_embedded_python " Ensure venv and dependencies are set up call ollama#setup#EnsureVenv() call s:SetupPyVEnv() endif call ollama#setup#Setup() - call s:LoadPluginPyModules() + if g:ollama_embedded_python + call s:LoadPluginPyModules() + endif else " load the config file execute 'source' l:ollama_config - if g:ollama_use_venv + if g:ollama_use_venv && g:ollama_embedded_python " Ensure venv and dependencies are set up call ollama#setup#EnsureVenv() call s:SetupPyVEnv() endif - call s:LoadPluginPyModules() + if g:ollama_embedded_python + call s:LoadPluginPyModules() + endif endif endfunction diff --git a/plugin/ollama.vim b/plugin/ollama.vim index 7fbd16a..2e2e59a 100644 --- a/plugin/ollama.vim +++ b/plugin/ollama.vim @@ -19,13 +19,12 @@ if has('nvim') endif if has('python3') || has('python3_dynamic') - " Use system's python3 by default (can be changed by venv) - let g:ollama_python_interpreter = 'python3' + let g:ollama_embedded_python = 1 else - let g:ollama_enabled = 0 - echom "warning: your Vim version does not support python3. Vim-ollama is disabled." - finish + let g:ollama_embedded_python = 0 endif +" Use system's python3 by default (can be changed by venv) +let g:ollama_python_interpreter = 'python3' " Default settings if !exists('g:ollama_enabled') From 8c4c9d4504779a6a152cc0e2df0680a805c5c8a8 Mon Sep 17 00:00:00 2001 From: scarbajali Date: Tue, 12 Aug 2025 22:22:51 +0200 Subject: [PATCH 2/4] venv initialization fix. --- autoload/ollama/setup.vim | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/autoload/ollama/setup.vim b/autoload/ollama/setup.vim index ef0fb7f..3dde896 100644 --- a/autoload/ollama/setup.vim +++ b/autoload/ollama/setup.vim @@ -455,10 +455,12 @@ function! ollama#setup#Init() abort endif echon "\n" - if g:ollama_use_venv && g:ollama_embedded_python + if g:ollama_use_venv " Ensure venv and dependencies are set up call ollama#setup#EnsureVenv() - call s:SetupPyVEnv() + if g:ollama_embedded_python + call s:SetupPyVEnv() + endif endif call ollama#setup#Setup() if g:ollama_embedded_python @@ -470,7 +472,9 @@ function! ollama#setup#Init() abort if g:ollama_use_venv && g:ollama_embedded_python " Ensure venv and dependencies are set up call ollama#setup#EnsureVenv() - call s:SetupPyVEnv() + if g:ollama_embedded_python + call s:SetupPyVEnv() + endif endif if g:ollama_embedded_python call s:LoadPluginPyModules() From 011eb9d21ef1b49dca9962e7e07b79f5336d0030 Mon Sep 17 00:00:00 2001 From: scarbajali Date: Tue, 12 Aug 2025 22:25:15 +0200 Subject: [PATCH 3/4] Fix venv in both sides. --- autoload/ollama/setup.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/ollama/setup.vim b/autoload/ollama/setup.vim index 3dde896..5bb03e7 100644 --- a/autoload/ollama/setup.vim +++ b/autoload/ollama/setup.vim @@ -469,7 +469,7 @@ function! ollama#setup#Init() abort else " load the config file execute 'source' l:ollama_config - if g:ollama_use_venv && g:ollama_embedded_python + if g:ollama_use_venv " Ensure venv and dependencies are set up call ollama#setup#EnsureVenv() if g:ollama_embedded_python From 661189e14620c11c978bfc38dee43a6c54bdda3e Mon Sep 17 00:00:00 2001 From: scarbajali Date: Tue, 12 Aug 2025 22:58:12 +0200 Subject: [PATCH 4/4] Checking embedded python inside the functions. --- autoload/ollama/edit.vim | 60 +++++++++++++++++++++++---------------- autoload/ollama/setup.vim | 12 +++++--- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/autoload/ollama/edit.vim b/autoload/ollama/edit.vim index 135d6c6..7bb0244 100644 --- a/autoload/ollama/edit.vim +++ b/autoload/ollama/edit.vim @@ -5,35 +5,19 @@ let s:buf = -1 " avoid starting a 2nd edit job while one is in progress let g:edit_in_progress = 0 -if !exists('g:ollama_embedded_python') || g:ollama_embedded_python == 0 - function! ollama#edit#EditCodeDone(status) abort - echoerr "OllamaEdit features require Vim compiled with +python3 support." - endfunction - function! ollama#edit#DialogCallback(id, result) abort - echoerr "OllamaEdit features require Vim compiled with +python3 support." - endfunction - function! ollama#edit#UpdateProgress(popup) abort - echoerr "OllamaEdit features require Vim compiled with +python3 support." - endfunction - function! ollama#edit#EditCode(request) abort - echoerr "OllamaEdit features require Vim compiled with +python3 support." - endfunction - function! ollama#edit#EditPrompt() abort - echoerr "OllamaEdit features require Vim compiled with +python3 support." - endfunction - function! ollama#edit#AcceptAll() abort - echoerr "OllamaEdit features require Vim compiled with +python3 support." - endfunction - function! ollama#edit#RejectAll() abort - echoerr "OllamaEdit features require Vim compiled with +python3 support." - endfunction - finish -endif +" Function to check if embedded Python is available +function! ollama#edit#HasEmbeddedPython() abort + return exists('g:ollama_embedded_python') && g:ollama_embedded_python != 0 +endfunction " Define the VimScript callback function " This will be called from python when to operations is 'done' " or aborted with 'error' function! ollama#edit#EditCodeDone(status) + if !ollama#edit#HasEmbeddedPython() + echoerr "OllamaEdit features require Vim compiled with +python3 support." + return + endif if a:status == "done" echom "Code editing completed!" elseif a:status == "error" @@ -52,6 +36,10 @@ endfunction " Callback wrapper which delegates the call to Python function! ollama#edit#DialogCallback(id, result) + if !ollama#edit#HasEmbeddedPython() + echoerr "OllamaEdit features require Vim compiled with +python3 support." + return + endif python3 << EOF import vim try: @@ -72,6 +60,10 @@ endfunction " Give user visual feedback about job that is in progress function! ollama#edit#UpdateProgress(popup) + if !ollama#edit#HasEmbeddedPython() + echoerr "OllamaEdit features require Vim compiled with +python3 support." + return + endif " Cycle through progress states let g:progress_indicator = (g:progress_indicator + 1) % 4 let l:states = ['|', '/', '-', '\'] @@ -116,6 +108,10 @@ endfunction " Internal Helper function for offloading logic to Python """"""""""""""""""""""""""""""""""""""""""""""""""""""""""" function! s:EditCodeInternal(request, first_line, last_line) abort + if !ollama#edit#HasEmbeddedPython() + echoerr "OllamaEdit features require Vim compiled with +python3 support." + return + endif if exists('g:edit_in_progress') && g:edit_in_progress return endif @@ -167,6 +163,10 @@ endfunction " Start the Python function and return immediately (Range command) """"""""""""""""""""""""""""""""""""""""""""""""""""""""""" function! ollama#edit#EditCode(request) + if !ollama#edit#HasEmbeddedPython() + echoerr "OllamaEdit features require Vim compiled with +python3 support." + return + endif call s:EditCodeInternal(a:request, a:firstline, a:lastline) endfunction @@ -174,6 +174,10 @@ endfunction " Popup edit prompt, instead of Edit command """"""""""""""""""""""""""""""""""""""""""""""""""""""""""" function! ollama#edit#EditPrompt() + if !ollama#edit#HasEmbeddedPython() + echoerr "OllamaEdit features require Vim compiled with +python3 support." + return + endif " Initialize line numbers let l:firstline = 0 let l:lastline = 0 @@ -206,6 +210,10 @@ endfunction " Accept All Changes """"""""""""""""""""""""""""""""""""""""""""""""""""""""""" function! ollama#edit#AcceptAll() + if !ollama#edit#HasEmbeddedPython() + echoerr "OllamaEdit features require Vim compiled with +python3 support." + return + endif python3 << EOF import vim try: @@ -224,6 +232,10 @@ endfunction " Reject All Changes """"""""""""""""""""""""""""""""""""""""""""""""""""""""""" function! ollama#edit#RejectAll() + if !ollama#edit#HasEmbeddedPython() + echoerr "OllamaEdit features require Vim compiled with +python3 support." + return + endif python3 << EOF import vim try: diff --git a/autoload/ollama/setup.vim b/autoload/ollama/setup.vim index 5bb03e7..404f099 100644 --- a/autoload/ollama/setup.vim +++ b/autoload/ollama/setup.vim @@ -380,8 +380,11 @@ endfunction " Loads the plugin's python modules -if g:ollama_embedded_python function! s:LoadPluginPyModules() abort + if !ollama#edit#HasEmbeddedPython() + echoerr "LoadPluginPyModules requires Vim compiled with +python3 support." + return + endif python3 << EOF import os import sys @@ -400,13 +403,15 @@ except ImportError as e: print(f'Error importing CodeEditor module:\n{e}') EOF endfunction -endif " Initializes venv for python. " This must be done before loading the plugin's py modules, " to ensure the plugin's python requirements are available. -if g:ollama_embedded_python function! s:SetupPyVEnv() abort + if !ollama#edit#HasEmbeddedPython() + echoerr "SetupPyVenv requires Vim compiled with +python3 support." + return + endif python3 << EOF import os import sys @@ -432,7 +437,6 @@ else: print('Venv disabled') EOF endfunction -endif function! ollama#setup#Init() abort let l:ollama_config = expand('$HOME/.vim/config/ollama.vim')