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

Added extractors for defimpl and defprotocol #666

Merged
merged 4 commits into from
Mar 28, 2024
Merged
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
4 changes: 2 additions & 2 deletions apps/common/lib/lexical/ast/analysis.ex
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ defmodule Lexical.Ast.Analysis do
]} = quoted,
state
) do
module = protocol_segments
module = protocol_segments ++ for_segments
line = meta[:line]
current_module_alias = Alias.new(module, :__MODULE__, line)
for_alias = Alias.new(for_segments, :"@for", line)
protocol_alias = Alias.new(module, :"@protocol", line)
protocol_alias = Alias.new(protocol_segments, :"@protocol", line)

state
|> maybe_push_implicit_alias(protocol_segments, line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,34 @@ defmodule Lexical.RemoteControl.Search.Indexer.Extractors.Module do

require Logger

@definition_mappings %{
defmodule: :module,
defprotocol: :protocol
}
@module_definitions Map.keys(@definition_mappings)

# extract a module definition
def extract(
{:defmodule, defmodule_meta,
{definition, defmodule_meta,
[{:__aliases__, module_name_meta, module_name}, module_block]} = defmodule_ast,
%Reducer{} = reducer
) do
)
when definition in @module_definitions do
%Block{} = block = Reducer.current_block(reducer)

case resolve_alias(reducer, module_name) do
{:ok, aliased_module} ->
module_position = Metadata.position(module_name_meta)
range = to_range(reducer, module_name, module_position)
detail_range = to_range(reducer, module_name, module_position)

entry =
Entry.block_definition(
reducer.analysis.document.path,
block,
Subject.module(aliased_module),
:module,
@definition_mappings[definition],
block_range(reducer.analysis.document, defmodule_ast),
range,
detail_range,
Application.get_application(aliased_module)
)

Expand All @@ -53,6 +60,41 @@ defmodule Lexical.RemoteControl.Search.Indexer.Extractors.Module do
end
end

# defimpl MyProtocol, for: MyStruct do ...
def extract(
{:defimpl, _,
[
{:__aliases__, module_name_meta, module_name},
_for_block,
_impl_body
]} = defimpl_ast,
%Reducer{} = reducer
) do
%Block{} = block = Reducer.current_block(reducer)

case resolve_alias(reducer, module_name) do
{:ok, aliased_module} ->
module_position = Metadata.position(module_name_meta)
detail_range = to_range(reducer, module_name, module_position)

entry =
Entry.block_definition(
reducer.analysis.document.path,
block,
Subject.module(aliased_module),
:protocol_implementation,
block_range(reducer.analysis.document, defimpl_ast),
detail_range,
Application.get_application(aliased_module)
)

{:ok, entry}

_ ->
:ignored
end
end

# This matches an elixir module reference
def extract({:__aliases__, metadata, maybe_module}, %Reducer{} = reducer)
when is_list(maybe_module) do
Expand Down
26 changes: 21 additions & 5 deletions apps/remote_control/test/lexical/remote_control/analyzer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ defmodule Lexical.RemoteControl.AnalyzerTest do
test "works with @protocol in a protocol" do
{position, document} =
~q[
defimpl MyProtocol, for: Atom do
defimpl MyProtocol, for: Atom do

def pack(atom) do
|
def pack(atom) do
|
end
end
end
]
]
|> pop_cursor(as: :document)

analysis = Ast.analyze(document)
Expand All @@ -60,6 +60,22 @@ defmodule Lexical.RemoteControl.AnalyzerTest do
position
)
end

test "identifies the module in a protocol implementation" do
{position, document} =
~q[
defimpl MyProtocol, for: Atom do

def pack(atom) do
|
end
end
]
|> pop_cursor(as: :document)

analysis = Ast.analyze(document)
assert {:ok, MyProtocol.Atom} == Analyzer.current_module(analysis, position)
end
end

describe "reanalyze_to/2" do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
defmodule Lexical.RemoteControl.Search.Indexer.Extractors.ProtocolTest do
use Lexical.Test.ExtractorCase

def index(source) do
do_index(source, &(&1.type in [:protocol, :protocol_implementation]))
end

describe "indexing protocol definitions" do
test "works" do
{:ok, [protocol], doc} =
~q[
defprotocol Something do
def activate(thing, environment)
end
]
|> index()

assert protocol.type == :protocol
assert protocol.subtype == :definition
assert protocol.subject == Something

expected_block = ~q[
«defprotocol Something do
def activate(thing, environment)
end»
]t

assert decorate(doc, protocol.range) == "defprotocol «Something» do"
assert decorate(doc, protocol.block_range) == expected_block
end
end

describe "indexing protocol implementations" do
test "works" do
{:ok, [protocol], doc} =
~q[
defimpl Something, for: Atom do
def my_impl(atom, _opts) do
to_string(atom)
end
end
]
|> index()

assert protocol.type == :protocol_implementation
assert protocol.subtype == :definition
assert protocol.subject == Something

expected_block =
~q[
«defimpl Something, for: Atom do
def my_impl(atom, _opts) do
to_string(atom)
end
end»
]t
|> String.trim_trailing()

assert decorate(doc, protocol.range) == "defimpl «Something», for: Atom do"
assert decorate(doc, protocol.block_range) == expected_block
end
end
end
Loading