nvim-lspconfig icon indicating copy to clipboard operation
nvim-lspconfig copied to clipboard

Unable to rename on angular projects

Open zer09 opened this issue 2 years ago • 4 comments

Description

When renaming a variable or a function on typescritp angular project. rename is not working properly renaming unintended names.

Neovim version

NVIM v0.7.0-dev+1392-gdaa8ac051 Build type: RelWithDebInfo LuaJIT 2.1.0-beta3 Compilation: /usr/bin/gcc-11 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -DNVIM_TS_HAS_SET_MATCH_LIMIT -DNVIM_TS_HAS_SET_ALLOCATOR -O2 -g -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wdouble-promotion -Wmissing-noreturn -Wmissing-format-attribute -Wmissing-prototypes -Wimplicit-fallthrough -Wsuggest-attribute=pure -Wsuggest-attribute=const -Wsuggest-attribute=malloc -Wsuggest-attribute=cold -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=always -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=3 -I/home/runner/work/neovim/neovim/build/config -I/home/runner/work/neovim/neovim/src -I/home/runner/work/neovim/neovim/.deps/usr/include -I/usr/include -I/home/runner/work/neovim/neovim/build/src/nvim/auto -I/home/runner/work/neovim/neovim/build/include Compiled by runner@fv-az87-429

Features: +acl +iconv +tui See ":help feature-compile"

system vimrc file: "$VIM/sysinit.vim" fall-back for $VIM: "/share/nvim"

Nvim-lspconfig version

fd7843ad04cbc8ee2181bed9c3e83839b0d0b285

Operating system and version

5.16.14-1-MANJARO

Affected language servers

Typescript, Angular

Steps to reproduce

  1. execute lua vim.lsp.buf.rename()
  2. rename the variable on the cursor
  3. When pressing Enter it wont directly rename the variable you need to to press enter twice.

Actual behavior

The intended new variable name is not whats intended, it add some random characters.

Expected behavior

It should successfully rename the variable

Minimal config

local root_pattern = require("lspconfig.util").root_pattern
local lsp_installer = require("nvim-lsp-installer")

vim.diagnostic.config({
	virtual_text = false,
})

local servers = {
	angularls = {
		root_dir = root_pattern("angular.json"),
	},
	bashls = {},
	cssls = {},
	-- cssmodules_ls = {},
	eslint = {},
	html = {},
	-- sqlls = {},
	sqls = {
		cmd = { "sqls", "--config", vim.loop.cwd() .. "/sqls.yml" },
	},
	tsserver = {},
	yamlls = {},
	sumneko_lua = {
		settings = {
			Lua = {
				diagnostics = {
					globals = { "vim", "use", "require" },
				},
				workspace = {
					library = vim.api.nvim_get_runtime_file("", true),
					[vim.fn.expand("$VIMRUNTIME/lua")] = true,
					[vim.fn.expand("$VIMRUNTIME/lua/vim/lsp")] = true,
				},
				telemetry = {
					enable = false,
				},
			},
		},
	},
	jsonls = {
		settings = {
			json = {
				schemas = require("schemastore").json.schemas(),
			},
		},
	},
}

for name, _ in pairs(servers) do
	local server_is_found, server = lsp_installer.get_server(name)
	if server_is_found and not server:is_installed() then
		server:install()
	end
end

local null_ls_mapping = require("mappings").null_ls
local function tsAttach(client, bufnr)
	local ts_utils = require("nvim-lsp-ts-utils")
	ts_utils.setup({})
	ts_utils.setup_client(client)
	null_ls_mapping(bufnr)
end

-- Add additional capabilities supported by nvim-cmp
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.completion.completionItem.snippetSupport = true
capabilities = require("cmp_nvim_lsp").update_capabilities(capabilities)

local aerial = require("aerial")
local on_attach = require("mappings").lsp_on_attach
lsp_installer.on_server_ready(function(server)
	local config = servers[server.name] or {}
	config.capabilities = capabilities
	config.on_attach = function(client, bufnr)
		-- Disabled lsp formatting
		client.resolved_capabilities.document_formatting = false
		client.resolved_capabilities.document_range_formatting = false

		if server.name == "tsserver" then
			tsAttach(client, bufnr)
		end
		on_attach(bufnr)
		aerial.on_attach(client, bufnr)
	end
	server:setup(config)
end)

local function goto_definition(split_cmd)
	local util = vim.lsp.util
	local log = require("vim.lsp.log")
	local api = vim.api

	-- note, this handler style is for neovim 0.5.1/0.6, if on 0.5, call with function(_, method, result)
	local handler = function(_, result, ctx)
		if result == nil or vim.tbl_isempty(result) then
			local _ = log.info() and log.info(ctx.method, "No location found")
			return nil
		end

		local wc = 0
		local windows = vim.api.nvim_tabpage_list_wins(0)

		for _, v in pairs(windows) do
			local cfg = vim.api.nvim_win_get_config(v)
			local ft = vim.api.nvim_buf_get_option(vim.api.nvim_win_get_buf(v), "filetype")

			if (cfg.relative == "" or cfg.external == false) and ft ~= "qf" then
				wc = wc + 1
			end
		end

		if result[1].uri ~= ctx.params.textDocument.uri and wc < 3 then
			vim.cmd(split_cmd)
		end

		util.jump_to_location(result[1], "utf-8")

		if #result > 1 then
			util.set_qflist(util.locations_to_items(result))
			api.nvim_command("copen")
			api.nvim_command("wincmd p")
		end
	end

	return handler
end

vim.lsp.handlers["textDocument/definition"] = goto_definition("vsplit")

LSP log

https://gist.github.com/zer09/55f861f5f50a7c24626185f7898310d2

This is the recording Peek 2022-04-13 09-34

zer09 avatar Apr 13 '22 02:04 zer09

I am able to reproduce this in version 0.7.0

jugarpeupv avatar May 02 '22 04:05 jugarpeupv

Is your tsserver running alongside angularls? I had a similar renaming problem and setting server_capabilities.renameProvider to false for tsserver fixed it.

ziombo avatar May 25 '22 21:05 ziombo

@ziombo thanks great, It is working only if there if angularls is running. I need to check first if angularls is running before disable the renameProvider.

BTW what is the best way to check if the server is already running?

zer09 avatar May 25 '22 22:05 zer09

I'm not sure if I understand correctly. You work with typescript files that aren't angular files only, so if tsserver is the only LSP server, you don't want to disable the renameProvider?

If you want to first see if angularls is running, you can use vim.lsp.buf_get_clients() to get the names.

See how it's used here: https://github.com/feline-nvim/feline.nvim/blob/dd9e4199d1c05c3edc0f86820a83444486c84521/lua/feline/providers/lsp.lua#L18

ziombo avatar May 26 '22 10:05 ziombo

Excuse me, has the problem been solved, I also encountered the same problem?

oriming avatar Aug 08 '22 08:08 oriming

The problem is that both tsserver and angularls registers rename providers for typescript. So, disabling on of them should fix this issue, it did for me. I decided to disable angularls rename handler as tsserver is more than enough for my use case

require('lspconfig').angularls.setup({
    on_attach = function(client)
        client.server_capabilities.renameProvider = false
    end,
})

numToStr avatar Aug 10 '22 07:08 numToStr

closed . if still have problem I will reopen it.

glepnir avatar Aug 25 '22 12:08 glepnir