-
Notifications
You must be signed in to change notification settings - Fork 34
Switching to installed python when vim is not compiled with +python3 #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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." | ||
|
Comment on lines
+17
to
+18
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Considering the same error message is repeated after every invocation to function! ollama#edit#EnsureEmbeddedPythonIsAvailable() abort
if exists('g:ollama_embedded_python') && g:ollama_embedded_python != 0
echoerr "OllamaEdit features require Vim compiled with +python3 support."
return v:true
else
return v:false
endif
endfunctionAnd then the usage looks like this: if !ollama#edit#EnsureEmbeddedPythonIsAvailable()
return
endif |
||
| 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,13 +163,21 @@ 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 | ||
|
|
||
| """"""""""""""""""""""""""""""""""""""""""""""""""""""""""" | ||
| " 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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' | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
|
Comment on lines
+427
to
+431
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor If you are switching to vertical formatting, maybe format all the argument vertically:
Suggested change
|
||||||||||||||||||||||||
| 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) | ||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor This has been fixed already. |
||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor
In
autoload/ollama/setup.vimonly the value ofg:ollama_embedded_pythonis checked:Here both existence and value are checked. While checking existence is more robust, if the assumption is that this value will always be set by the plugin setup code, the existence check becomes redundant.
In any case, it seems inconsistent to treat this variable one way in one location and another way in another.
I think either both locations should check existence, or neither should.
I would probably recommend the latter.
It would make the check simpler.