]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
runtime(vimgoto): make 'gf' find imported python files
authorMateo Gjika <104777599+mateoxh@users.noreply.github.com>
Sun, 2 Aug 2026 17:49:36 +0000 (17:49 +0000)
committerChristian Brabandt <cb@256bit.org>
Sun, 2 Aug 2026 17:53:57 +0000 (17:53 +0000)
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 <cb@256bit.org>
runtime/autoload/vimgoto.vim
runtime/ftplugin/vim.vim

index 634457dff5f7efdf7674102e173ef1654c3fb957..351714f02d15d338c0e936c58ad51c2e8dabb27e 100644 (file)
@@ -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 =~ '\<from\>' || curline =~ '\<as\>'
+    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',
+    "\<C-W>F": 'split',
+    "\<C-W>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('<cword>')
     finally
         &iskeyword = saved_iskeyword
index b54cb48f02ae6a296c3aee288648247d8f3e3e47..9c57399264e295ecc1a0fd2fefcc925d0e9f3e97 100644 (file)
@@ -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