Skip to content

Commit

Permalink
Fix comments duplicating
Browse files Browse the repository at this point in the history
The comments were duplicating since we alter the AST only for one
module. Filter only to comments within the module.
  • Loading branch information
NJichev committed Apr 18, 2024
1 parent da52516 commit 27958f9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
4 changes: 4 additions & 0 deletions lib/next_ls/commands/alias.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ defmodule NextLS.Commands.Alias do
indent = EditHelpers.get_indent(text, range.start.line)
aliased = get_aliased(defm, modules)

comments = Enum.filter(comments, fn comment ->
comment.line > range.start.line && comment.line <= range.end.line
end)

to_algebra_opts = [comments: comments]
doc = Code.Formatter.to_algebra(aliased, to_algebra_opts)
formatted = doc |> Inspect.Algebra.format(@line_length) |> Enum.join()
Expand Down
33 changes: 18 additions & 15 deletions test/next_ls/commands/alias_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,12 @@ defmodule NextLS.Commands.AliasTest do
"""
defmodule MyApp do
# Comment
def to_list(map) do
# Also a comment
Foo.Bar.to_list(map)
defmodule Baz do
# Another comment
def to_list(map) do
# Also a comment
Foo.Bar.to_list(map)
end
end
end
""",
Expand All @@ -334,26 +337,26 @@ defmodule NextLS.Commands.AliasTest do

expected_edit =
String.trim("""
defmodule MyApp do
alias Foo.Bar
# Comment
def to_list(map) do
# Also a comment
Bar.to_list(map)
defmodule Baz do
alias Foo.Bar
# Another comment
def to_list(map) do
# Also a comment
Bar.to_list(map)
end
end
end
""")

position = %{"line" => 4, "character" => 6}
position = %{"line" => 6, "character" => 8}

assert %WorkspaceEdit{changes: %{^uri => [edit = %TextEdit{range: range}]}} =
Alias.run(%{uri: uri, text: text, position: position})

assert edit.new_text == expected_edit
assert range.start.line == 0
assert range.start.character == 0
assert range.end.line == 6
assert range.end.character == 3
assert range.start.line == 2
assert range.start.character == 2
assert range.end.line == 8
assert range.end.character == 5
end
end
end

0 comments on commit 27958f9

Please sign in to comment.