Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion autoload/ollama/edit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Contributor

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.vim only the value of g:ollama_embedded_python is checked:

            if g:ollama_embedded_python
              call s:SetupPyVEnv()
            endif

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.

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering the same error message is repeated after every invocation to ollama#edit#HasEmbeddedPython() maybe it would make sense to move the error message into the same function?

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
endfunction

And 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"
Expand All @@ -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:
Expand All @@ -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 = ['|', '/', '-', '\']
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -212,4 +249,3 @@ finally:
pass
EOF
endfunction

42 changes: 28 additions & 14 deletions autoload/ollama/setup.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
venv_site_packages = os.path.join(
venv_path,
'lib', f'python{sys.version_info.major}.{sys.version_info.minor}',
'site-packages'
)
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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor

This has been fixed already.
It seems that this project uses merges for PRs, so maybe it does not matter much.
But if you rebase this minor fix should disappear.

else:
print('Venv disabled')
EOF
Expand Down Expand Up @@ -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
9 changes: 4 additions & 5 deletions plugin/ollama.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down