Skip to content

Commit 774e7cb

Browse files
mhanbergNJichev
andauthored
feat(commands): from-pipe (#378)
Co-authored-by: Nikola Jichev <[email protected]>
1 parent 9195694 commit 774e7cb

File tree

8 files changed

+769
-439
lines changed

8 files changed

+769
-439
lines changed

Diff for: lib/next_ls.ex

+16-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ defmodule NextLS do
142142
document_formatting_provider: true,
143143
execute_command_provider: %GenLSP.Structures.ExecuteCommandOptions{
144144
commands: [
145-
"to-pipe"
145+
"to-pipe",
146+
"from-pipe"
146147
]
147148
},
148149
hover_provider: true,
@@ -618,14 +619,27 @@ defmodule NextLS do
618619
) do
619620
reply =
620621
case command do
622+
"from-pipe" ->
623+
[arguments] = params.arguments
624+
625+
uri = arguments["uri"]
626+
position = arguments["position"]
627+
text = lsp.assigns.documents[uri]
628+
629+
NextLS.Commands.Pipe.from(%{
630+
uri: uri,
631+
text: text,
632+
position: position
633+
})
634+
621635
"to-pipe" ->
622636
[arguments] = params.arguments
623637

624638
uri = arguments["uri"]
625639
position = arguments["position"]
626640
text = lsp.assigns.documents[uri]
627641

628-
NextLS.Commands.ToPipe.run(%{
642+
NextLS.Commands.Pipe.to(%{
629643
uri: uri,
630644
text: text,
631645
position: position

Diff for: lib/next_ls/commands/to_pipe.ex renamed to lib/next_ls/commands/pipe.ex

+61-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule NextLS.Commands.ToPipe do
1+
defmodule NextLS.Commands.Pipe do
22
@moduledoc false
33
import Schematic
44

@@ -18,7 +18,7 @@ defmodule NextLS.Commands.ToPipe do
1818
})
1919
end
2020

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

49+
def from(opts) do
50+
with {:ok, %{text: text, uri: uri, position: position}} <- unify(opts(), Map.new(opts)),
51+
{:ok, ast} = parse(text),
52+
{:ok, {:|>, _m, [left, {right, _, args}]} = original} <- get_pipe_node(ast, position) do
53+
range = make_range(original)
54+
indent = EditHelpers.get_indent(text, range.start.line)
55+
unpiped = {right, [], [left | args]}
56+
57+
%WorkspaceEdit{
58+
changes: %{
59+
uri => [
60+
%TextEdit{
61+
new_text:
62+
EditHelpers.add_indent_to_edit(
63+
Macro.to_string(unpiped),
64+
indent
65+
),
66+
range: range
67+
}
68+
]
69+
}
70+
}
71+
else
72+
{:error, message} ->
73+
%GenLSP.ErrorResponse{code: ErrorCodes.parse_error(), message: inspect(message)}
74+
end
75+
end
76+
4977
defp parse(lines) do
5078
lines
5179
|> Enum.join("\n")
@@ -109,4 +137,35 @@ defmodule NextLS.Commands.ToPipe do
109137
{:ok, node}
110138
end
111139
end
140+
141+
def get_pipe_node(ast, pos) do
142+
pos = [line: pos.line + 1, column: pos.character + 1]
143+
144+
result =
145+
ast
146+
|> Z.zip()
147+
|> Z.traverse(nil, fn tree, acc ->
148+
node = Z.node(tree)
149+
range = Sourceror.get_range(node)
150+
151+
if not is_nil(range) and match?({:|>, _, _}, node) do
152+
if Sourceror.compare_positions(range.start, pos) == :lt &&
153+
Sourceror.compare_positions(range.end, pos) == :gt do
154+
{tree, node}
155+
else
156+
{tree, acc}
157+
end
158+
else
159+
{tree, acc}
160+
end
161+
end)
162+
163+
case result do
164+
{_, nil} ->
165+
{:error, "could not find a pipe operator at the cursor position"}
166+
167+
{_, {_t, _m, [_argument | _rest]} = node} ->
168+
{:ok, node}
169+
end
170+
end
112171
end

Diff for: lib/next_ls/document_symbol.ex

+1-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ defmodule NextLS.DocumentSymbol do
3333
ast
3434
end
3535

36-
ast
37-
|> walker(nil)
38-
|> List.wrap()
36+
List.wrap(walker(ast, nil))
3937
end
4038

4139
defp walker([{{:__literal__, _, [:do]}, {_, _, _exprs} = ast}], mod) do

Diff for: lib/next_ls/runtime/sidecar.ex

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ defmodule NextLS.Runtime.Sidecar do
1717
end
1818

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

2223
{:noreply, state}
@@ -82,6 +83,7 @@ defmodule NextLS.Runtime.Sidecar do
8283
end
8384

8485
def handle_info({{:tracer, :dbg}, payload}, state) do
86+
# credo:disable-for-next-line
8587
dbg(payload)
8688
{:noreply, state}
8789
end

0 commit comments

Comments
 (0)