Skip to content

Instantly share code, notes, and snippets.

@bfrg
Last active May 3, 2022 11:19
Show Gist options
  • Save bfrg/a7343cb63b4a7170a140e9b5b9326b23 to your computer and use it in GitHub Desktop.
Save bfrg/a7343cb63b4a7170a140e9b5b9326b23 to your computer and use it in GitHub Desktop.
ftplugin for vim9script: add proper 'include', 'includeexpr', 'define', and 'path' values for vimscript files
vim9script
# File: autoload/ft/vim/includeexpr.vim
export def Main(fname: string): string
if fname =~# '^import\s\+autoload\s\+\(["'']\)\f\+\1$'
return 'autoload/' .. matchstr(fname, '^import\s\+autoload\s\+\([''"]\)\zs\f\+\ze\1$')
elseif fname =~# '^import\s\+\(["'']\)\f\+\1'
return matchstr(fname, '^import\s\+\([''"]\)\zs\f\+\ze\1$')
endif
return fname
enddef
vim9script
# File: after/ftplugin/vim.vim
# Proper 'include', 'includeexpr', 'define', and 'path' settings for vim9script
#
# TODO pressing gf on file.vim in "import autoload 'file.vim'" doesn't work yet
import autoload 'ft/vim/includeexpr.vim'
# Set up include-search for [i, [d and friends in vim9script
&l:include = '^\s*\zsimport\s\+\%(autoload\s\+\)\?\(["'']\)\f\+\1\ze\($\|\s\+as\s\+.*$\)'
&l:includeexpr = 'includeexpr.Main(v:fname)'
&l:define = '^\s*\%('
.. '\%(\<export\>\)\=\s*\<\%(cons\%[t]\|let\|var\|def\)\>'
.. '\|\<fu\%[nction]\>'
.. '\|^\s*com\%[mand]!\=\s\+\%(-\w\+\%(=\S\+\)*\s\+\)*'
.. '\)'
# We need to move the '.' in '&path' to the end, or [d, [i, gf and friends won't
# work when both $VIMHOME/autoload/foo/bar.vim and $VIMHOME/import/foo/bar.vim
# exist while editing a file in $VIMHOME/autoload/ since the search starts
# relative to the directory of the current file.
# TODO we need to add all runtimepath directories, or autoload/ won't be found
# when [D or [I are invoked while not in plugin root directory
&l:path = printf('%s,.,,%s,',
split(&runtimepath, '[^\\]\zs,')
->map((_, i) => i .. (exists('+shellslash') && !&shellslash ? '\' : '/') .. 'import')
->join(','),
$VIMHOME
)
b:undo_ftplugin = get(b:, 'undo_ftplugin', 'execute') .. ' | setlocal include< includeexpr< define< path<'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment