|
| 1 | +defmodule NextLS.Commands.ToPipe do |
| 2 | + @moduledoc false |
| 3 | + import Schematic |
| 4 | + |
| 5 | + alias GenLSP.Enumerations.ErrorCodes |
| 6 | + alias GenLSP.Structures.Position |
| 7 | + alias GenLSP.Structures.Range |
| 8 | + alias GenLSP.Structures.TextEdit |
| 9 | + alias GenLSP.Structures.WorkspaceEdit |
| 10 | + alias NextLS.EditHelpers |
| 11 | + alias Sourceror.Zipper, as: Z |
| 12 | + |
| 13 | + defp opts do |
| 14 | + map(%{ |
| 15 | + position: Position.schematic(), |
| 16 | + uri: str(), |
| 17 | + text: list(str()) |
| 18 | + }) |
| 19 | + end |
| 20 | + |
| 21 | + def run(opts) do |
| 22 | + with {:ok, %{text: text, uri: uri, position: position}} <- unify(opts(), Map.new(opts)), |
| 23 | + {:ok, ast} <- parse(text), |
| 24 | + {:ok, new_ast, original} <- get_node_and_new_node(ast, position) do |
| 25 | + range = make_range(original) |
| 26 | + indent = EditHelpers.get_indent(text, range.start.line) |
| 27 | + |
| 28 | + %WorkspaceEdit{ |
| 29 | + changes: %{ |
| 30 | + uri => [ |
| 31 | + %TextEdit{ |
| 32 | + new_text: |
| 33 | + EditHelpers.add_indent_to_edit( |
| 34 | + Macro.to_string(new_ast), |
| 35 | + indent |
| 36 | + ), |
| 37 | + range: range |
| 38 | + } |
| 39 | + ] |
| 40 | + } |
| 41 | + } |
| 42 | + else |
| 43 | + {:error, message} -> |
| 44 | + %GenLSP.ErrorResponse{code: ErrorCodes.parse_error(), message: inspect(message)} |
| 45 | + |
| 46 | + {:error, _ast, messages} -> |
| 47 | + error = |
| 48 | + Enum.map_join(messages, "\n", fn {ctx, message} -> |
| 49 | + message <> " on line: #{ctx[:line]}, column: #{ctx[:column]}" |
| 50 | + end) |
| 51 | + |
| 52 | + %GenLSP.ErrorResponse{code: ErrorCodes.parse_error(), message: error} |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + defp parse(lines) do |
| 57 | + lines |
| 58 | + |> Enum.join("\n") |
| 59 | + |> Spitfire.parse(column: 0, line: 0) |
| 60 | + |> case do |
| 61 | + {:errors, ast, _errors} -> {:ok, ast} |
| 62 | + other -> other |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + defp make_range(original_ast) do |
| 67 | + range = Sourceror.get_range(original_ast) |
| 68 | + |
| 69 | + %Range{ |
| 70 | + start: %Position{line: range.start[:line], character: range.start[:column]}, |
| 71 | + end: %Position{line: range.end[:line], character: range.end[:column]} |
| 72 | + } |
| 73 | + end |
| 74 | + |
| 75 | + def get_node_and_new_node(ast, pos) do |
| 76 | + pos = [line: pos.line, column: pos.character] |
| 77 | + |
| 78 | + result = |
| 79 | + ast |
| 80 | + |> Z.zip() |
| 81 | + |> Z.traverse(nil, fn tree, acc -> |
| 82 | + node = Z.node(tree) |
| 83 | + range = Sourceror.get_range(node) |
| 84 | + |
| 85 | + if not is_nil(range) and |
| 86 | + (match?({{:., _, _}, _, [_ | _]}, node) or |
| 87 | + match?({t, _, [_ | _]} when t not in [:., :__aliases__], node)) do |
| 88 | + if Sourceror.compare_positions(range.start, pos) == :lt && |
| 89 | + Sourceror.compare_positions(range.end, pos) == :gt do |
| 90 | + {tree, node} |
| 91 | + else |
| 92 | + {tree, acc} |
| 93 | + end |
| 94 | + else |
| 95 | + {tree, acc} |
| 96 | + end |
| 97 | + end) |
| 98 | + |
| 99 | + case result do |
| 100 | + {_, nil} -> |
| 101 | + {:error, "could not find an argument to extract at the cursor position"} |
| 102 | + |
| 103 | + {_, {_t, _m, []}} -> |
| 104 | + {:error, "could not find an argument to extract at the cursor position"} |
| 105 | + |
| 106 | + {_, {t, m, [argument | rest]} = node} -> |
| 107 | + piped = {:|>, [], [argument, {t, m, rest}]} |
| 108 | + {:ok, piped, node} |
| 109 | + end |
| 110 | + end |
| 111 | +end |
0 commit comments