Relocate string utility functions into ./tools/strings.vim

This commit is contained in:
Danielle McLean 2023-10-28 13:46:36 +11:00
parent 8d7870ae5e
commit 6d7037d88e
Signed by: 00dani
GPG key ID: 52C059C3B22A753E
3 changed files with 59 additions and 40 deletions

View file

@ -1,5 +1,6 @@
vim9script vim9script
import './tools/strings.vim'
import './tools/perl.vim' import './tools/perl.vim'
const lspServers = [ const lspServers = [
@ -121,17 +122,6 @@ const lspOptions = {
command! -nargs=* -complete=customlist,ListMissingServers -bar LspInstall Install(<f-args>) command! -nargs=* -complete=customlist,ListMissingServers -bar LspInstall Install(<f-args>)
# 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<string>): dict<bool>
var stringSet: dict<bool>
for string in stringList
stringSet[string] = true
endfor
return stringSet
enddef
def IsInstalled(server: dict<any>): bool def IsInstalled(server: dict<any>): bool
return server->has_key('installed') ? server.installed() : executable(server.path) == 1 return server->has_key('installed') ? server.installed() : executable(server.path) == 1
enddef 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.' 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 augroup dot/vim/lsp.vim
autocmd! autocmd!
exe $'autocmd VimEnter * ++once echo "{escape(warn, '"\\')}"' exe $'autocmd VimEnter * ++once echo {strings.Quote(warn)}'
augroup END augroup END
endif endif
@ -176,7 +166,7 @@ export def Install(...serverNames: list<string>): void
return return
endif endif
const serverNamesSet = ToStringSet(serverNames) const serverNamesSet = strings.ToStringSet(serverNames)
const serversToInstall = empty(serverNamesSet) const serversToInstall = empty(serverNamesSet)
? missingServers ? missingServers
: missingServers->copy()->filter((_, server): bool => serverNamesSet->has_key(server.name)) : missingServers->copy()->filter((_, server): bool => serverNamesSet->has_key(server.name))

View file

@ -1,27 +1,7 @@
vim9script vim9script
import autoload ($XDG_CACHE_HOME .. '/vim/pack/minpac/start/vim-crystalline/autoload/crystalline.vim') as cr import autoload ($XDG_CACHE_HOME .. '/vim/pack/minpac/start/vim-crystalline/autoload/crystalline.vim') as cr
import './tools/strings.vim' as st
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
def StatuslineSection(seps: number, group: string, components: list<string>): string def StatuslineSection(seps: number, group: string, components: list<string>): string
const hiGroup = cr.ModeGroup(group) const hiGroup = cr.ModeGroup(group)
@ -34,21 +14,21 @@ def GitStatus(): string
return '' return ''
endif endif
const branch = g:FugitiveHead()->PrependIfVisible("\ue0a0 ") # nf-pl-branch const branch = g:FugitiveHead()->st.PrependIfVisible("\ue0a0 ") # nf-pl-branch
if !empty(branch) if !empty(branch)
return branch return branch
endif endif
return g:FugitiveHead(7) return g:FugitiveHead(7)
->PrependIfVisible("\Uf135e (") # nf-md-head ->st.PrependIfVisible("\Uf135e (") # nf-md-head
->AppendIfVisible(")") ->st.AppendIfVisible(")")
enddef enddef
def StatuslineLeft(window: number, inactive: bool): string def StatuslineLeft(window: number, inactive: bool): string
const bufnr = window->winbufnr() const bufnr = window->winbufnr()
const b = bufnr->getbufvar('&') const b = bufnr->getbufvar('&')
const fileName = [ 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.buftype == '' ? '%t' : '%f',
b.modifiable && b.modified ? cr.ModeHiItem('Modified') .. '+' .. cr.ModeHiItem('Fill') : '', b.modifiable && b.modified ? cr.ModeHiItem('Modified') .. '+' .. cr.ModeHiItem('Fill') : '',
b.readonly ? " \uf023" : '', # nf-fa-lock b.readonly ? " \uf023" : '', # nf-fa-lock
@ -60,7 +40,7 @@ def StatuslineLeft(window: number, inactive: bool): string
const info = StatuslineSection(0, 'B', [ const info = StatuslineSection(0, 'B', [
GitStatus(), GitStatus(),
g:battery#component_escaped(), 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') const vimMode = cr.ModeSection(0, 'A', empty(info) ? 'Fill' : 'B')
return join([vimMode, info, fileName]) return join([vimMode, info, fileName])
@ -113,7 +93,7 @@ def InitTab()
width += 2 width += 2
endif 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) const iconWidth = strchars(icon)
if width + iconWidth >= maxWidth if width + iconWidth >= maxWidth
return [tabDisplay, width] return [tabDisplay, width]

View file

@ -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<string>): dict<bool>
var stringSet: dict<bool>
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