From 80b5cf1fd200b7e5284d1dae976a8c7707fd7b59 Mon Sep 17 00:00:00 2001 From: Zach Allaun Date: Thu, 25 Jul 2024 13:20:32 -0400 Subject: [PATCH] Guard against new positions with line or char less than 1 --- apps/common/lib/lexical/ast.ex | 2 +- .../code_action/handlers/replace_remote_function.ex | 2 +- .../lexical/remote_control/search/indexer/source/reducer.ex | 2 +- .../code_action/handlers/replace_remote_function_test.exs | 4 ++-- .../code_action/handlers/replace_with_underscore_test.exs | 4 ++-- .../code_intelligence/completion/translations/callback.ex | 2 +- projects/lexical_shared/lib/lexical/document/position.ex | 4 ++-- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/apps/common/lib/lexical/ast.ex b/apps/common/lib/lexical/ast.ex index 46f19d606..86a7c72c9 100644 --- a/apps/common/lib/lexical/ast.ex +++ b/apps/common/lib/lexical/ast.ex @@ -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 diff --git a/apps/remote_control/lib/lexical/remote_control/code_action/handlers/replace_remote_function.ex b/apps/remote_control/lib/lexical/remote_control/code_action/handlers/replace_remote_function.ex index d7062bc53..e055f269a 100644 --- a/apps/remote_control/lib/lexical/remote_control/code_action/handlers/replace_remote_function.ex +++ b/apps/remote_control/lib/lexical/remote_control/code_action/handlers/replace_remote_function.ex @@ -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 diff --git a/apps/remote_control/lib/lexical/remote_control/search/indexer/source/reducer.ex b/apps/remote_control/lib/lexical/remote_control/search/indexer/source/reducer.ex index 9880f0303..7914dc941 100644 --- a/apps/remote_control/lib/lexical/remote_control/search/indexer/source/reducer.ex +++ b/apps/remote_control/lib/lexical/remote_control/search/indexer/source/reducer.ex @@ -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 diff --git a/apps/remote_control/test/lexical/remote_control/code_action/handlers/replace_remote_function_test.exs b/apps/remote_control/test/lexical/remote_control/code_action/handlers/replace_remote_function_test.exs index 5e5b51cde..95f1afe45 100644 --- a/apps/remote_control/test/lexical/remote_control/code_action/handlers/replace_remote_function_test.exs +++ b/apps/remote_control/test/lexical/remote_control/code_action/handlers/replace_remote_function_test.exs @@ -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) diff --git a/apps/remote_control/test/lexical/remote_control/code_action/handlers/replace_with_underscore_test.exs b/apps/remote_control/test/lexical/remote_control/code_action/handlers/replace_with_underscore_test.exs index d19b38563..a76c58628 100644 --- a/apps/remote_control/test/lexical/remote_control/code_action/handlers/replace_with_underscore_test.exs +++ b/apps/remote_control/test/lexical/remote_control/code_action/handlers/replace_with_underscore_test.exs @@ -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) diff --git a/apps/server/lib/lexical/server/code_intelligence/completion/translations/callback.ex b/apps/server/lib/lexical/server/code_intelligence/completion/translations/callback.ex index 1c96ac192..6ebf066fc 100644 --- a/apps/server/lib/lexical/server/code_intelligence/completion/translations/callback.ex +++ b/apps/server/lib/lexical/server/code_intelligence/completion/translations/callback.ex @@ -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 diff --git a/projects/lexical_shared/lib/lexical/document/position.ex b/projects/lexical_shared/lib/lexical/document/position.ex index 86c9990ee..b41b4ef74 100644 --- a/projects/lexical_shared/lib/lexical/document/position.ex +++ b/projects/lexical_shared/lib/lexical/document/position.ex @@ -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