-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
feat: module attributes #215
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
127328d
feat: go to module attribute definition
biletskyy 2d9e63e
fix references
biletskyy 29f3c06
extract to ASTHelpers module and some small fixes
biletskyy d171420
fix spec
biletskyy d9042f3
fix error get_attribute_reference_name never nil
biletskyy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | nil | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be helpful to extra a module for single use AST functions, to get them out of this sidecar process, as it's just meant to be a sidecar/proxy.
You can then unit test them if you'd like (I don't think it's necessary for this PR, but as we discover more complicated edge cases, would be useful)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that's good idea. I extracted it. I'm not happy about module design but I think it could be refactored later with more usecases.
I'm not sure about naming so if you like call it differently I don't mind