vimgoto.vim (8322B)
1 vim9script 2 3 # Language: Vim9 script 4 # Contributers: @lacygoill 5 # Shane-XB-Qian 6 # Andrew Radev 7 # thinca 8 # Last Change: 2026 Aug 02 9 # 10 # Vim script to handle jumping to the targets of several types of Vim commands 11 # (:import, :packadd, :runtime, :colorscheme), and to autoloaded functions of 12 # the style <path>#<function_name>. 13 # 14 # see runtime/ftplugin/vim.vim 15 16 # Interface {{{1 17 export def Find(editcmd: string) #{{{2 18 var curline: string = getline('.') 19 20 if curline =~ '^\s*\%(:\s*\)\=\%(sil\%[ent]!\=\s\+\)\=packadd!\=\s' 21 HandlePackaddLine(editcmd, curline) 22 return 23 endif 24 25 if curline =~ '^\s*\%(:\s*\)\=\%(sil\%[ent]!\=\s\+\)\=ru\%[ntime]!\=' 26 HandleRuntimeLine(editcmd, curline, expand('<cfile>')) 27 return 28 endif 29 30 if curline =~ '^\s*\%(:\s*\)\=\%(sil\%[ent]!\=\s\+\)\=colo\%[rscheme]\s' 31 HandleColoLine(editcmd, curline) 32 return 33 endif 34 35 if curline =~ '^\s*\%(:\s*\)\=import\s' 36 HandleImportLine(editcmd, curline) 37 return 38 endif 39 40 if curline =~ '^\s*py\%(thon\)\=3\s\+\%(from\|import\)\s' 41 HandlePythonImportLine(editcmd, curline) 42 return 43 endif 44 45 var curfunc = ExpandCurWord('#') 46 47 if stridx(curfunc, '#') >= 0 48 var parts = split(curfunc, '#') 49 var path = $"autoload/{join(parts[0 : -2], '/')}.vim" 50 var resolved_path = globpath(&runtimepath, path, 1, 1) 51 52 if !resolved_path->empty() 53 var function_pattern: string = $'^\s*\%(:\s*\)\=fun\%[ction]!\=\s\+\zs{curfunc}(' 54 resolved_path->Open(editcmd, function_pattern) 55 endif 56 return 57 endif 58 59 try 60 execute 'normal! ' .. editcmd 61 catch 62 Error(v:exception) 63 endtry 64 enddef 65 #}}}1 66 # Core {{{1 67 def HandlePackaddLine(editcmd: string, curline: string) #{{{2 68 var pat: string = '\s*\%(:\s*\)\=packadd!\=\s\+\zs\S\+\>\ze' 69 var plugin: string = curline 70 ->matchstr(pat) 71 ->substitute('^vim-\|\.vim$', '', 'g') 72 73 if plugin == '' 74 Fallback(editcmd) 75 else 76 var files: list<string> = getcompletion($'plugin/{plugin}', 'runtime') 77 ->map((_, fname: string) => fname->findfile(&rtp)->fnamemodify(':p')) 78 ->filter((_, path: string): bool => filereadable(path)) 79 if empty(files) 80 echo 'Could not find any plugin file for ' .. string(plugin) 81 return 82 endif 83 files->Open(editcmd) 84 endif 85 enddef 86 87 def HandleRuntimeLine(editcmd: string, curline: string, cfile: string) #{{{2 88 var fname: string 89 var where_pat: string = '\%(START\|OPT\|PACK\|ALL\)' 90 91 if cfile == 'runtime' || cfile =~# $'^{where_pat}$' 92 # then the cursor was not on one of the filenames, jump to the first file: 93 var fname_pat: string = $'\s*\%(:\s*\)\=ru\%[ntime]\%(!\s*\|\s\+\)\%({where_pat}\s\+\)\=\zs\S\+\>\ze' 94 fname = curline->matchstr(fname_pat) 95 else 96 fname = cfile 97 endif 98 99 if fname == '' 100 Fallback(editcmd) 101 else 102 var file: string = fname 103 ->findfile(&rtp) 104 ->fnamemodify(':p') 105 if file == '' || !filereadable(file) 106 echo 'Could not be found in the runtimepath: ' .. string(fname) 107 return 108 endif 109 file->Open(editcmd) 110 endif 111 enddef 112 113 def HandleColoLine(editcmd: string, curline: string) #{{{2 114 var pat: string = '\s*\%(:\s*\)\=colo\%[rscheme]\s\+\zs\S\+\>\ze' 115 var colo: string = curline->matchstr(pat) 116 117 if colo == '' 118 Fallback(editcmd) 119 else 120 var files: list<string> = getcompletion($'colors/{colo}', 'runtime') 121 ->map((_, fname: string) => fname->findfile(&rtp)->fnamemodify(':p')) 122 ->filter((_, path: string): bool => filereadable(path)) 123 if empty(files) 124 echo 'Could not find any colorscheme file for ' .. string(colo) 125 return 126 endif 127 files->Open(editcmd) 128 endif 129 enddef 130 131 def HandleImportLine(editcmd: string, curline: string) #{{{2 132 var fname: string 133 var import_cmd: string = '^\s*\%(:\s*\)\=import\s\+\%(autoload\s\+\)\=' 134 var import_alias: string = '\%(\s\+as\s\+\w\+\)\=$' 135 var import_string: string = import_cmd .. '\([''"]\)\zs.*\ze\1' .. import_alias 136 var import_expr: string = import_cmd .. '\zs.*\ze' .. import_alias 137 # the script is referred to by its name in a quoted string 138 if curline =~ import_string 139 fname = curline->matchstr(import_string) 140 # the script is referred to by an expression 141 elseif curline =~ import_expr 142 try 143 sandbox fname = curline 144 ->matchstr(import_expr) 145 ->eval() 146 catch 147 Error(v:exception) 148 return 149 endtry 150 endif 151 152 var filepath: string 153 if fname->isabsolutepath() 154 filepath = fname 155 elseif fname[0] == '.' 156 filepath = (expand('%:h') .. '/' .. fname)->simplify() 157 else 158 var subdir: string = curline =~ '^\s*import\s\+autoload\>' ? 'autoload' : 'import' 159 # Matching patterns in `'wildignore'` can be slow. 160 # Let's set `{nosuf}` to `true` to avoid `globpath()` to be slow. 161 filepath = globpath(&runtimepath, subdir .. '/' .. fname, true, true) 162 ->get(0, '') 163 endif 164 165 if !filepath->filereadable() 166 printf('E447: Can''t find file "%s" in path', fname) 167 ->Error() 168 return 169 endif 170 171 var how_to_split: string = { 172 gF: 'edit', 173 "\<C-W>F": 'split', 174 "\<C-W>gF": 'tab split', 175 }[editcmd] 176 execute how_to_split .. ' ' .. fnameescape(filepath) 177 enddef 178 179 def HandlePythonImportLine(editcmd: string, curline: string) #{{{2 180 # from ftplugin/python.vim 181 var grandparent_match: string = '^\(.\.\)\(\.*\)' 182 var grandparent_sub: string = '\=submatch(1)."/".repeat("../",strlen(submatch(2)))' 183 184 var parent_match: string = '^\.\(\.\)\@!' 185 var parent_sub: string = './' 186 187 var child_match: string = '\(\w\)\.\(\w\)' 188 var child_sub: string = '\1/\2' 189 190 var fname: string 191 192 if curline =~ '\<from\>' || curline =~ '\<as\>' 193 fname = curline->matchstr('^\s*py\%(thon\)\=3\s\+\%(from\|import\)\s\+\zs\S\+') 194 else 195 fname = ExpandCurWord('.') 196 endif 197 198 fname = fname 199 ->substitute(grandparent_match, grandparent_sub, '') 200 ->substitute(parent_match, parent_sub, '') 201 ->substitute(child_match, child_sub, 'g') 202 .. '.py' 203 204 var filepath: string = globpath(&runtimepath, 'python/' .. fname, true, true) 205 ->get(0, '') 206 207 if !filepath->filereadable() 208 printf('E447: Can''t find file "%s" in path', fname) 209 ->Error() 210 return 211 endif 212 213 var how_to_split: string = { 214 gF: 'edit', 215 "\<C-W>F": 'split', 216 "\<C-W>gF": 'tab split', 217 }[editcmd] 218 execute how_to_split .. ' ' .. fnameescape(filepath) 219 enddef 220 221 def Open(target: any, editcmd: string, search_pattern: string = '') #{{{2 222 var split: string = editcmd[0] == 'g' ? 'edit' : editcmd[1] == 'g' ? 'tabedit' : 'split' 223 var fname: string 224 var cmd: string 225 226 if target->typename() == 'list<string>' 227 if target->empty() 228 return 229 endif 230 fname = target[0] 231 else 232 if target->typename() != 'string' 233 return 234 endif 235 fname = target 236 endif 237 238 if search_pattern != '' 239 var escaped_pattern = escape(search_pattern, '\#'' ') 240 cmd = $'+silent\ call\ search(''{escaped_pattern}'')' 241 endif 242 243 execute $'{split} {cmd} {fnameescape(fname)}' 244 245 # If there are several files to open, put them into an arglist. 246 if target->typename() == 'list<string>' 247 && target->len() > 1 248 var arglist: list<string> = target 249 ->copy() 250 ->map((_, f: string) => f->fnameescape()) 251 execute $'arglocal {arglist->join()}' 252 endif 253 enddef 254 #}}}1 255 # Util {{{1 256 def Error(msg: string) #{{{2 257 echohl ErrorMsg 258 echomsg msg 259 echohl NONE 260 enddef 261 262 def Fallback(editcmd: string) #{{{2 263 try 264 execute 'normal! ' .. editcmd .. 'zv' 265 catch 266 Error(v:exception) 267 endtry 268 enddef 269 270 def ExpandCurWord(char: string): string #{{{2 271 var curfunc = '' 272 var saved_iskeyword = &iskeyword 273 274 try 275 execute 'set iskeyword+=' .. char 276 curfunc = expand('<cword>') 277 finally 278 &iskeyword = saved_iskeyword 279 endtry 280 281 return curfunc 282 enddef 283 284 # vim: sw=4 et