Skip to content
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

[draft] Add formatting support #471

Open
wants to merge 1 commit into
base: master
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
83 changes: 83 additions & 0 deletions autoload/lsc/format.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
" https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_formatting
" for the whole document
" and
" https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_rangeFormatting
" for ranges
"
"
"

function! s:FormattingOptions()
" TODO Not sure what things like clangd do with this stuff when they read
" their own configuration files, but it's a non-optional field in the
" formatting request params. Work out what it does, and make it configurable
" to users (probably per language, but maybe also globally; or both)
return {
\ 'tabSize': 8,
\ 'insertSpaces': v:false,
\ 'trimTrailingWhitespace': v:true,
\ 'insertFinalNewline': v:true,
\ 'trimFinalNewlines': v:true,
\}
endfunction

function! s:ApplyEdit(textedit)
" FIXME This is a hack. We probably don't want to use hack around workspace
" edits here
let l:workspace_edit = {'changes': {lsc#uri#documentUri(): a:textedit}}
return lsc#edit#apply(l:workspace_edit)
endfunction

function! s:OnResult(textedit) abort
"Response:

" result: TextEdit[] | null describing the modification to the document to be formatted.
" error: code and message set in case an exception happens during the range formatting request.
"
" Since version 3.18.0

"call lsc#message#error('onresult')
if a:textedit == v:null | return | endif
call lsc#message#log('got edits' . json_encode(a:textedit), 'Info')
Copy link
Contributor

@arp242 arp242 Oct 12, 2024

Choose a reason for hiding this comment

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

It probably shouldn't have these log lines (there's a few more of them)?

return s:ApplyEdit(a:textedit)
endfunction

function! s:OnSkip(bufnr, textedit) abort
call lsc#message#error('formatting skipped for ')
endfunction

function! s:startDocFormattingRequest() abort
let l:params = lsc#params#textDocument()
let l:params['options'] = s:FormattingOptions()
let l:server = lsc#server#forFileType(&filetype)[0]
call lsc#message#log('sending message' . json_encode(l:params), 'Info')
call l:server.request('textDocument/formatting', l:params,
\ lsc#util#gateResult('FormatDoc',
\ function('<SID>OnResult', []),
\ function('<SID>OnSkip', [bufnr('%')])))
endfunction

function! s:startRangeFormattingRequest(range) abort
let l:params = lsc#params#textDocument()
let l:params['range'] = a:range
let l:params['options'] = s:FormattingOptions()
let l:server = lsc#server#forFileType(&filetype)[0]
call l:server.request('textDocument/rangeFormatting', l:params,
\ lsc#util#gateResult('FormatRange',
\ function('<SID>OnResult', []),
\ function('<SID>OnSkip', [bufnr('%')])))
endfunction

function! lsc#format#formatFile() abort
return s:startDocFormattingRequest()
endfunction

function! lsc#format#formatRange() range abort
let l:range = {
\ 'start': {'line': a:firstline - 1, 'character': 0},
\ 'end': {'line': a:lastline - 1, 'character': strlen(getline("."))}
\}
call lsc#message#log('formatting range' . json_encode(l:range), 'Info')

return s:startRangeFormattingRequest(l:range)
endfunction
2 changes: 2 additions & 0 deletions plugin/lsc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ command! LSClientDisable call lsc#server#disable()
command! LSClientEnable call lsc#server#enable()
command! LSClientDisableDiagnosticHighlights call <SID>DisableHighlights()
command! LSClientEnableDiagnosticHighlights call <SID>EnableHighlights()
command! LSClientFormat call lsc#format#formatFile()
command! -range LSClientFormat '<,'> call lsc#format#formatRange()
Copy link
Contributor

Choose a reason for hiding this comment

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

This overrides the :LSClientFormat that calls formatFile(); running it doesn't work:

:100verbose LSClientFormat
Executing: '<,'> call lsc#format#formatRange()
E20: Mark not set

If I do use visual mode to set <' and '> I get:

[lsc:Error] JSON RPC method not found: "RangeFormatting" not yet implemented

I guess my LSP server doesn't implement that.

Anyway, if I comment out this line then it works. Probably need to be a bit smarter about the range being passed and what mode it's in.


if !exists('g:lsc_enable_apply_edit') || g:lsc_enable_apply_edit
command! -nargs=? LSClientRename call lsc#edit#rename(<args>)
Expand Down