Skip to content

Add LanguageClient_formatOnSave config option #1216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions doc/LanguageClient.txt
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,24 @@ Default: []
For a full list of the possible token types (name) see the LSP documentation:
https://microsoft.github.io/language-server-protocol/specifications/specification-current/

2.48 g:LanguageClient_formatOnSave *g:LanguageClient_formatOnSave*

A list of filetypes for which you want to run format after saving the buffer.
If you include '*' in this list, format will be run on any filetype, ignoring
any other values in the list.

Default: []

Example:

Format only go and rust filetypes:
>
let g:LanguageClient_formatOnSave = ['go', 'rust']

Format all filetypes:
>
let g:LanguageClient_formatOnSave = ['*']

==============================================================================
3. Commands *LanguageClientCommands*

Expand Down
22 changes: 22 additions & 0 deletions plugin/LanguageClient.vim
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,24 @@ function! s:OnFileType()
call s:ConfigureAutocmds()
endfunction

function! s:AfterFormat(...) abort
noautocmd w
endfunction

function! s:FormatTextDocument(...)
if !LanguageClient#HasCommand(&filetype)
return
endif

let l:format_on_save = get(g:, 'LanguageClient_formatOnSave', [])
echom index(l:format_on_save, '*') ==# -1
if index(l:format_on_save, &filetype) ==# -1 && index(l:format_on_save, '*') ==# -1
return
endif

noautocmd call LanguageClient#textDocument_formatting({}, funcref('s:AfterFormat'))
endfunction

function! s:ConfigureAutocmds()
augroup languageClient
autocmd!
Expand All @@ -197,6 +215,10 @@ function! s:ConfigureAutocmds()
autocmd CompleteChanged <buffer> call LanguageClient#handleCompleteChanged(deepcopy(v:event))
endif

if len(get(g:, 'LanguageClient_formatOnSave', [])) != 0
autocmd BufWritePre <buffer> call s:FormatTextDocument()
endif

nnoremap <Plug>(lcn-menu) :call LanguageClient_contextMenu()<CR>
nnoremap <Plug>(lcn-hover) :call LanguageClient_textDocument_hover()<CR>
nnoremap <Plug>(lcn-rename) :call LanguageClient_textDocument_rename()<CR>
Expand Down