Skip to content

Commit

Permalink
cursor and parsing improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsamson committed Sep 18, 2024
1 parent dd572f4 commit 38bd07d
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 24 deletions.
3 changes: 1 addition & 2 deletions apps/debug_adapter/lib/debug_adapter/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2925,8 +2925,7 @@ defmodule ElixirLS.DebugAdapter.Server do
if String.ends_with?(file, ".ex") or String.ends_with?(file, ".exs") do
code = File.read!(file)
buffer_file_metadata = ElixirSense.Core.Parser.parse_string(code, false, true, {line, 1})

env = ElixirSense.Core.Metadata.get_env(buffer_file_metadata, {line, 1})
env = Metadata.get_cursor_env(buffer_file_metadata, {line, 1})

{buffer_file_metadata, env, ElixirSense.Core.State.Env.to_macro_env(env, file, line)}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule ElixirLS.LanguageServer.Providers.CodeAction.ReplaceRemoteFunction do
alias ElixirLS.LanguageServer.Providers.CodeMod.Text
alias ElixirLS.LanguageServer.SourceFile
alias ElixirSense.Core.Parser
alias ElixirSense.Core.Metadata

import ElixirLS.LanguageServer.Providers.CodeAction.Helpers

Expand Down Expand Up @@ -180,12 +181,9 @@ defmodule ElixirLS.LanguageServer.Providers.CodeAction.ReplaceRemoteFunction do
defp aliases_at(source_file, line_number) do
one_based_line = line_number + 1

metadata = Parser.parse_string(source_file.text, true, true, {one_based_line, 1})

case metadata.lines_to_env[one_based_line] do
%ElixirSense.Core.State.Env{aliases: aliases} -> {:ok, aliases}
_ -> :error
end
metadata = Parser.parse_string(source_file.text, true, false, {one_based_line, 1})
env = Metadata.get_cursor_env(metadata, {one_based_line, 1})
{:ok, env.aliases}
end

defp module_to_alias(module) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion.Suggestion do

metadata =
Keyword.get_lazy(options, :metadata, fn ->
Parser.parse_string(code, true, true, {line, column})
Parser.parse_string(code, true, false, {line, column})
end)

{text_before, text_after} = Source.split_at(code, line, column)
Expand Down Expand Up @@ -192,17 +192,18 @@ defmodule ElixirLS.LanguageServer.Providers.Completion.Suggestion do

# TODO this may no longer be needed
# only fix_incomplete_call has some tests depending on it
# on 1.17 no tests depend on those hacks
fixers = [
fix_incomplete_call,
fix_incomplete_kw,
fix_incomplete_kw_key
# fix_incomplete_call,
# fix_incomplete_kw,
# fix_incomplete_kw_key
]

Enum.reduce_while(fixers, nil, fn fun, _ ->
Enum.reduce_while(fixers, metadata, fn fun, metadata ->
new_buffer = fun.(text_before, text_after)

with true <- new_buffer != nil,
meta <- Parser.parse_string(new_buffer, false, true, {line, column}),
meta <- Parser.parse_string(new_buffer, false, false, {line, column}),
%Metadata{error: error} <- meta,
true <- error == nil do
{:halt, meta}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule ElixirLS.LanguageServer.Providers.Definition.Locator do
context ->
metadata =
Keyword.get_lazy(options, :metadata, fn ->
Parser.parse_string(code, true, true, {line, column})
Parser.parse_string(code, true, false, {line, column})
end)

env = Metadata.get_cursor_env(metadata, {line, column}, {context.begin, context.end})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ defmodule ElixirLS.LanguageServer.Providers.ExecuteCommand.ExpandMacro do
end

def expand_full(buffer, code, file, line) do
buffer_file_metadata = Parser.parse_string(buffer, true, true, {line, 1})

env = Metadata.get_env(buffer_file_metadata, {line, 1})
buffer_file_metadata = Parser.parse_string(buffer, true, false, {line, 1})
env = Metadata.get_cursor_env(buffer_file_metadata, {line, 1})

do_expand_full(code, env, file, line)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ defmodule ElixirLS.LanguageServer.Providers.Hover.Docs do
%{begin: begin_pos, end: end_pos} = context ->
metadata =
Keyword.get_lazy(options, :metadata, fn ->
Parser.parse_string(code, true, true, {line, column})
Parser.parse_string(code, true, false, {line, column})
end)

env =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ defmodule ElixirLS.LanguageServer.Providers.Implementation.Locator do
context ->
metadata =
Keyword.get_lazy(options, :metadata, fn ->
Parser.parse_string(code, true, true, {line, column})
Parser.parse_string(code, true, false, {line, column})
end)

env = Metadata.get_env(metadata, {line, column})
env = Metadata.get_cursor_env(metadata, {line, column}, {context.begin, context.end})

find(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule ElixirLS.LanguageServer.Providers.References.Locator do
context ->
metadata =
Keyword.get_lazy(options, :metadata, fn ->
Parser.parse_string(code, true, true, {line, column})
Parser.parse_string(code, true, false, {line, column})
end)

# if context is var try to find env by scope_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ defmodule ElixirLS.LanguageServer.Providers.SignatureHelp.Signature do

metadata =
Keyword.get_lazy(options, :metadata, fn ->
Parser.parse_string(code, true, true, {line, column})
Parser.parse_string(code, true, false, {line, column})
end)

env = Metadata.get_env(metadata, {line, column})
env = Metadata.get_cursor_env(metadata, {line, column})

find(prefix, {line, column}, env, metadata)
end
Expand Down

0 comments on commit 38bd07d

Please sign in to comment.