Skip to content

Commit

Permalink
extract to ASTHelpers module and some small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
biletskyy committed Sep 15, 2023
1 parent cbaba0e commit 488b943
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 73 deletions.
75 changes: 75 additions & 0 deletions lib/next_ls/ast_helpers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
defmodule NextLS.ASTHelpers do
@moduledoc false

@spec get_attribute_reference_name(String.t(), integer(), integer()) :: String.t()
def get_attribute_reference_name(file, line, column) do
ast = ast_from_file(file)

{_ast, name} =
Macro.prewalk(ast, nil, fn
{:@, [line: ^line, column: ^column], [{name, _meta, nil}]} = ast, _acc -> {ast, name}
other, acc -> {other, acc}
end)

"@#{name}"
end

@spec get_module_attributes(String.t(), module()) :: [{atom(), String.t(), integer(), integer()}]
def get_module_attributes(file, module) do
reserved_attributes = Module.reserved_attributes()

symbols = parse_symbols(file, module)

Enum.filter(symbols, fn
{:attribute, "@" <> name, _, _} ->
not Map.has_key?(reserved_attributes, String.to_atom(name))

_other ->
false
end)
end

defp parse_symbols(file, module) do
ast = ast_from_file(file)

{_ast, %{symbols: symbols}} =
Macro.traverse(ast, %{modules: [], symbols: []}, &prewalk/2, &postwalk(&1, &2, module))

symbols
end

# add module name to modules stack on enter
defp prewalk({:defmodule, _, [{:__aliases__, _, module_name_atoms} | _]} = ast, acc) do
modules = [module_name_atoms | acc.modules]
{ast, %{acc | modules: modules}}
end

defp prewalk(ast, acc), do: {ast, acc}

defp postwalk({:@, meta, [{name, _, args}]} = ast, acc, module) when is_list(args) do
ast_module =
acc.modules
|> Enum.reverse()
|> List.flatten()
|> Module.concat()

if module == ast_module do
symbols = [{:attribute, "@#{name}", meta[:line], meta[:column]} | acc.symbols]
{ast, %{acc | symbols: symbols}}
else
{ast, acc}
end
end

# remove module name from modules stack on exit
defp postwalk({:defmodule, _, [{:__aliases__, _, _modules} | _]} = ast, acc, _module) do
[_exit_mudule | modules] = acc.modules
{ast, %{acc | modules: modules}}
end

defp postwalk(ast, acc, _module), do: {ast, acc}

defp ast_from_file(file) do
file |> File.read!() |> Code.string_to_quoted!(columns: true)
end
end
77 changes: 4 additions & 73 deletions lib/next_ls/runtime/sidecar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule NextLS.Runtime.Sidecar do
@moduledoc false
use GenServer

alias NextLS.ASTHelpers
alias NextLS.DB

def start_link(args) do
Expand All @@ -15,30 +16,16 @@ defmodule NextLS.Runtime.Sidecar do
end

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

attributes = ASTHelpers.get_module_attributes(payload.file, payload.module)
payload = Map.put_new(payload, :symbols, attributes)
DB.insert_symbol(state.db, payload)

{:noreply, state}
end

def handle_info({{:tracer, :reference, :attribute}, payload}, state) do
ast = payload.file |> File.read!() |> Code.string_to_quoted!(columns: true)
location = [line: payload.meta[:line], column: payload.meta[:column]]

{_ast, name} =
Macro.prewalk(ast, nil, fn
{:@, ^location, [{name, _meta, nil}]} = ast, _acc -> {ast, name}
other, acc -> {other, acc}
end)

if name do
payload = %{payload | identifier: "@#{name}"}
DB.insert_reference(state.db, payload)
end
name = ASTHelpers.get_attribute_reference_name(payload.file, payload.meta[:line], payload.meta[:column])
if name, do: DB.insert_reference(state.db, %{payload | identifier: name})

Check warning on line 28 in lib/next_ls/runtime/sidecar.ex

View workflow job for this annotation

GitHub Actions / dialyzer

guard_fail

The guard clause can never succeed.

{:noreply, state}
end
Expand All @@ -54,60 +41,4 @@ defmodule NextLS.Runtime.Sidecar do

{:noreply, state}
end

defp filter_attributes(symbols) do
symbols
|> Enum.filter(&match?({:attribute, _, _, _}, &1))
|> Enum.reject(fn {_, "@" <> name, _, _} ->
Map.has_key?(Module.reserved_attributes(), String.to_atom(name))
end)
end

defp parse_symbols(file, module) do
ast = file |> File.read!() |> Code.string_to_quoted!(columns: true)

{_ast, %{symbols: symbols}} =
Macro.traverse(ast, %{modules: [], symbols: []}, &prewalk/2, &postwalk(&1, &2, module))

symbols
end

# add module name to modules stack on enter
defp prewalk({:defmodule, _, [{:__aliases__, _, modules} | _]} = ast, acc) do
modules_string =
modules
|> Enum.map(&Atom.to_string/1)
|> Enum.intersperse(".")
|> List.to_string()

modules = [modules_string | acc.modules]

{ast, %{acc | modules: modules}}
end

defp prewalk(ast, acc), do: {ast, acc}

defp postwalk({:@, meta, [{name, _, args}]} = ast, acc, module) when is_list(args) do
# get current module for this node
ast_module =
acc.modules
|> Enum.reverse()
|> Enum.intersperse(".")
|> List.to_string()

if module == ast_module do
symbols = [{:attribute, "@#{name}", meta[:line], meta[:column]} | acc.symbols]
{ast, %{acc | symbols: symbols}}
else
{ast, acc}
end
end

# remove module name from modules stack on exit
defp postwalk({:defmodule, _, [{:__aliases__, _, _modules} | _]} = ast, acc, _module) do
[_exit_mudule | modules] = acc.modules
{ast, %{acc | modules: modules}}
end

defp postwalk(ast, acc, _module), do: {ast, acc}
end

0 comments on commit 488b943

Please sign in to comment.