|
| 1 | +defmodule NextLS.ASTHelpers do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + @spec get_attribute_reference_name(String.t(), integer(), integer()) :: String.t() | nil |
| 5 | + def get_attribute_reference_name(file, line, column) do |
| 6 | + ast = ast_from_file(file) |
| 7 | + |
| 8 | + {_ast, name} = |
| 9 | + Macro.prewalk(ast, nil, fn |
| 10 | + {:@, [line: ^line, column: ^column], [{name, _meta, nil}]} = ast, _acc -> {ast, "@#{name}"} |
| 11 | + other, acc -> {other, acc} |
| 12 | + end) |
| 13 | + |
| 14 | + name |
| 15 | + end |
| 16 | + |
| 17 | + @spec get_module_attributes(String.t(), module()) :: [{atom(), String.t(), integer(), integer()}] |
| 18 | + def get_module_attributes(file, module) do |
| 19 | + reserved_attributes = Module.reserved_attributes() |
| 20 | + |
| 21 | + symbols = parse_symbols(file, module) |
| 22 | + |
| 23 | + Enum.filter(symbols, fn |
| 24 | + {:attribute, "@" <> name, _, _} -> |
| 25 | + not Map.has_key?(reserved_attributes, String.to_atom(name)) |
| 26 | + |
| 27 | + _other -> |
| 28 | + false |
| 29 | + end) |
| 30 | + end |
| 31 | + |
| 32 | + defp parse_symbols(file, module) do |
| 33 | + ast = ast_from_file(file) |
| 34 | + |
| 35 | + {_ast, %{symbols: symbols}} = |
| 36 | + Macro.traverse(ast, %{modules: [], symbols: []}, &prewalk/2, &postwalk(&1, &2, module)) |
| 37 | + |
| 38 | + symbols |
| 39 | + end |
| 40 | + |
| 41 | + # add module name to modules stack on enter |
| 42 | + defp prewalk({:defmodule, _, [{:__aliases__, _, module_name_atoms} | _]} = ast, acc) do |
| 43 | + modules = [module_name_atoms | acc.modules] |
| 44 | + {ast, %{acc | modules: modules}} |
| 45 | + end |
| 46 | + |
| 47 | + defp prewalk(ast, acc), do: {ast, acc} |
| 48 | + |
| 49 | + defp postwalk({:@, meta, [{name, _, args}]} = ast, acc, module) when is_list(args) do |
| 50 | + ast_module = |
| 51 | + acc.modules |
| 52 | + |> Enum.reverse() |
| 53 | + |> List.flatten() |
| 54 | + |> Module.concat() |
| 55 | + |
| 56 | + if module == ast_module do |
| 57 | + symbols = [{:attribute, "@#{name}", meta[:line], meta[:column]} | acc.symbols] |
| 58 | + {ast, %{acc | symbols: symbols}} |
| 59 | + else |
| 60 | + {ast, acc} |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + # remove module name from modules stack on exit |
| 65 | + defp postwalk({:defmodule, _, [{:__aliases__, _, _modules} | _]} = ast, acc, _module) do |
| 66 | + [_exit_mudule | modules] = acc.modules |
| 67 | + {ast, %{acc | modules: modules}} |
| 68 | + end |
| 69 | + |
| 70 | + defp postwalk(ast, acc, _module), do: {ast, acc} |
| 71 | + |
| 72 | + defp ast_from_file(file) do |
| 73 | + file |> File.read!() |> Code.string_to_quoted!(columns: true) |
| 74 | + end |
| 75 | +end |
0 commit comments