Skip to content

Commit

Permalink
Expose error tolerant parser through public api
Browse files Browse the repository at this point in the history
Closes #87
  • Loading branch information
lukaszsamson committed Apr 19, 2020
1 parent 871571a commit 0c8a97c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
21 changes: 21 additions & 0 deletions lib/elixir_sense.ex
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,25 @@ defmodule ElixirSense do
[]
end
end

@doc ~S"""
Provides an error tolerant parser
## Example
iex> code = ~S'''
...> defmodule do
...> end
...> '''
iex> ElixirSense.string_to_quoted(code, 1)
{:ok, {:defmodule, [line: 1, column: 1], [[do: {:__block__, [], []}]]}}
"""
@spec string_to_quoted(String.t(), pos_integer | nil, non_neg_integer) ::
{:ok, Macro.t()} | {:error, {line :: pos_integer(), term(), term()}}
def string_to_quoted(source, cursor_line_number \\ nil, error_threshold \\ 6) do
case Parser.string_to_ast(source, error_threshold, cursor_line_number) do
{:ok, ast, _source} -> {:ok, ast}
other -> other
end
end
end
2 changes: 1 addition & 1 deletion lib/elixir_sense/core/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ defmodule ElixirSense.Core.Parser do
}
end

defp string_to_ast(source, errors_threshold, cursor_line_number, original_error \\ nil) do
def string_to_ast(source, errors_threshold, cursor_line_number, original_error \\ nil) do
case Code.string_to_quoted(source, columns: true) do
{:ok, ast} ->
{:ok, ast, source}
Expand Down
4 changes: 4 additions & 0 deletions lib/elixir_sense/server/request_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ defmodule ElixirSense.Server.RequestHandler do
ElixirSense.suggestions(buffer, line, column)
end

def handle_request("string_to_quoted", %{"buffer" => buffer} = args) do
ElixirSense.suggestions(buffer, args["cursor_line"], args["error_threshold"] || 6)
end

def handle_request("expand_full", %{
"buffer" => buffer,
"selected_code" => selected_code,
Expand Down

0 comments on commit 0c8a97c

Please sign in to comment.