diff --git a/dot-config/vim/lsp.vim b/dot-config/vim/lsp.vim index c0e9b29..7aa7efb 100644 --- a/dot-config/vim/lsp.vim +++ b/dot-config/vim/lsp.vim @@ -1,5 +1,6 @@ vim9script +import './tools/strings.vim' import './tools/perl.vim' const lspServers = [ @@ -121,17 +122,6 @@ const lspOptions = { command! -nargs=* -complete=customlist,ListMissingServers -bar LspInstall Install() -# Convert a list of strings to a dictionary containing those same strings as -# keys, approximating a set type. You can use has_key() to test set membership -# on the result, rather than index(). -def ToStringSet(stringList: list): dict - var stringSet: dict - for string in stringList - stringSet[string] = true - endfor - return stringSet -enddef - def IsInstalled(server: dict): bool return server->has_key('installed') ? server.installed() : executable(server.path) == 1 enddef @@ -160,7 +150,7 @@ export def Configure(): void const warn = $'{missingCount} language server{missingCount > 1 ? "s are" : " is"} configured, but not installed. You may want to run :LspInstall.' augroup dot/vim/lsp.vim autocmd! - exe $'autocmd VimEnter * ++once echo "{escape(warn, '"\\')}"' + exe $'autocmd VimEnter * ++once echo {strings.Quote(warn)}' augroup END endif @@ -176,7 +166,7 @@ export def Install(...serverNames: list): void return endif - const serverNamesSet = ToStringSet(serverNames) + const serverNamesSet = strings.ToStringSet(serverNames) const serversToInstall = empty(serverNamesSet) ? missingServers : missingServers->copy()->filter((_, server): bool => serverNamesSet->has_key(server.name)) diff --git a/dot-config/vim/statusline.vim b/dot-config/vim/statusline.vim index 33d9531..e0705c9 100644 --- a/dot-config/vim/statusline.vim +++ b/dot-config/vim/statusline.vim @@ -1,27 +1,7 @@ vim9script import autoload ($XDG_CACHE_HOME .. '/vim/pack/minpac/start/vim-crystalline/autoload/crystalline.vim') as cr - -def DropIfDefault(status: string, default: string): string - if status == default - return '' - endif - return status -enddef - -def PrependIfVisible(status: string, prefix: string): string - if empty(status) - return '' - endif - return prefix .. status -enddef - -def AppendIfVisible(status: string, affix: string): string - if empty(status) - return '' - endif - return status .. affix -enddef +import './tools/strings.vim' as st def StatuslineSection(seps: number, group: string, components: list): string const hiGroup = cr.ModeGroup(group) @@ -34,21 +14,21 @@ def GitStatus(): string return '' endif - const branch = g:FugitiveHead()->PrependIfVisible("\ue0a0 ") # nf-pl-branch + const branch = g:FugitiveHead()->st.PrependIfVisible("\ue0a0 ") # nf-pl-branch if !empty(branch) return branch endif return g:FugitiveHead(7) - ->PrependIfVisible("\Uf135e (") # nf-md-head - ->AppendIfVisible(")") + ->st.PrependIfVisible("\Uf135e (") # nf-md-head + ->st.AppendIfVisible(")") enddef def StatuslineLeft(window: number, inactive: bool): string const bufnr = window->winbufnr() const b = bufnr->getbufvar('&') const fileName = [ - bufname(bufnr)->g:nerdfont#find()->DropIfDefault(g:nerdfont#default)->AppendIfVisible(' '), + bufname(bufnr)->g:nerdfont#find()->st.DropIfDefault(g:nerdfont#default)->st.AppendIfVisible(' '), b.buftype == '' ? '%t' : '%f', b.modifiable && b.modified ? cr.ModeHiItem('Modified') .. '+' .. cr.ModeHiItem('Fill') : '', b.readonly ? " \uf023" : '', # nf-fa-lock @@ -60,7 +40,7 @@ def StatuslineLeft(window: number, inactive: bool): string const info = StatuslineSection(0, 'B', [ GitStatus(), g:battery#component_escaped(), - ])->AppendIfVisible(' ' .. cr.Sep(0, cr.ModeGroup('B'), cr.ModeGroup('Fill'))) + ])->st.AppendIfVisible(' ' .. cr.Sep(0, cr.ModeGroup('B'), cr.ModeGroup('Fill'))) const vimMode = cr.ModeSection(0, 'A', empty(info) ? 'Fill' : 'B') return join([vimMode, info, fileName]) @@ -113,7 +93,7 @@ def InitTab() width += 2 endif - const icon = bufname(bufnr)->g:nerdfont#find()->DropIfDefault(g:nerdfont#default)->PrependIfVisible(' ') + const icon = bufname(bufnr)->g:nerdfont#find()->st.DropIfDefault(g:nerdfont#default)->st.PrependIfVisible(' ') const iconWidth = strchars(icon) if width + iconWidth >= maxWidth return [tabDisplay, width] diff --git a/dot-config/vim/tools/strings.vim b/dot-config/vim/tools/strings.vim new file mode 100644 index 0000000..36b2840 --- /dev/null +++ b/dot-config/vim/tools/strings.vim @@ -0,0 +1,49 @@ +vim9script + +# Convert a list of strings to a dictionary containing those same strings as +# keys, approximating a set type. You can use has_key() to test set membership +# on the result, rather than index(). +export def ToStringSet(stringList: list): dict + var stringSet: dict + for string in stringList + stringSet[string] = true + endfor + return stringSet +enddef + + +# Returns the given status if it's not the same string as the provided +# 'default'. Otherwise returns an empty string. +export def DropIfDefault(status: string, default: string): string + if status == default + return '' + endif + return status +enddef + +# Prepends the given prefix to the status, but only when the status is +# non-empty. Very useful for constructing a statusline with optional segments. +export def PrependIfVisible(status: string, prefix: string): string + if empty(status) + return '' + endif + return prefix .. status +enddef + +# Appends the given affix to the status, but only when the status is +# non-empty. Quite similar to PrependIfVisible, and useful in the same sorts +# of situations. +export def AppendIfVisible(status: string, affix: string): string + if empty(status) + return '' + endif + return status .. affix +enddef + +# Double-quotes a string, so it can safely be substituted into a Vim command +# with :execute or eval() and be interpreted as the same string. +export def Quote(str: string): string + return '"' .. escape(str, '"\\') .. '"' +enddef + +defcompile