Skip to content

Commit

Permalink
Fixed ranges for explicit aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
scohen committed Apr 24, 2024
1 parent 64e88c2 commit 08535dd
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
11 changes: 7 additions & 4 deletions apps/common/lib/lexical/ast/analysis/alias.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ defmodule Lexical.Ast.Analysis.Alias do
range_for_implicit_alias(document, ast)
end

defp range_for_ast(document, ast, alias, as) do
if List.last(alias) == as do
defp range_for_ast(document, ast, alias, _as) do
if List.last(alias) == alias do
range_for_implicit_alias(document, ast)
else
range_for_explicit_alias(document, ast)
Expand All @@ -36,8 +36,11 @@ defmodule Lexical.Ast.Analysis.Alias do

defp range_for_explicit_alias(%Document{} = document, ast) do
case Ast.Range.fetch(ast, document) do
{:ok, range} -> range
_ -> nil
{:ok, %Range{end: end_pos} = range} ->
%Range{range | end: %Position{end_pos | character: end_pos.character}}

_ ->
nil
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Lexical.RemoteControl.Analyzer.AliasesTest do

import Lexical.Test.CursorSupport
import Lexical.Test.CodeSigil
import Lexical.Test.RangeSupport

use ExUnit.Case

Expand All @@ -15,6 +16,19 @@ defmodule Lexical.RemoteControl.Analyzer.AliasesTest do
|> Analyzer.aliases_at(position)
end

defp scope_aliases(text) do
{position, document} = pop_cursor(text, as: :document)

aliases =
document
|> Ast.analyze()
|> Ast.Analysis.scopes_at(position)
|> Enum.flat_map(& &1.aliases)
|> Map.new(&{&1.as, &1})

{aliases, document}
end

describe "top level aliases" do
test "a useless alias" do
aliases =
Expand Down Expand Up @@ -247,6 +261,56 @@ defmodule Lexical.RemoteControl.Analyzer.AliasesTest do
end
end

describe "alias ranges" do
test "for a simple alias" do
{aliases, doc} =
~q[
defmodule Parent do
alias Foo.Bar.Baz|
end
]
|> scope_aliases()

assert decorate(doc, aliases[:Baz].range) =~ " «alias Foo.Bar.Baz»"
end

test "for a multiple alias on one line" do
{aliases, doc} =
~q[
defmodule Parent do
alias Foo.Bar.{Baz, Quux}|
end
]
|> scope_aliases()

assert decorate(doc, aliases[:Baz].range) =~ " «alias Foo.Bar.{Baz, Quux}»"
assert decorate(doc, aliases[:Quux].range) =~ " «alias Foo.Bar.{Baz, Quux}»"
end

test "for a multiple alias on multiple lines" do
{aliases, doc} =
~q[
defmodule Parent do
alias Foo.Bar.{
Baz,
Quux,
Other
}|
end
]
|> scope_aliases()

assert decorate(doc, aliases[:Baz].range) =~
" «alias Foo.Bar.{\n Baz,\n Quux,\n Other\n}»"

assert decorate(doc, aliases[:Quux].range) =~
" «alias Foo.Bar.{\n Baz,\n Quux,\n Other\n}»"

assert decorate(doc, aliases[:Other].range) =~
" «alias Foo.Bar.{\n Baz,\n Quux,\n Other\n}»"
end
end

describe "nested modules" do
test "no aliases are defined for modules with dots" do
aliases =
Expand Down

0 comments on commit 08535dd

Please sign in to comment.