]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
add lsp format operator mapping (#546)
authorEnno <Konfekt@users.noreply.github.com>
Sat, 21 Sep 2024 23:57:31 +0000 (01:57 +0200)
committerGitHub <noreply@github.com>
Sat, 21 Sep 2024 23:57:31 +0000 (16:57 -0700)
* add lsp format operator mapping

* document normal mode lspformat mapping

* convert legacy to vim9 script

doc/lsp.txt
plugin/lsp.vim

index bd8f1232512a67bc2bfcbffc9ecd4f796d48c953..a40da7ad52b361c95b713b6ba29fcdfcd06f7ccc 100644 (file)
@@ -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 <plug>(LspFormat)
+                       in normal mode to operate on text objects.
 
                                                *:LspGotoDeclaration*
 :[count]LspGotoDeclaration
index 3b3815a168288fa1d5361b85cc79e69ba817b9fa..cc9b42b450dcafad6fdf7b883e25c77766fc24ba 100644 (file)
@@ -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(<range>, <line1>, <line2>)
+def LspFormatFunc(type: string, visual_mode = v:false)
+  if visual_mode
+    exe "normal! gv:LspFormat\<cr>"
+  elseif type ==# 'line'
+    exe "normal! '[V']:LspFormat\<cr>"
+  elseif type ==# 'char'
+    exe "normal! `[v`]:LspFormat\<cr>"
+  endif
+enddef
+nnoremap <silent> <plug>(LspFormat)  <Cmd>set operatorfunc=LspFormatFunc<cr>g@
+
 command! -nargs=0 -bar -count LspGotoDeclaration lsp.GotoDeclaration(v:false, <q-mods>, <count>)
 command! -nargs=0 -bar -count LspGotoDefinition lsp.GotoDefinition(v:false, <q-mods>, <count>)
 command! -nargs=0 -bar -count LspGotoImpl lsp.GotoImplementation(v:false, <q-mods>, <count>)