Closed towry closed 2 months ago
It is this line causing the difference: https://github.com/neoclide/coc.nvim/blob/2ec3166653f76acd42b0d9f62161c4db2673c204/src/model/document.ts#L349
tracked down to line https://github.com/neoclide/coc.nvim/blob/2ec3166653f76acd42b0d9f62161c4db2673c204/autoload/coc/compat.vim#L18
turns out when format without change, the arguments passed to nvim_buf_set_lines is (bufnr, 1, 1, 0, {})
And i test this function on a buffer, it did change the buffer and create empty change.
From :h nvim_buf_set_lines:
To insert lines at a given index, set
start
andend
to the same index. To delete a range of lines, setreplacement
to an empty array.
So (bufnr, 1, 1, 0, {})
will do nothing but make an empty change since a:start
is same as a:end
and replacement is empty.
Thank you for your tracking, but I can't reproduce this with your mini.nivm and formatted demo.lua, with coc-sumneko-lua on nvim nightly. And I couldn't reproduce with other language servers too.
The checking fix in #4995 is OK, but I couldn't result any format without change, coc.nvim couldn't run to this check.
@fannheyward Just tested on different version of coc.
coc release branch coc.nvim version: 0.0.82-2ec31666 2024-04-23 09:10:14 +0800
have issue.
coc tag v0.0.82, v0.0.81 doesn't have issue.
I believe this issue was introduced after v0.0.82.
another issue in format is that I have to use call CocAction('format') | sleep 1m
to make it work in BufWritePre
, otherwise the buf sometimes will be in dirty state.
But in v0.0.82, it works well without the need of sleep 1m
.
You can discard this pr, and i will just stick to version v0.0.82.
I believe this issue was introduced after v0.0.82.
Thank you for your tests with different releases of coc, I don't complain your PR, but how to reproduce this, here's my tests:
mkdir -p test-4994
@statiolake/coc-stylua
because sumneko-lua already supports formatting, no need to use stylus any more.nvim-treesitter
, otherwise, nvim reports highlighting error augroup vimrc | autocmd! | augroup end
set nocompatible
set number
set laststatus=2
let g:coc_global_extensions = ['coc-sumneko-lua']
" change this to your local coc.nvim location.
set runtimepath^=/Users/fannheyward/.local/share/nvim/plugged/coc.nvim
set runtimepath^=/Users/fannheyward/.local/share/nvim/plugged/nvim-treesitter
filetype plugin indent on
syntax on
autocmd BufWritePre * call CocAction('format')
command! -nargs=0 Format :call CocActionAsync('format')
set expandtab shiftwidth=4 tabstop=4 softtabstop=4
nvim -u mini.vim demo.lua
, sumneko-lua reports unused local foo
, the Lua LS is working:call CocAction('format')
, no changes:Format
, no changes:w
to fire autocmd, no changesAlso I've tested the formatting with tsserver, gopls, rust-analyzer servers, won't change on formatted files.
You can use coc.preferences.formatOnSave
instead of BufWritePre.
Result from CocInfo
Describe the bug
In Vim, after run
:Format
on already formatted file do not change the file, but it does change the file in Neovim (tested on 0.83,0.9,0.91,nightly versions).The inconvenience result is that:
u
to undo multiple empty changes (0 changes; before#1
1 second ago), and pressu
will always move your cursor to the 2 line at the same column before you pressu
.BufWritePre
autocmd to format this file, sometimes the file is not saved.Reproduce the bug
We will close your issue when you don't provide minimal vimrc and we can't reproduce it
mini.vim
with:Note, the demo file should be already formated, if it is not, format and save it;
nvim --clean -u mini.vim demo.lua
:Format
[+]
.u
will move cursor to 2 line at the same column before theu
.Screenshots (optional)
https://github.com/neoclide/coc.nvim/assets/8279858/dfce5ff5-8658-4ff6-8de8-42e4a05a5d7e