LuaLS / LuaLS/lua-language-server

Swap Params codeAction breaks code

オープン
#3,413 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Lua
スター
4.4k
フォーク
442
PR マージ指標
30日以内にマージされた PR はありません

説明

How are you using the lua-language-server?

NeoVim

Which OS are you using?

MacOS

What is the issue affecting?

Formatting

Expected Behaviour

Write lua code in neovim with codeActions triggered on buffer save. The lua code looks like the following:

vim.api.nvim_create_autocmd('BufWritePre', {})

and save, it will remain unchanged.

Actual Behaviour

It changes the code to the following:

vim.api.nvim_create_autocmd({}, 'BufWritePre')
Reproduction steps
  1. Install lua-language-server
  2. Setup lua lsp in neovim (can expand if needed)
  3. Add the following autocmd to run codeactions on save
vim.api.nvim_create_autocmd('BufWritePre', {
	group = vim.api.nvim_create_augroup('auto-format', { clear = false }),
	buffer = ev.buf,
	callback = function(evt)
		local params = vim.lsp.util.make_range_params()
		params.context = { }
		local result = vim.lsp.buf_request_sync(evt.buf, "textDocument/codeAction", params)
		for _, res in pairs(result or {}) do
			for _, r in pairs(res.result or {}) do
				if r.edit then
					local enc = client.offset_encoding or "utf-16"
					vim.lsp.util.apply_workspace_edit(r.edit, enc)
				end
			end
		end

		vim.lsp.buf.format({ bufnr = ev.buf, id = client.id, timeout_ms = 1000, async = false })
	end,
})
  1. Write some lua code and save that lua code with the cursor still on the same line.
Additional Notes

I was editing my neovim config and the lsp kept switching params like the following:

vim.api.nvim_create_autocmd('BufWritePre', {})

And after my PreWrite autocmd calls codeActions

vim.api.nvim_create_autocmd({}, 'BufWritePre')

which immediately breaks the code because the params are position sensitive.

It only does this if the cursor is on the line of the method call which I suppose makes sense since the code action is called with a range. My full autocmd works like this:

vim.api.nvim_create_autocmd('BufWritePre', {
	group = vim.api.nvim_create_augroup('auto-format', { clear = false }),
	buffer = ev.buf,
	callback = function(evt)
		local params = vim.lsp.util.make_range_params()
		params.context = { }
		local result = vim.lsp.buf_request_sync(evt.buf, "textDocument/codeAction", params)
		for _, res in pairs(result or {}) do
			for _, r in pairs(res.result or {}) do
				if r.edit then
					local enc = client.offset_encoding or "utf-16"
					vim.lsp.util.apply_workspace_edit(r.edit, enc)
				end
			end
		end

		vim.lsp.buf.format({ bufnr = ev.buf, id = client.id, timeout_ms = 1000, async = false })
	end,
})

I narrowed it down to this code action here https://github.com/LuaLS/lua-language-server/blob/b5e57c36a9a27b89eb283861fb8946fa787e37d8/script/core/code-action.lua#L462

Maybe I am holding it wrong and it is not supposed to be used in this way but I don't have a problem with other LSPs.

Log File

No log entry was added during this behaviour.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、レポートにある位置引数の例を使い、保存時にコードアクションがトリガーされる NeoVim で問題を再現します。次に、script/core/code-action.lua の 462 行目付近を調べ、「Swap Params」編集を追跡します。コードアクションの実行後も例が変更されない状態になれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
lua, neovim
領域
devtools, tooling
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
静か
明瞭さ
明確に書かれている
初心者へのやさしさ
68/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。