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
import './tools/strings.vim'
import './tools/perl.vim'
const lspServers = [
@ -121,17 +122,6 @@ const lspOptions = {
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
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<string>): 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))

View file

@ -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>): 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]

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