# Get the keyword prefix before the current cursor column.
var col = charcol('.')
var starttext = getline('.')[ : col - 1]
- var prefix = tolower(matchstr(starttext, '\k*$'))
+ var prefix = starttext->tolower()->matchstr('\k*$')
var completeItems: list<dict<any>> = []
for item in items
else
# plain text completion. If the completion item text doesn't start with
# the current (case ignored) keyword prefix, skip it.
- if prefix != '' && stridx(tolower(d.word), prefix) != 0
+ if prefix != '' && d.word->tolower()->stridx(prefix) != 0
continue
endif
endif
return lspserver.tagFunc(pat, flags, info)
enddef
+# Function to use with the 'formatexpr' option.
+export def FormatExpr(): number
+ var lspserver: dict<any> = buf.CurbufGetServerChecked()
+ if lspserver->empty()
+ return 1
+ endif
+
+ lspserver.textDocFormat(@%, true, v:lnum, v:lnum + v:count - 1)
+ return 0
+enddef
+
export def RegisterCmdHandler(cmd: string, Handler: func)
codeaction.RegisterCmdHandler(cmd, Handler)
enddef
lspserver.signaturePopup = -1
lspserver.workspaceFolders = [getcwd()]
- var job = job_start(cmd, opts)
+ var job = cmd->job_start(opts)
if job->job_status() == 'fail'
util.ErrMsg($'Error: Failed to start LSP server {lspserver.path}')
return 1
# Send a request message to LSP server
def SendMessage(lspserver: dict<any>, content: dict<any>): void
var ch = lspserver.job->job_getchannel()
- if ch_status(ch) != 'open'
+ if ch->ch_status() != 'open'
# LSP server has exited
return
endif
req.params->extend(params)
var ch = lspserver.job->job_getchannel()
- if ch_status(ch) != 'open'
+ if ch->ch_status() != 'open'
# LSP server has exited
return {}
endif
req.params->extend(params)
var ch = lspserver.job->job_getchannel()
- if ch_status(ch) != 'open'
+ if ch->ch_status() != 'open'
# LSP server has exited
return -1
endif
if rangeFormat
var r: dict<dict<number>> = {
start: {line: start_lnum - 1, character: 0},
- end: {line: end_lnum - 1, character: 99999}}
+ end: {line: end_lnum - 1, character: charcol([end_lnum, '$']) - 1}}
param.range = r
endif
Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
For Vim version 9.0 and above
-Last change: Nov 26, 2022
+Last change: March 16, 2023
==============================================================================
*lsp-license*
the symbol is defined in multiple places in the code.
==============================================================================
-9. Call Hierarchy *lsp-call-hierarchy*
+9. Code Formatting
+
+The |:LspFormat| command can be used to format either the entire file or a
+selected range of lines using the language server. The *shiftwidth* and
+*expandtab* values set for the current buffer are used when format is applied.
+
+To format code using the 'gq' command, you can set the 'formatexpr' option:
+>
+ setlocal formatexpr=lsp#lsp#FormatExpr()
+<
+==============================================================================
+10. Call Hierarchy *lsp-call-hierarchy*
The |:LspIncomingCalls| and the |:LspOutoingCalls| commands can be used to
display the call hierarchy of a symbol. For example, the functions calling a
refreshed to display the outgoing calls.
==============================================================================
-10. Autocommands *lsp-autocmds*
+11. Autocommands *lsp-autocmds*
*LspAttached*
LspAttached A |User| autocommand fired when the LSP client
has processed the diagnostics.
==============================================================================
-11. Highlight Groups *lsp-highlight-groups*
+12. Highlight Groups *lsp-highlight-groups*
The following highlight groups are used by the LSP plugin. You can define
these highlight groups in your .vimrc file before sourcing this plugin to
LspInlayHintsType Used to highlight inlay hints of kind "type".
==============================================================================
-12. Debugging *lsp-debug*
+13. Debugging *lsp-debug*
To debug this plugin, you can log the language server protocol messages sent
and received by the plugin from the language server. The following command
:LspServerTrace { off | messages | verbose }
<
==============================================================================
-12. Custom Command Handlers *lsp-custom-commands*
+14. Custom Command Handlers *lsp-custom-commands*
When applying a code action, the language server may issue a non-standard
command. For example, the Java language server uses non-standard commands
:%bw!
enddef
+# Test for formatting a file using 'formatexpr'
+def Test_LspFormatExpr()
+ :silent! edit Xtest.c
+ sleep 200m
+ setlocal formatexpr=lsp#lsp#FormatExpr()
+ setline(1, [' int i;', ' int j;'])
+ :redraw!
+ normal! ggVGgq
+ assert_equal(['int i;', 'int j;'], getline(1, '$'))
+
+ # empty line/file
+ deletebufline('', 1, '$')
+ setline(1, [''])
+ redraw!
+ normal! ggVGgq
+ assert_equal([''], getline(1, '$'))
+
+ setlocal formatexpr&
+ :%bw!
+enddef
+
# Test for :LspShowReferences - showing all the references to a symbol in a
# file using LSP
def Test_LspShowReferences()