Skip to content

Commit

Permalink
fix references
Browse files Browse the repository at this point in the history
  • Loading branch information
biletskyy committed Sep 13, 2023
1 parent adb396d commit cbaba0e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
23 changes: 22 additions & 1 deletion lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,20 @@ defmodule NextLS do
[module, "alias"]
)

{:attribute, module, attribute} ->
DB.query(
database,
~Q"""
SELECT file, start_line, end_line, start_column, end_column
FROM "references" as refs
WHERE refs.identifier = ?
AND refs.type = ?
AND refs.module = ?
AND refs.source = 'user'
""",
[attribute, "attribute", module]
)

:unknown ->
[]
end
Expand Down Expand Up @@ -266,7 +280,7 @@ defmodule NextLS do
filtered_symbols =
for {pid, _} <- entries, symbol <- symbols.(pid), score = fuzzy_match(symbol.name, query, case_sensitive?) do
name =
if symbol.type != "defstruct" do
if symbol.type not in ["defstruct", "attribute"] do
"#{symbol.type} #{symbol.name}"
else
"#{symbol.name}"
Expand Down Expand Up @@ -679,6 +693,7 @@ defmodule NextLS do

defp elixir_kind_to_lsp_kind("defmodule"), do: GenLSP.Enumerations.SymbolKind.module()
defp elixir_kind_to_lsp_kind("defstruct"), do: GenLSP.Enumerations.SymbolKind.struct()
defp elixir_kind_to_lsp_kind("attribute"), do: GenLSP.Enumerations.SymbolKind.property()

defp elixir_kind_to_lsp_kind(kind) when kind in ["def", "defp", "defmacro", "defmacrop"],
do: GenLSP.Enumerations.SymbolKind.function()
Expand Down Expand Up @@ -737,11 +752,17 @@ defmodule NextLS do
[[module, "defmacro", function]] ->
{:function, module, function}

[[module, "attribute", attribute]] ->
{:attribute, module, attribute}

_unknown_definition ->
case DB.query(database, reference_query, [file, line, col]) do
[[function, "function", module]] ->
{:function, module, function}

[[attribute, "attribute", module]] ->
{:attribute, module, attribute}

[[_alias, "alias", module]] ->
{:module, module}

Expand Down
2 changes: 1 addition & 1 deletion lib/next_ls/runtime/sidecar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule NextLS.Runtime.Sidecar do
end

def handle_info({:tracer, payload}, state) do
"Elixir." <> module_name = to_string(payload.module)
module_name = payload.module |> to_string() |> String.replace("Elixir.", "")
all_symbols = parse_symbols(payload.file, module_name)
attributes = filter_attributes(all_symbols)

Expand Down
53 changes: 53 additions & 0 deletions test/next_ls/references_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ defmodule NextLS.ReferencesTest do
Peace.and_love()
end
end
defmodule Foo do
@foo_attr 123
def foo_foo(a) do
{:ok, a + @foo_attr}
end
def foo2 do
{:error, @foo_attr}
end
end
""")

[bar: bar, peace: peace]
Expand Down Expand Up @@ -105,4 +117,45 @@ defmodule NextLS.ReferencesTest do
}
]
end

test "list attribute references", %{client: client, bar: bar} = context do
assert :ok == notify(client, %{method: "initialized", jsonrpc: "2.0", params: %{}})
assert_request(client, "client/registerCapability", fn _params -> nil end)
assert_is_ready(context, "my_proj")
assert_compiled(context, "my_proj")
assert_notification "$/progress", %{"value" => %{"kind" => "end", "message" => "Finished indexing!"}}

request(client, %{
method: "textDocument/references",
id: 4,
jsonrpc: "2.0",
params: %{
position: %{line: 8, character: 4},
textDocument: %{uri: uri(bar)},
context: %{includeDeclaration: true}
}
})

uri = uri(bar)

assert_result2(
4,
[
%{
"uri" => uri,
"range" => %{
"start" => %{"line" => 11, "character" => 14},
"end" => %{"line" => 11, "character" => 23}
}
},
%{
"uri" => uri,
"range" => %{
"start" => %{"line" => 15, "character" => 13},
"end" => %{"line" => 15, "character" => 22}
}
}
]
)
end
end

0 comments on commit cbaba0e

Please sign in to comment.