Skip to content

Commit

Permalink
fix some issues and typos
Browse files Browse the repository at this point in the history
  • Loading branch information
biletskyy authored and mhanberg committed Oct 2, 2023
1 parent 68562cc commit ba534a4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 53 deletions.
4 changes: 2 additions & 2 deletions lib/next_ls/helpers/ast_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ defmodule NextLS.ASTHelpers do
{ast, acc}
end

# search symbols in a left side of forward arrow clause adn increase scope
# search symbols in a left side of forward arrow clause and increase scope
defp prewalk({:->, meta, [left, _right]} = ast, acc) do
acc = increase_scope_nesting(acc, meta[:line])
acc = find_symbols(left, acc)
Expand Down Expand Up @@ -315,7 +315,7 @@ defmodule NextLS.ASTHelpers do
defp calculate_range(name, line, column) do
length = name |> to_string() |> String.length()

{line..line, column..(column + length)}
{line..line, column..(column + length - 1)}
end

defp position_in_range?({position_line, position_column}, {range_lines, range_columns}) do
Expand Down
2 changes: 1 addition & 1 deletion test/next_ls/definition_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ defmodule NextLS.DefinitionTest do
},
"end" => %{
"line" => 5,
"character" => 13
"character" => 12
}
},
"uri" => ^uri
Expand Down
94 changes: 46 additions & 48 deletions test/next_ls/helpers/ast_helpers_variables_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ defmodule NextLS.ASTHelpersVariablesTest do
describe("get_variable_definition/2") do
test "symbol defined in a match is found", %{source: source} do
symbol = Variables.get_variable_definition(source, {7, 25})
assert symbol == {:charlie, {5..5, 5..12}}
assert symbol == {:charlie, {5..5, 5..11}}
end

test "returns nil when position is not a variable reference", %{source: source} do
Expand All @@ -167,172 +167,170 @@ defmodule NextLS.ASTHelpersVariablesTest do
test "references that defined by same symbol as target reference", %{source: source} do
refs = Variables.list_variable_references(source, {6, 17})
assert length(refs) == 2
assert exists_in?(refs, {:charlie, {6..6, 17..24}})
assert exists_in?(refs, {:charlie, {7..7, 25..32}})
assert {:charlie, {6..6, 17..23}} in refs
assert {:charlie, {7..7, 25..31}} in refs
end

test "symbol set in a match and corrctly processing ^", %{source: source} do
refs = Variables.list_variable_references(source, {5, 5})
assert length(refs) == 2
assert exists_in?(refs, {:charlie, {6..6, 17..24}})
assert exists_in?(refs, {:charlie, {7..7, 25..32}})
assert {:charlie, {6..6, 17..23}} in refs
assert {:charlie, {7..7, 25..31}} in refs
end

test "symbol set in a function arguments", %{source: source} do
refs = Variables.list_variable_references(source, {4, 30})
assert length(refs) == 2
assert exists_in?(refs, {:alpha, {6..6, 42..47}})
assert exists_in?(refs, {:alpha, {7..7, 11..16}})
assert {:alpha, {6..6, 42..46}} in refs
assert {:alpha, {7..7, 11..15}} in refs
end

test "symbol set in a function arguments and referenced in 'when' clause", %{source: source} do
refs = Variables.list_variable_references(source, {4, 21})
assert length(refs) == 2
assert exists_in?(refs, {:bravo, {4..4, 49..54}})
assert exists_in?(refs, {:bravo, {7..7, 18..23}})
assert {:bravo, {4..4, 49..53}} in refs
assert {:bravo, {7..7, 18..22}} in refs
end

test "symbol set in a mattern match", %{source: source} do
refs = Variables.list_variable_references(source, {6, 33})
assert length(refs) == 1
assert exists_in?(refs, {:delta, {7..7, 34..39}})
assert {:delta, {7..7, 34..38}} in refs
end

test "references shadowed by 'if/else' blocks", %{source: source} do
refs = Variables.list_variable_references(source, {11, 5})
assert length(refs) == 2
assert exists_in?(refs, {:alpha, {14..14, 10..15}})
assert exists_in?(refs, {:alpha, {24..24, 11..16}})
assert {:alpha, {14..14, 10..14}} in refs
assert {:alpha, {24..24, 11..15}} in refs
end

test "symbol set in 'if' block", %{source: source} do
refs = Variables.list_variable_references(source, {15, 9})
assert length(refs) == 1
assert exists_in?(refs, {:alpha, {17..17, 20..25}})
assert {:alpha, {17..17, 20..24}} in refs
end

test "symbol set in match with 'if' containing it's shadow", %{source: source} do
refs = Variables.list_variable_references(source, {13, 5})
assert length(refs) == 1
assert exists_in?(refs, {:bravo, {24..24, 18..23}})
assert {:bravo, {24..24, 18..22}} in refs
end

test "symbol set in 'case' clause", %{source: source} do
refs = Variables.list_variable_references(source, {33, 13})
assert length(refs) == 2
assert exists_in?(refs, {:bravo, {33..33, 32..37}})
assert exists_in?(refs, {:bravo, {34..34, 19..24}})
assert {:bravo, {33..33, 32..36}} in refs
assert {:bravo, {34..34, 19..23}} in refs
end

test "symbol referenced in 'cond' clause", %{source: source} do
refs = Variables.list_variable_references(source, {46, 5})
assert length(refs) == 2
assert exists_in?(refs, {:alpha, {51..51, 9..14}})
assert exists_in?(refs, {:alpha, {58..58, 16..21}})
assert {:alpha, {51..51, 9..13}} in refs
assert {:alpha, {58..58, 16..20}} in refs
end

test "symbol shadowed in 'for' and 'with'", %{source: source} do
refs = Variables.list_variable_references(source, {62, 12})
assert length(refs) == 4
assert exists_in?(refs, {:alpha, {65..65, 18..23}})
assert exists_in?(refs, {:alpha, {69..69, 18..23}})
assert exists_in?(refs, {:alpha, {73..73, 26..31}})
assert exists_in?(refs, {:alpha, {81..81, 18..23}})
assert {:alpha, {65..65, 18..22}} in refs
assert {:alpha, {69..69, 18..22}} in refs
assert {:alpha, {73..73, 26..30}} in refs
assert {:alpha, {81..81, 18..22}} in refs

refs2 = Variables.list_variable_references(source, {63, 5})
assert length(refs2) == 1
assert exists_in?(refs2, {:bravo, {81..81, 11..16}})
assert {:bravo, {81..81, 11..15}} in refs2
end

test "symbol defined in 'for'", %{source: source} do
refs = Variables.list_variable_references(source, {65, 9})
assert length(refs) == 1
assert exists_in?(refs, {:alpha, {66..66, 18..23}})
assert {:alpha, {66..66, 18..22}} in refs

refs2 = Variables.list_variable_references(source, {69, 9})
assert length(refs2) == 1
assert exists_in?(refs2, {:bravo, {69..69, 47..52}})
assert {:bravo, {69..69, 47..51}} in refs2

refs3 = Variables.list_variable_references(source, {69, 25})
assert length(refs3) == 2
assert exists_in?(refs3, {:charlie, {69..69, 55..62}})
assert exists_in?(refs3, {:charlie, {70..70, 18..25}})
assert {:charlie, {69..69, 55..61}} in refs3
assert {:charlie, {70..70, 18..24}} in refs3
end

test "symbol defined in 'with'", %{source: source} do
refs = Variables.list_variable_references(source, {73, 16})
assert length(refs) == 1
assert exists_in?(refs, {:delta, {74..74, 27..32}})
assert {:delta, {74..74, 27..31}} in refs

refs2 = Variables.list_variable_references(source, {74, 11})
assert length(refs2) == 1
assert exists_in?(refs2, {:delta, {75..75, 18..23}})
assert {:delta, {75..75, 18..22}} in refs2

refs3 = Variables.list_variable_references(source, {78, 7})
assert length(refs3) == 1
assert exists_in?(refs3, {:error, {78..78, 25..30}})
assert {:error, {78..78, 25..29}} in refs3
end

test "symbol shadowed by anonymus funciton", %{source: source} do
test "symbol shadowed by anonymous funciton", %{source: source} do
refs = Variables.list_variable_references(source, {84, 12})
assert length(refs) == 2
assert exists_in?(refs, {:alpha, {96..96, 7..12}})
assert exists_in?(refs, {:alpha, {100..100, 21..26}})
assert {:alpha, {96..96, 7..11}} in refs
assert {:alpha, {100..100, 21..25}} in refs
end

test "symbol defined in anonymus funciton", %{source: source} do
test "symbol defined in anonymous funciton", %{source: source} do
refs = Variables.list_variable_references(source, {86, 7})
assert length(refs) == 1
assert exists_in?(refs, {:charlie, {88..88, 15..22}})
assert {:charlie, {88..88, 15..21}} in refs

refs2 = Variables.list_variable_references(source, {86, 22})
assert length(refs2) == 1
assert exists_in?(refs2, {:delta, {88..88, 24..29}})
assert {:delta, {88..88, 24..28}} in refs2

refs3 = Variables.list_variable_references(source, {86, 31})
assert length(refs3) == 1
assert exists_in?(refs3, {:alpha, {87..87, 20..25}})
assert {:alpha, {87..87, 20..24}} in refs3

refs4 = Variables.list_variable_references(source, {97, 22})
assert length(refs4) == 1
assert exists_in?(refs4, {:alpha, {97..97, 37..42}})
assert {:alpha, {97..97, 37..41}} in refs4

refs5 = Variables.list_variable_references(source, {98, 25})
assert length(refs5) == 1
assert exists_in?(refs5, {:alpha, {98..98, 51..56}})
assert {:alpha, {98..98, 51..55}} in refs5
end

test "symbols with 'receive' macro", %{source: source} do
refs = Variables.list_variable_references(source, {103, 12})
assert length(refs) == 2
assert exists_in?(refs, {:alpha, {110..110, 10..15}})
assert exists_in?(refs, {:alpha, {113..113, 17..22}})
assert {:alpha, {110..110, 10..14}} in refs
assert {:alpha, {113..113, 17..21}} in refs

refs2 = Variables.list_variable_references(source, {105, 19})
assert length(refs2) == 1
assert exists_in?(refs2, {:bravo, {107..107, 17..22}})
assert {:bravo, {107..107, 17..21}} in refs2
end

test "symbols set with 'destructure'", %{source: source} do
refs = Variables.list_variable_references(source, {117, 18})
assert length(refs) == 1
assert exists_in?(refs, {:alpha, {118..118, 16..21}})
assert {:alpha, {118..118, 16..20}} in refs
end

test "symbols set in macro", %{source: source} do
refs = Variables.list_variable_references(source, {121, 37})
assert length(refs) == 1
assert exists_in?(refs, {:variables, {122..122, 14..23}})
assert {:variables, {122..122, 14..22}} in refs

refs2 = Variables.list_variable_references(source, {124, 7})
assert length(refs2) == 1
assert exists_in?(refs2, {:length, {127..127, 32..38}})
assert {:length, {127..127, 32..37}} in refs2

refs3 = Variables.list_variable_references(source, {123, 7})
assert length(refs3) == 1
assert exists_in?(refs3, {:var, {127..127, 17..20}})
assert {:var, {127..127, 17..19}} in refs3
end
end

defp exists_in?(results, var), do: Enum.find(results, &(&1 == var))
end
4 changes: 2 additions & 2 deletions test/next_ls/references_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,14 @@ defmodule NextLS.ReferencesTest do
"uri" => uri,
"range" => %{
"start" => %{"line" => 17, "character" => 14},
"end" => %{"line" => 17, "character" => 19}
"end" => %{"line" => 17, "character" => 18}
}
},
%{
"uri" => uri,
"range" => %{
"start" => %{"line" => 18, "character" => 12},
"end" => %{"line" => 18, "character" => 17}
"end" => %{"line" => 18, "character" => 16}
}
}
]
Expand Down

0 comments on commit ba534a4

Please sign in to comment.