Skip to content

Commit

Permalink
feat(commands): from-pipe (#378)
Browse files Browse the repository at this point in the history
Co-authored-by: Nikola Jichev <[email protected]>
  • Loading branch information
mhanberg and NJichev committed Feb 26, 2024
1 parent 9195694 commit 774e7cb
Show file tree
Hide file tree
Showing 8 changed files with 769 additions and 439 deletions.
18 changes: 16 additions & 2 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ defmodule NextLS do
document_formatting_provider: true,
execute_command_provider: %GenLSP.Structures.ExecuteCommandOptions{
commands: [
"to-pipe"
"to-pipe",
"from-pipe"
]
},
hover_provider: true,
Expand Down Expand Up @@ -618,14 +619,27 @@ defmodule NextLS do
) do
reply =
case command do
"from-pipe" ->
[arguments] = params.arguments

uri = arguments["uri"]
position = arguments["position"]
text = lsp.assigns.documents[uri]

NextLS.Commands.Pipe.from(%{
uri: uri,
text: text,
position: position
})

"to-pipe" ->
[arguments] = params.arguments

uri = arguments["uri"]
position = arguments["position"]
text = lsp.assigns.documents[uri]

NextLS.Commands.ToPipe.run(%{
NextLS.Commands.Pipe.to(%{
uri: uri,
text: text,
position: position
Expand Down
63 changes: 61 additions & 2 deletions lib/next_ls/commands/to_pipe.ex → lib/next_ls/commands/pipe.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule NextLS.Commands.ToPipe do
defmodule NextLS.Commands.Pipe do
@moduledoc false
import Schematic

Expand All @@ -18,7 +18,7 @@ defmodule NextLS.Commands.ToPipe do
})
end

def run(opts) do
def to(opts) do
with {:ok, %{text: text, uri: uri, position: position}} <- unify(opts(), Map.new(opts)),
{:ok, ast} = parse(text),
{:ok, {t, m, [argument | rest]} = original} <- get_node(ast, position) do
Expand Down Expand Up @@ -46,6 +46,34 @@ defmodule NextLS.Commands.ToPipe do
end
end

def from(opts) do
with {:ok, %{text: text, uri: uri, position: position}} <- unify(opts(), Map.new(opts)),
{:ok, ast} = parse(text),
{:ok, {:|>, _m, [left, {right, _, args}]} = original} <- get_pipe_node(ast, position) do
range = make_range(original)
indent = EditHelpers.get_indent(text, range.start.line)
unpiped = {right, [], [left | args]}

%WorkspaceEdit{
changes: %{
uri => [
%TextEdit{
new_text:
EditHelpers.add_indent_to_edit(
Macro.to_string(unpiped),
indent
),
range: range
}
]
}
}
else
{:error, message} ->
%GenLSP.ErrorResponse{code: ErrorCodes.parse_error(), message: inspect(message)}
end
end

defp parse(lines) do
lines
|> Enum.join("\n")
Expand Down Expand Up @@ -109,4 +137,35 @@ defmodule NextLS.Commands.ToPipe do
{:ok, node}
end
end

def get_pipe_node(ast, pos) do
pos = [line: pos.line + 1, column: pos.character + 1]

result =
ast
|> Z.zip()
|> Z.traverse(nil, fn tree, acc ->
node = Z.node(tree)
range = Sourceror.get_range(node)

if not is_nil(range) and match?({:|>, _, _}, node) do
if Sourceror.compare_positions(range.start, pos) == :lt &&
Sourceror.compare_positions(range.end, pos) == :gt do
{tree, node}
else
{tree, acc}
end
else
{tree, acc}
end
end)

case result do
{_, nil} ->
{:error, "could not find a pipe operator at the cursor position"}

{_, {_t, _m, [_argument | _rest]} = node} ->
{:ok, node}
end
end
end
4 changes: 1 addition & 3 deletions lib/next_ls/document_symbol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ defmodule NextLS.DocumentSymbol do
ast
end

ast
|> walker(nil)
|> List.wrap()
List.wrap(walker(ast, nil))
end

defp walker([{{:__literal__, _, [:do]}, {_, _, _exprs} = ast}], mod) do
Expand Down
2 changes: 2 additions & 0 deletions lib/next_ls/runtime/sidecar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule NextLS.Runtime.Sidecar do
end

def handle_info({:tracer, :dbg, term}, state) do
# credo:disable-for-next-line
dbg(term)

{:noreply, state}
Expand Down Expand Up @@ -82,6 +83,7 @@ defmodule NextLS.Runtime.Sidecar do
end

def handle_info({{:tracer, :dbg}, payload}, state) do
# credo:disable-for-next-line
dbg(payload)
{:noreply, state}
end
Expand Down
Loading

0 comments on commit 774e7cb

Please sign in to comment.