From: Mateo Gjika <104777599+mateoxh@users.noreply.github.com> Date: Sun, 2 Aug 2026 17:49:36 +0000 (+0000) Subject: runtime(vimgoto): make 'gf' find imported python files X-Git-Tag: v9.2.0897~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=cdeee18f31fd78d36e9bbe65266805d2e9053d07;p=thirdparty%2Fvim.git runtime(vimgoto): make 'gf' find imported python files In Vim script with this line: py3 import file make gf work using vimgoto plugin. closes: #20908 Signed-off-by: Mateo Gjika <104777599+mateoxh@users.noreply.github.com> Signed-off-by: Christian Brabandt --- diff --git a/runtime/autoload/vimgoto.vim b/runtime/autoload/vimgoto.vim index 634457dff5..351714f02d 100644 --- a/runtime/autoload/vimgoto.vim +++ b/runtime/autoload/vimgoto.vim @@ -5,7 +5,7 @@ vim9script # Shane-XB-Qian # Andrew Radev # thinca -# Last Change: 2026 Mar 30 +# Last Change: 2026 Aug 02 # # Vim script to handle jumping to the targets of several types of Vim commands # (:import, :packadd, :runtime, :colorscheme), and to autoloaded functions of @@ -37,7 +37,12 @@ export def Find(editcmd: string) #{{{2 return endif - var curfunc = FindCurfunc() + if curline =~ '^\s*py\%(thon\)\=3\s\+\%(from\|import\)\s' + HandlePythonImportLine(editcmd, curline) + return + endif + + var curfunc = ExpandCurWord('#') if stridx(curfunc, '#') >= 0 var parts = split(curfunc, '#') @@ -171,6 +176,48 @@ def HandleImportLine(editcmd: string, curline: string) #{{{2 execute how_to_split .. ' ' .. fnameescape(filepath) enddef +def HandlePythonImportLine(editcmd: string, curline: string) #{{{2 + # from ftplugin/python.vim + var grandparent_match: string = '^\(.\.\)\(\.*\)' + var grandparent_sub: string = '\=submatch(1)."/".repeat("../",strlen(submatch(2)))' + + var parent_match: string = '^\.\(\.\)\@!' + var parent_sub: string = './' + + var child_match: string = '\(\w\)\.\(\w\)' + var child_sub: string = '\1/\2' + + var fname: string + + if curline =~ '\' || curline =~ '\' + fname = curline->matchstr('^\s*py\%(thon\)\=3\s\+\%(from\|import\)\s\+\zs\S\+') + else + fname = ExpandCurWord('.') + endif + + fname = fname + ->substitute(grandparent_match, grandparent_sub, '') + ->substitute(parent_match, parent_sub, '') + ->substitute(child_match, child_sub, 'g') + .. '.py' + + var filepath: string = globpath(&runtimepath, 'python/' .. fname, true, true) + ->get(0, '') + + if !filepath->filereadable() + printf('E447: Can''t find file "%s" in path', fname) + ->Error() + return + endif + + var how_to_split: string = { + gF: 'edit', + "\F": 'split', + "\gF": 'tab split', + }[editcmd] + execute how_to_split .. ' ' .. fnameescape(filepath) +enddef + def Open(target: any, editcmd: string, search_pattern: string = '') #{{{2 var split: string = editcmd[0] == 'g' ? 'edit' : editcmd[1] == 'g' ? 'tabedit' : 'split' var fname: string @@ -220,12 +267,12 @@ def Fallback(editcmd: string) #{{{2 endtry enddef -def FindCurfunc(): string #{{{2 +def ExpandCurWord(char: string): string #{{{2 var curfunc = '' var saved_iskeyword = &iskeyword try - set iskeyword+=# + execute 'set iskeyword+=' .. char curfunc = expand('') finally &iskeyword = saved_iskeyword diff --git a/runtime/ftplugin/vim.vim b/runtime/ftplugin/vim.vim index b54cb48f02..9c57399264 100644 --- a/runtime/ftplugin/vim.vim +++ b/runtime/ftplugin/vim.vim @@ -7,6 +7,7 @@ " @tpope (s:Help()) " @lacygoill " Last Change: 2026 Jun 27 +" 2026 Aug 02 by Vim project: Update 'path' setting #20908 " Only do this when not done yet for this buffer if exists("b:did_ftplugin") @@ -135,7 +136,7 @@ endif " set 'path' to common Vim directories setlocal path-=/usr/include -setlocal path+=pack/**,runtime/**,autoload/**,colors/**,compiler/**,ftplugin/**,indent/**,keymap/**,macros/**,plugin/**,syntax/**,after/** +setlocal path+=pack/**,runtime/**,autoload/**,colors/**,compiler/**,ftplugin/**,indent/**,keymap/**,macros/**,plugin/**,syntax/**,after/**,python/** if !exists("no_plugin_maps") && !exists("no_vim_maps") let b:did_add_maps = 1