From 55c8310b5bf3b83af787657c6789b2755dfee649 Mon Sep 17 00:00:00 2001 From: Rin Date: Tue, 7 May 2024 12:56:42 +0800 Subject: [PATCH] fix: offset column by 1 to make goto def/refs work when cursor is at end of symbol (#1038) Co-authored-by: Cocoa --- .../lib/language_server/providers/definition.ex | 10 +++++++++- .../lib/language_server/providers/references.ex | 13 ++++++++++++- .../providers/references/locator.ex | 14 +++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/apps/language_server/lib/language_server/providers/definition.ex b/apps/language_server/lib/language_server/providers/definition.ex index 25ea03bf1..ddf972c3b 100644 --- a/apps/language_server/lib/language_server/providers/definition.ex +++ b/apps/language_server/lib/language_server/providers/definition.ex @@ -16,7 +16,15 @@ defmodule ElixirLS.LanguageServer.Providers.Definition do result = case Locator.definition(source_file.text, line, character, metadata: metadata) do nil -> - nil + case Locator.definition(source_file.text, line, max(character - 1, 1), + metadata: metadata + ) do + nil -> + nil + + %ElixirLS.LanguageServer.Location{} = location -> + Protocol.Location.new(location, uri, source_file.text, project_dir) + end %ElixirLS.LanguageServer.Location{} = location -> Protocol.Location.new(location, uri, source_file.text, project_dir) diff --git a/apps/language_server/lib/language_server/providers/references.ex b/apps/language_server/lib/language_server/providers/references.ex index e03302128..830abf48e 100644 --- a/apps/language_server/lib/language_server/providers/references.ex +++ b/apps/language_server/lib/language_server/providers/references.ex @@ -25,7 +25,18 @@ defmodule ElixirLS.LanguageServer.Providers.References do Build.with_build_lock(fn -> trace = ElixirLS.LanguageServer.Tracer.get_trace() - Locator.references(source_file.text, line, character, trace, metadata: metadata) + references = + case Locator.references(source_file.text, line, character, trace, metadata: metadata) do + [] -> + Locator.references(source_file.text, line, max(character - 1, 1), trace, + metadata: metadata + ) + + references -> + references + end + + references |> Enum.map(fn elixir_sense_reference -> elixir_sense_reference |> build_reference(uri, source_file.text, project_dir) diff --git a/apps/language_server/lib/language_server/providers/references/locator.ex b/apps/language_server/lib/language_server/providers/references/locator.ex index 5e96a6724..b96b39e32 100644 --- a/apps/language_server/lib/language_server/providers/references/locator.ex +++ b/apps/language_server/lib/language_server/providers/references/locator.ex @@ -20,7 +20,19 @@ defmodule ElixirLS.LanguageServer.Providers.References.Locator do alias ElixirSense.Core.Parser def references(code, line, column, trace, options \\ []) do - case NormalizedCode.Fragment.surround_context(code, {line, column}) do + {context, column} = + case NormalizedCode.Fragment.surround_context(code, {line, column}) do + :none -> + {:none, column} + + %{context: {:dot, _, _}} -> + {NormalizedCode.Fragment.surround_context(code, {line, max(column - 1, 1)}), column - 1} + + context -> + {context, column} + end + + case context do :none -> []