Skip to content

Commit

Permalink
add lsp format operator mapping (#546)
Browse files Browse the repository at this point in the history
* add lsp format operator mapping

* document normal mode lspformat mapping

* convert legacy to vim9 script
  • Loading branch information
Konfekt authored Sep 21, 2024
1 parent 5a7e33e commit c813516
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion doc/lsp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions plugin/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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>)
Expand Down

1 comment on commit c813516

@Konfekt
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no clue why, but the functions is not defined while everything else loads, for example the accompanying mapping;
sourcing the function again by :so makes everything work

Please sign in to comment.