Open bombela opened 1 year ago
:set formatexpr=CocAction('formatSelected')
Reproduced, fail to do formatting.
xmap <leader>f <Plug>(coc-format-selected)
nmap <leader>f <Plug>(coc-format-selected)
works as expected.
Will debug on the first one.
My usage with coc-format-selected
, whole line or multiple lines:
V
to select whole linecoc-format-selected
with gq
:
V
to select whole line, for example current line number is 21gq
v:lnum,v:count,mode()
returns 21, 0, n
, coc stops to do formatting because count = 0.
another test, gggqG
, v:lnum,v:count,mode()
returns 1 0 n
, coc stops cause count = 0.
Have no idea why vim returns v:count
0.
:h formatexpr
document:
The |v:lnum| variable holds the first line to be formatted.
The |v:count| variable holds the number of lines to be formatted.
Thank you for investigating! I confirm v:count is zero after running gq
(over multiple lines). I guess when the format handler returns -1
, neovim falls-back to the default?
I will see if I can debug some more. I wonder if vim behaves similar.
format handler returns -1, neovim falls-back to the default
:h formatexpr
:
When the expression evaluates to non-zero Vim will fall back to using
the internal format mechanism.
v:count is zero after running gq (over multiple lines)
This is the key issue, have no idea why v:count
is 0.
if vim behaves similar.
The same.
function! Test()
echomsg "->" v:lnum v:count v:char mode()
return 0
endfunction
:set formatexpr=Test()
v:lnum and v:count are both correct in the :messages
output.
I don't really know how the async stuff in typescript/nodejs interacts with neovim. But I think what we observe is a race condition. The value of v:count
is only valid during the synchronous execution of the expression in formatexp
. After that, the selection is cleared, and v:count
becomes zero.
@bombela set formatexpr=CocAction('formatSelected')
works with clangd
, but not rust-analyzer.
Both gq2j
or V
select two lines then gq
, works as expected with clangd.
What's the output of
:CocInfo
What's the output of
:CocCommand rust-analyzer.serverVersion
What's your coc-rust-analyzer version? You can get it from
:CocList extensions
In my coc-config I also have:
And
:echo CocHasProvider('formatRange')
returnsv:true
.(by the way,
"rust-analyzer.rustfmt.enableRangeFormatting": true
enables the feature but"rust-analyzer": { "rustfmt.enableRangeFormatting": true }
does not. Took me 45minutes to realize that).Then I do
:set formatexpr=CocAction('formatSelected')
. Now I expectgq
on a visual selection to format similar to RustFmt. Instead it formats the same as the default vim behavior.:CoInfo
reports nothing of interest besidesRequest action: formatSelected []
.As a baseline, I can compare with rustfmt nightly and
:'<,'>RustFmtRange
.Just to make sure things are happening at all, I can
:set formatexpr=CocAction('wontwork')
, and in this case it silently doesn't format anything. And:CoInfo
reportsRequest error: wontwork [] Error: Action "wontwork" not exist
.I can also
:set formatexpr=CocAction('format')
, which does indeed format the whole file like RustFmt, confirming again that something is really executed.So it appears that
CocAction('formatSelected')
with coc-rust-analyzer somehow falls-back to the default neovim formatting. I am not sure how to debug the issue further.