Skip to content

Commit

Permalink
Revert "For module definitions, use the Indexer version instead of …
Browse files Browse the repository at this point in the history
…`ElixirSense` (#658)"

This reverts commit 1e00d8e.
  • Loading branch information
scohen committed Apr 13, 2024
1 parent e5f415c commit 03e890d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 182 deletions.
Original file line number Diff line number Diff line change
@@ -1,52 +1,17 @@
defmodule Lexical.RemoteControl.CodeIntelligence.Definition do
alias Future.Code, as: Code
alias Lexical.Ast
alias Lexical.Ast.Analysis
alias Lexical.Document
alias Lexical.Document.Location
alias Lexical.Document.Position
alias Lexical.Formats
alias Lexical.RemoteControl.CodeIntelligence.Entity
alias Lexical.RemoteControl.Search.Store
alias Lexical.Text

@spec definition(Document.t(), Position.t()) :: {:ok, [Location.t()]} | {:error, String.t()}
def definition(%Document{} = document, %Position{} = position) do
with {:ok, _, analysis} <- Document.Store.fetch(document.uri, :analysis),
{:ok, entity, _range} <- Entity.resolve(analysis, position) do
fetch_definition(entity, analysis, position)
end
end

defp fetch_definition({type, entity}, %Analysis{} = _analysis, %Position{} = _position)
when type in [:struct, :module] do
module = Formats.module(entity)

locations =
for entry <- Store.exact(module, type: type, subtype: :definition),
result = to_location(entry),
match?({:ok, _}, result) do
{:ok, location} = result
location
end

case locations do
[] ->
{:error, "No definition found for #{inspect(module)}"}

[location] ->
{:ok, location}

_ ->
{:ok, locations}
end
end

defp fetch_definition(_, %Analysis{} = analysis, %Position{} = position) do
analysis.document
document
|> Document.to_string()
|> ElixirSense.definition(position.line, position.character)
|> parse_location(analysis.document)
|> parse_location(document)
end

defp parse_location(%ElixirSense.Location{} = location, document) do
Expand Down Expand Up @@ -107,16 +72,4 @@ defmodule Lexical.RemoteControl.CodeIntelligence.Definition do
Entity.to_range(document, pos, pos)
end
end

defp to_location(entry) do
uri = Document.Path.ensure_uri(entry.path)

case Document.Store.open_temporary(uri) do
{:ok, document} ->
{:ok, Location.new(entry.range, document)}

_ ->
:error
end
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule MyDefinition do
@type result :: String.t()

defstruct [:field, another_field: nil]

defmacro __using__(_opts) do
quote do
import MyDefinition
Expand Down
Loading

0 comments on commit 03e890d

Please sign in to comment.