Compare commits

...

2 commits

3 changed files with 40 additions and 12 deletions

View file

@ -105,7 +105,7 @@ import "./statusline.vim"
statusline.Init() statusline.Init()
import "./lsp.vim" import "./lsp.vim"
lsp.LazyConfigure() lsp.Configure()
set formatexpr=lsp#lsp#FormatExpr() set formatexpr=lsp#lsp#FormatExpr()
set keywordprg=:LspHover set keywordprg=:LspHover

View file

@ -1,5 +1,7 @@
vim9script vim9script
import './tools/perl.vim'
const lspServers = [ const lspServers = [
{ {
name: 'dockerfile-langserver', name: 'dockerfile-langserver',
@ -25,6 +27,8 @@ const lspServers = [
install: 'npm install -g perlnavigator-server', install: 'npm install -g perlnavigator-server',
}, },
perl.Lsp('Perl::LanguageServer', ['-e', 'Perl::LanguageServer::run']),
{ {
name: 'taplo', name: 'taplo',
filetype: 'toml', filetype: 'toml',
@ -128,25 +132,22 @@ def ToStringSet(stringList: list<string>): dict<bool>
return stringSet return stringSet
enddef enddef
def IsInstalled(server: dict<any>): bool
return server->has_key('installed') ? server.installed() : executable(server.path) == 1
enddef
def InstalledServers(): list<dict<any>> def InstalledServers(): list<dict<any>>
return lspServers->deepcopy()->filter((_, server): bool => executable(server.path) == 1) return lspServers->deepcopy()->filter((_, server): bool => server->IsInstalled())
enddef enddef
def MissingServers(): list<dict<any>> def MissingServers(): list<dict<any>>
return lspServers->deepcopy()->filter((_, server): bool => executable(server.path) == 0) return lspServers->deepcopy()->filter((_, server): bool => !server->isInstalled())
enddef enddef
def ListMissingServers(argLead: string, cmdLine: string, cursorPos: number): list<string> def ListMissingServers(argLead: string, cmdLine: string, cursorPos: number): list<string>
return MissingServers()->mapnew((_, server): string => server.name) return MissingServers()->mapnew((_, server): string => server.name)
enddef enddef
export def LazyConfigure(): void
augroup lspLazyConfigure
autocmd!
autocmd VimEnter * ++once Configure()
augroup END
enddef
export def Configure(): void export def Configure(): void
# We have to use final rather than const because LspAddServer() assumes it can # We have to use final rather than const because LspAddServer() assumes it can
# modify the dicts it gets, rather than making a fresh copy to mess with. # modify the dicts it gets, rather than making a fresh copy to mess with.
@ -154,8 +155,8 @@ export def Configure(): void
if len(lspServers) != len(installedServers) if len(lspServers) != len(installedServers)
echo $'{len(lspServers) - len(installedServers)} language servers are configured, but not installed. You may want to run :LspInstall.' echo $'{len(lspServers) - len(installedServers)} language servers are configured, but not installed. You may want to run :LspInstall.'
endif endif
g:LspAddServer(installedServers) g:lsp#lsp#AddServer(installedServers)
g:LspOptionsSet(lspOptions) g:lsp#options#OptionsSet(lspOptions)
enddef enddef
export def Install(...serverNames: list<string>): void export def Install(...serverNames: list<string>): void

View file

@ -0,0 +1,27 @@
vim9script
export def Lsp(module: string, args: list<string>): dict<any>
const lsp = {
name: module,
filetype: 'perl',
path: exepath('perl'),
args: [$'-M{module}']->extend(args),
install: $'cpanm {module}',
installed: (): bool => HasModule(module),
}
return lsp
enddef
export def HasModule(module: string): bool
# This can more reliably be tested by calling `perl -MModule::Name -E ''`
# and checking the exit status, but simply looking for a matching file on
# disk is much faster and works fine for my purposes.
const libs = $PERL5LIB->split(';')
const filename = module->substitute('::', '/', 'g') .. '.pm'
for lib in libs
if filereadable($'{lib}/{filename}')
return true
endif
endfor
return false
enddef