diff --git a/autoload/ollama/edit.vim b/autoload/ollama/edit.vim index e1efd7a..7bb0244 100644 --- a/autoload/ollama/edit.vim +++ b/autoload/ollama/edit.vim @@ -5,10 +5,19 @@ let s:buf = -1 " avoid starting a 2nd edit job while one is in progress let g:edit_in_progress = 0 +" 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" @@ -27,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: @@ -47,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 = ['|', '/', '-', '\'] @@ -91,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 @@ -142,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 @@ -149,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 @@ -181,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: @@ -199,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: @@ -212,4 +249,3 @@ finally: pass EOF endfunction - diff --git a/autoload/ollama/setup.vim b/autoload/ollama/setup.vim index 3771ca2..404f099 100644 --- a/autoload/ollama/setup.vim +++ b/autoload/ollama/setup.vim @@ -379,7 +379,12 @@ function! ollama#setup#EnsureVenv() abort endfunction " Loads the plugin's python modules + function! s:LoadPluginPyModules() abort + if !ollama#edit#HasEmbeddedPython() + echoerr "LoadPluginPyModules requires Vim compiled with +python3 support." + return + endif python3 << EOF import os import sys @@ -391,7 +396,7 @@ 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: @@ -403,6 +408,10 @@ endfunction " This must be done before loading the plugin's py modules, " to ensure the plugin's python requirements are available. function! s:SetupPyVEnv() abort + if !ollama#edit#HasEmbeddedPython() + echoerr "SetupPyVenv requires Vim compiled with +python3 support." + return + endif python3 << EOF import os import sys @@ -412,21 +421,18 @@ 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 @@ -456,18 +462,26 @@ function! ollama#setup#Init() abort 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() - 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 " 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() endif - call s:LoadPluginPyModules() 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')