Skip to content

Commit

Permalink
Elixir 1.16 support (#283)
Browse files Browse the repository at this point in the history
* fix warnings

* get line in predictable way

* position is :erl.anno since 1.16

* alias external submodule behaviour changed in 1.16

see elixir-lang/elixir#12456

* fix parser tests

this is a WIP solution atm

* handle anonymous_call in complete

* fix version check and another test

* remove not needed test

* pass cursor line to parser

fall back to container_cursor_to_quoted if unable to parse

* fix crash in parser

* fix warning

* improve parser

* return correct error on parse success via container_cursor_to_quoted

* do not fall back to container_cursor_to_quoted when fixing no env

* format on 1.15

* fix some dialyzer errors
  • Loading branch information
lukaszsamson authored Nov 27, 2023
1 parent 16d28f7 commit d61c3a0
Show file tree
Hide file tree
Showing 19 changed files with 361 additions and 184 deletions.
54 changes: 39 additions & 15 deletions lib/elixir_sense.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ defmodule ElixirSense do
nil

%{begin: begin_pos, end: end_pos} = context ->
metadata = Parser.parse_string(code, true, true, line)
metadata = Parser.parse_string(code, true, true, {line, column})

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

Expand Down Expand Up @@ -106,7 +106,7 @@ defmodule ElixirSense do
nil

context ->
buffer_file_metadata = Parser.parse_string(code, true, true, line)
buffer_file_metadata = Parser.parse_string(code, true, true, {line, column})

env =
Metadata.get_env(buffer_file_metadata, {line, column})
Expand Down Expand Up @@ -139,7 +139,7 @@ defmodule ElixirSense do
[]

context ->
buffer_file_metadata = Parser.parse_string(code, true, true, line)
buffer_file_metadata = Parser.parse_string(code, true, true, {line, column})

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

Expand Down Expand Up @@ -205,11 +205,16 @@ defmodule ElixirSense do
@spec suggestions(String.t(), pos_integer, pos_integer, keyword()) :: [Suggestion.suggestion()]
def suggestions(buffer, line, column, opts \\ []) do
hint = Source.prefix(buffer, line, column)
buffer_file_metadata = Parser.parse_string(buffer, true, true, line)
buffer_file_metadata = Parser.parse_string(buffer, true, true, {line, column})
{text_before, text_after} = Source.split_at(buffer, line, column)

buffer_file_metadata =
maybe_fix_autocomple_on_cursor(buffer_file_metadata, text_before, text_after, line)
maybe_fix_autocomple_on_cursor(
buffer_file_metadata,
text_before,
text_after,
{line, column}
)

env =
Metadata.get_env(buffer_file_metadata, {line, column})
Expand Down Expand Up @@ -278,7 +283,7 @@ defmodule ElixirSense do
@spec signature(String.t(), pos_integer, pos_integer) :: Signature.signature_info() | :none
def signature(code, line, column) do
prefix = Source.text_before(code, line, column)
buffer_file_metadata = Parser.parse_string(code, true, true, line)
buffer_file_metadata = Parser.parse_string(code, true, true, {line, column})

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

Expand All @@ -289,7 +294,7 @@ defmodule ElixirSense do
Returns a map containing the results of all different code expansion methods
available.
Available axpansion methods:
Available expansion methods:
* `expand_once` - Calls `Macro.expand_once/2`
* `expand` - Calls `Macro.expand/2`
Expand Down Expand Up @@ -361,7 +366,7 @@ defmodule ElixirSense do
"""
@spec expand_full(String.t(), String.t(), pos_integer) :: Expand.expanded_code_map()
def expand_full(buffer, code, line) do
buffer_file_metadata = Parser.parse_string(buffer, true, true, line)
buffer_file_metadata = Parser.parse_string(buffer, true, true, {line, 1})

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

Expand Down Expand Up @@ -438,7 +443,7 @@ defmodule ElixirSense do
%{
begin: {begin_line, begin_col}
} = context ->
buffer_file_metadata = Parser.parse_string(code, true, true, line)
buffer_file_metadata = Parser.parse_string(code, true, true, {line, column})

env =
%State.Env{
Expand Down Expand Up @@ -500,10 +505,29 @@ defmodule ElixirSense do
iex> ElixirSense.string_to_quoted(code, 1)
{:ok, {:defmodule, [do: [line: 1, column: 11], end: [line: 2, column: 1], line: 1, column: 1], [[do: {:__block__, [], []}]]}}
"""
@spec string_to_quoted(String.t(), pos_integer | nil, non_neg_integer, keyword) ::
{:ok, Macro.t()} | {:error, {line :: pos_integer(), term(), term()}}
def string_to_quoted(source, cursor_line_number \\ nil, error_threshold \\ 6, opts \\ []) do
case Parser.string_to_ast(source, error_threshold, cursor_line_number, nil, opts) do
@spec string_to_quoted(
String.t(),
{pos_integer, pos_integer} | nil,
non_neg_integer,
boolean,
keyword
) ::
{:ok, Macro.t()} | {:error, :parse_error}
def string_to_quoted(
source,
cursor_position \\ nil,
error_threshold \\ 6,
fallback_to_container_cursor_to_quoted \\ true,
parser_options \\ []
) do
string_to_ast_options = [
errors_threshold: error_threshold,
cursor_position: cursor_position,
fallback_to_container_cursor_to_quoted: fallback_to_container_cursor_to_quoted,
parser_options: parser_options
]

case Parser.string_to_ast(source, string_to_ast_options) do
{:ok, ast, _source, _error} -> {:ok, ast}
other -> other
end
Expand All @@ -519,7 +543,7 @@ defmodule ElixirSense do
meta
end

defp maybe_fix_autocomple_on_cursor(metadata, text_before, text_after, line) do
defp maybe_fix_autocomple_on_cursor(metadata, text_before, text_after, {line, column}) do
# Fix incomplete call, e.g. cursor after `var.`
fix_incomplete_call = fn text_before, text_after ->
if String.ends_with?(text_before, ".") do
Expand Down Expand Up @@ -553,7 +577,7 @@ defmodule ElixirSense do
new_buffer = fun.(text_before, text_after)

with true <- new_buffer != nil,
meta <- Parser.parse_string(new_buffer, false, true, line),
meta <- Parser.parse_string(new_buffer, false, true, {line, column}),
%Metadata{error: error} <- meta,
true <- error == nil do
{:halt, meta}
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir_sense/core/metadata.ex
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ defmodule ElixirSense.Core.Metadata do
Enum.map(params, fn param ->
param
|> Macro.to_string()
|> String.slice(1..-2)
|> String.slice(1..-2//1)
end)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/elixir_sense/core/normalized/path.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule ElixirSense.Core.Normalized.Path do
absname(path, &File.cwd!/0)
end

@spec absname(t, t) :: binary
@spec absname(t, t | (-> t)) :: binary
def absname(path, relative_to) do
path = IO.chardata_to_string(path)

Expand Down
Loading

0 comments on commit d61c3a0

Please sign in to comment.