Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make goto def/refs work when cursor is at end of symbol (#1038) #1087

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions apps/language_server/lib/language_server/codefragment_utils.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule ElixirLS.LanguageServer.CodeFragmentUtils do
alias ElixirSense.Core.Normalized.Code, as: NormalizedCode

def surround_context_with_fallback(code, {line, column}, options \\ []) do
case NormalizedCode.Fragment.surround_context(code, {line, column}, options) do
:none ->
{NormalizedCode.Fragment.surround_context(code, {line, max(column - 1, 1)}, options),
column - 1}

%{context: {:dot, _, _}} ->
{NormalizedCode.Fragment.surround_context(code, {line, max(column - 1, 1)}, options),
column - 1}

context ->
{context, column}
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ defmodule ElixirLS.LanguageServer.Providers.Definition.Locator do
alias ElixirSense.Core.Parser

alias ElixirLS.LanguageServer.Plugins.Phoenix.Scope
alias ElixirSense.Core.Normalized.Code, as: NormalizedCode
alias ElixirLS.LanguageServer.CodeFragmentUtils

def definition(code, line, column, options \\ []) do
case NormalizedCode.Fragment.surround_context(code, {line, column}) do
:none ->
case CodeFragmentUtils.surround_context_with_fallback(code, {line, column}) do
{:none, _} ->
nil

context ->
{context, column} ->
metadata =
Keyword.get_lazy(options, :metadata, fn ->
Parser.parse_string(code, true, true, {line, column})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ defmodule ElixirLS.LanguageServer.Providers.Hover.Docs do
alias ElixirSense.Core.TypeInfo
alias ElixirSense.Core.Parser

alias ElixirLS.LanguageServer.CodeFragmentUtils

@type markdown :: String.t()

@type module_doc :: %{kind: :module, docs: markdown, metadata: map, module: module()}
Expand Down Expand Up @@ -71,11 +73,11 @@ defmodule ElixirLS.LanguageServer.Providers.Hover.Docs do
|> Kernel.--([:exception, :message])

def docs(code, line, column, options \\ []) do
case NormalizedCode.Fragment.surround_context(code, {line, column}) do
:none ->
case CodeFragmentUtils.surround_context_with_fallback(code, {line, column}) do
{:none, _} ->
nil

%{begin: begin_pos, end: end_pos} = context ->
{%{begin: begin_pos, end: end_pos} = context, column} ->
metadata =
Keyword.get_lazy(options, :metadata, fn ->
Parser.parse_string(code, true, true, {line, column})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ defmodule ElixirLS.LanguageServer.Providers.Implementation.Locator do
alias ElixirLS.LanguageServer.Location
alias ElixirSense.Core.Parser
alias ElixirSense.Core.Normalized.Code, as: NormalizedCode
alias ElixirLS.LanguageServer.CodeFragmentUtils

require ElixirSense.Core.Introspection, as: Introspection

def implementations(code, line, column, options \\ []) do
case NormalizedCode.Fragment.surround_context(code, {line, column}) do
:none ->
case CodeFragmentUtils.surround_context_with_fallback(code, {line, column}) do
{:none, _} ->
[]

context ->
{context, column} ->
metadata =
Keyword.get_lazy(options, :metadata, fn ->
Parser.parse_string(code, true, true, {line, column})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ defmodule ElixirLS.LanguageServer.Providers.References.Locator do
alias ElixirSense.Core.SurroundContext
alias ElixirSense.Core.Parser

alias ElixirLS.LanguageServer.CodeFragmentUtils

def references(code, line, column, trace, options \\ []) do
case NormalizedCode.Fragment.surround_context(code, {line, column}) do
:none ->
case CodeFragmentUtils.surround_context_with_fallback(code, {line, column}) do
{:none, _} ->
[]

%{
begin: {begin_line, begin_col}
} = context ->
{%{begin: {begin_line, begin_col}} = context, column} ->
metadata =
Keyword.get_lazy(options, :metadata, fn ->
Parser.parse_string(code, true, true, {line, column})
Expand Down
64 changes: 64 additions & 0 deletions apps/language_server/test/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,70 @@ defmodule ElixirLS.LanguageServer.ServerTest do
wait_until_compiled(server)
end)
end

test "definition at end of symbol", %{server: server} do
in_fixture(__DIR__, "clean", fn ->
uri = "file:///file.ex"
code = ~S(
defmodule Test do
def test, do: nil
end
defmodule OtherModule do
def test do
Test
Test.test(\)
_ = &Test.test/0
end
end
)
fake_initialize(server)
Server.receive_packet(server, did_open(uri, "elixir", 1, code))
Server.receive_packet(server, definition_req(1, uri, 6, 17))

assert_receive(
response(1, %{
"range" => %{
"end" => %{"character" => 8, "line" => 1},
"start" => %{"character" => 8, "line" => 1}
},
"uri" => ^uri
}),
3000
)

wait_until_compiled(server)

Server.receive_packet(server, definition_req(2, uri, 7, 17))

assert_receive(
response(2, %{
"range" => %{
"end" => %{"character" => 8, "line" => 1},
"start" => %{"character" => 8, "line" => 1}
},
"uri" => ^uri
}),
3000
)

wait_until_compiled(server)

Server.receive_packet(server, definition_req(3, uri, 8, 22))

assert_receive(
response(3, %{
"range" => %{
"end" => %{"character" => 8, "line" => 1},
"start" => %{"character" => 8, "line" => 1}
},
"uri" => ^uri
}),
3000
)

wait_until_compiled(server)
end)
end
end

describe "textDocument/implementation" do
Expand Down