Skip to content
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: 12 additions & 6 deletions autoload/lsp/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1031,8 +1031,8 @@ def DocRangeFormat(fname: string, line1: number, line2: number, canFallback: boo
if canFallback
util.WarnMsg('Formatting unsupported; falling back to built-in.')
execute $'keepjumps normal! {line1}Ggq{line2}G'
elseif !&filetype->empty()
util.ErrMsg($'Language server for "{&filetype}" file type supporting "documentRangeFormatting" feature is not found')
# elseif !&filetype->empty()
# util.ErrMsg($'Language server for "{&filetype}" file type supporting "documentRangeFormatting" feature is not found')
else
util.ErrMsg('No language server supporting "documentRangeFormatting" feature is found')
endif
Expand All @@ -1051,10 +1051,10 @@ def DocFormat(fname: string, canFallback: bool)
if canFallback
util.WarnMsg('Formatting unsupported; falling back to built-in.')
execute 'keepjumps normal! 1GgqG'
elseif !&filetype->empty()
util.ErrMsg($'Language server for "{&filetype}" file type supporting "documentFormatting" feature is not found')
# else
# util.ErrMsg('No language server supporting "documentFormatting" feature is found')
# elseif !&filetype->empty()
# util.ErrMsg($'Language server for "{&filetype}" file type supporting "documentFormatting" feature is not found')
else
util.ErrMsg('No language server supporting "documentFormatting" feature is found')
endif
enddef

Expand Down Expand Up @@ -1289,6 +1289,12 @@ enddef
export def FormatExpr(): number
var lspserver: dict<any> = buf.CurbufGetServerChecked('documentRangeFormatting')
if lspserver->empty()
if &formatexpr == 'lsp#lsp#FormatExpr()'
defer execute("setl formatexpr='lsp#lsp#FormatExpr()'")
setl formatexpr=''
execute $'keepjumps normal! {v:lnum}Ggq{v:lnum + v:count - 1}G'
return 0
endif
return 1
endif

Expand Down
11 changes: 8 additions & 3 deletions test/clangd_tests.vim
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,12 @@ def g:Test_LspFormat()
bw!

# empty file
assert_equal('', execute('LspFormat'))
assert_equal('Error: No language server supporting "documentFormatting" feature is found',
execute('LspFormat')->split("\n")[0])

# file without an LSP server
edit a.raku
assert_equal('Error: Language server for "raku" file type supporting "documentFormatting" feature is not found',
assert_equal('Error: No language server supporting "documentFormatting" feature is found',
execute('LspFormat')->split("\n")[0])

:%bw!
Expand All @@ -164,7 +165,11 @@ def g:Test_LspFormatExpr()
setline(1, [' int i;', ' int j;'])
:redraw!
normal! ggVGgq
assert_equal(['int i;', 'int j;'], getline(1, '$'))
if v:version == 900
g:WaitForAssert(() => assert_equal([' int i;', ' int j;'], getline(1, '$')))
elseif g:WaitForAssert(() => assert_equal(['int i;', 'int j;'], getline(1, '$')))
g:WaitForAssert(() => assert_equal([' int i; int j;'], getline(1, '$')))
endif

# empty line/file
deletebufline('', 1, '$')
Expand Down