Skip to content

Commit

Permalink
Guard against new positions with line or char less than 1
Browse files Browse the repository at this point in the history
  • Loading branch information
zachallaun committed Jul 25, 2024
1 parent 6572a6a commit 80b5cf1
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apps/common/lib/lexical/ast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ defmodule Lexical.Ast do

defp one_line_range(%Document{} = document, line_number) do
start_pos = Position.new(document, line_number, 1)
end_pos = Position.new(document, line_number + 1, 0)
end_pos = Position.new(document, line_number + 1, 1)
Range.new(start_pos, end_pos)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule Lexical.RemoteControl.CodeAction.Handlers.ReplaceRemoteFunction do
defp apply_transform(%Document{} = doc, line_number, module, function, suggestion) do
{:ok, doc, analysis} = Document.Store.fetch(doc.uri, :analysis)
function_atom = String.to_atom(function)
position = Document.Position.new(doc, line_number, 0)
position = Document.Position.new(doc, line_number, 1)

doc
|> Ast.traverse_line(line_number, [], fn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule Lexical.RemoteControl.Search.Indexer.Source.Reducer do
blocks: [Block.root()],
entries: [],
extractors: extractors || @extractors,
position: {0, 0}
position: {1, 1}
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ defmodule Lexical.RemoteControl.CodeAction.Handlers.ReplaceRemoteFunctionTest do

range =
Document.Range.new(
Document.Position.new(document, line_number, 0),
Document.Position.new(document, line_number + 1, 0)
Document.Position.new(document, line_number, 1),
Document.Position.new(document, line_number + 1, 1)
)

diagnostic = Diagnostic.new(range, message, nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ defmodule Lexical.RemoteControl.CodeAction.Handlers.ReplaceWithUnderscoreTest do

range =
Document.Range.new(
Document.Position.new(document, line_number, 0),
Document.Position.new(document, line_number + 1, 0)
Document.Position.new(document, line_number, 1),
Document.Position.new(document, line_number + 1, 1)
)

diagnostic = Diagnostic.new(range, message, nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Callback do
start_char =
case String.split(line, "def", parts: 2) do
[i, _] -> String.length(i) + 1
[_] -> 0
[_] -> 1
end

end_char = String.length(line) + 1
Expand Down
4 changes: 2 additions & 2 deletions projects/lexical_shared/lib/lexical/document/position.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ defmodule Lexical.Document.Position do

@spec new(line_container(), line(), character()) :: t
def new(%Document{} = document, line, character)
when is_number(line) and is_number(character) do
when is_number(line) and is_number(character) and line >= 1 and character >= 1 do
new(document.lines, line, character)
end

def new(%Document.Lines{} = lines, line, character)
when is_number(line) and is_number(character) do
when is_number(line) and is_number(character) and line >= 1 and character >= 1 do
line_count = Document.Lines.size(lines)
starting_index = lines.starting_index

Expand Down

0 comments on commit 80b5cf1

Please sign in to comment.