From c8135167184b277db9940b075b3d8a6142614b74 Mon Sep 17 00:00:00 2001 From: Enno Date: Sun, 22 Sep 2024 01:57:31 +0200 Subject: [PATCH] add lsp format operator mapping (#546) * add lsp format operator mapping * document normal mode lspformat mapping * convert legacy to vim9 script --- doc/lsp.txt | 3 ++- plugin/lsp.vim | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/doc/lsp.txt b/doc/lsp.txt index bd8f1232..a40da7ad 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -815,7 +815,8 @@ can map these commands to keys and make it easier to invoke them. current buffer are used when format is applied. :{range}LspFormat Format the specified range of lines in the current - file using the language server. + file using the language server. Map (LspFormat) + in normal mode to operate on text objects. *:LspGotoDeclaration* :[count]LspGotoDeclaration diff --git a/plugin/lsp.vim b/plugin/lsp.vim index 3b3815a1..cc9b42b4 100644 --- a/plugin/lsp.vim +++ b/plugin/lsp.vim @@ -74,7 +74,19 @@ command! -nargs=0 -bar LspDiagShow lsp.ShowDiagnostics() command! -nargs=0 -bar LspDiagHere lsp.JumpToDiag('here') command! -nargs=0 -bar LspDocumentSymbol lsp.ShowDocSymbols() command! -nargs=0 -bar LspFold lsp.FoldDocument() + command! -nargs=0 -bar -range=% LspFormat lsp.TextDocFormat(, , ) +def LspFormatFunc(type: string, visual_mode = v:false) + if visual_mode + exe "normal! gv:LspFormat\" + elseif type ==# 'line' + exe "normal! '[V']:LspFormat\" + elseif type ==# 'char' + exe "normal! `[v`]:LspFormat\" + endif +enddef +nnoremap (LspFormat) set operatorfunc=LspFormatFuncg@ + command! -nargs=0 -bar -count LspGotoDeclaration lsp.GotoDeclaration(v:false, , ) command! -nargs=0 -bar -count LspGotoDefinition lsp.GotoDefinition(v:false, , ) command! -nargs=0 -bar -count LspGotoImpl lsp.GotoImplementation(v:false, , )