Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: to-pipe command #318

Merged
merged 1 commit into from
Feb 26, 2024
Merged

Conversation

NJichev
Copy link
Collaborator

@NJichev NJichev commented Nov 1, 2023

Closes #55

Aims to add to and from pipe to next-ls

alias GenLSP.Structures.WorkspaceEdit

defp opts do
Schematic.map(%{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let me know if you have any feedback for the Schematic library! It can get the job done but it was first built with codegen in mind, so the ergonomics are a little off I think. Curious to hear your thought!

Comment on lines 134 to 140
@test_scenarios [
{"to_list(Map.new)", "Map.new() |> to_list()"},
{"to_list(a, b, c)", "a |> to_list(b, c)"},
{"Foo.Bar.baz(foo, bar)", "foo |> Foo.Bar.baz(bar)"},
{"Foo.Bar.baz(foo, bar, Map.new())", "foo |> Foo.Bar.baz(bar, Map.new())"}
]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love this 🙌

@mhanberg
Copy link
Collaborator

mhanberg commented Nov 1, 2023

Looking great at first glance! I'll wait til you mark as ready for review til i do a full... um.. review 😆

end

def new(opts) do
{:ok, %{text: text, uri: uri, position: position}} = Schematic.unify(opts(), Map.new(opts))
Copy link
Collaborator Author

@NJichev NJichev Nov 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it okay crashing the server if we get bad arguments, I should probably take care of getting an error from schematic as well.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to suggest wrapping the whole handle_event callback in the NextLS module in a try/rescue, so that we can log an error to the user and not crash the whole server.

You can publish a window/showMessage notification (I think there are examples in the codebase), so that it actually presents an UI notification to the user's editor

@NJichev NJichev marked this pull request as ready for review November 1, 2023 19:21
lib/next_ls.ex Outdated
GenLSP.notify(lsp, %GenLSP.Notifications.WindowShowMessage{
params: %GenLSP.Structures.ShowMessageParams{
type: GenLSP.Enumerations.MessageType.warning(),
message: "[Next LS] #{command} command has crashed"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also log the exception, and the show message can be [Next LS] #{command} has failed, see the logs for more details

@mhanberg
Copy link
Collaborator

mhanberg commented Nov 1, 2023

Can you also write a couple of full integration tests?

@mhanberg
Copy link
Collaborator

mhanberg commented Nov 2, 2023

Okay, I tested this out and wasn't able to get it to work (after making some changes, I'll push them up on another branch so you can see, I rebased so i don't want to force push to your branch).

I think the integration tests will reveal where it breaks. I'll paste some of the errors I was seeing

to-pipe errors

NextLS: -32700: {[line: 1, column: 42], "missing terminator: } (for \"{\" starting at line 1)", ""}

from-pipe errors

NextLS: -32700: {[line: 1, column: 5], "syntax error before: ", "'|>'"}

@mhanberg
Copy link
Collaborator

mhanberg commented Nov 2, 2023

Changes i made: fb33786

@mhanberg
Copy link
Collaborator

mhanberg commented Nov 3, 2023

We can punt it to another PR, but I didn't realize that we can return this command from the code actions request from the client, so it shows up in the little light bulb menu

@NJichev
Copy link
Collaborator Author

NJichev commented Nov 10, 2023

Changes i made: fb33786

I have added the changes from this branch as well

@@ -114,6 +114,27 @@ defmodule NextLS.Support.Utils do
end
end

defmacro assert_opened(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the best name, but I need to notify the server so that it loads the text for a certain URI into the documents assigns. Other things that I could do is to load the files preemptively with the initialization step. Another way would be to just expect the client to send the text as well? I think this is the cleanest though, just a little overhead in the tests.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems fine, can you rename to did_open ?

and can you actually submit this as a separate PR, and add it to all the tests? then i can merge that and i won't have to approve your CI test runs every time you push

@NJichev NJichev changed the title Add initial from and to pipe command versions feat: add initial from and to pipe command versions Dec 14, 2023
@mhanberg
Copy link
Collaborator

i'll try and review/demo this before the weekend

@mhanberg
Copy link
Collaborator

Tested this out, seems to break in quite a few cases.

I was able to turn parser = code |> new(opts) |> next_token() |> next_token() into parser = next_token(next_token(new(code, opts))), but not the other way around. No error, just did nothing.

trying to do it on with the cursor on parse_program or parser here results in an error

    case parse_program(parser) do
      {ast, %{errors: []}} ->
        {:ok, ast}

      {ast, %{errors: errors}} ->
        {:error, ast, errors}
    end
  end

The error being NextLS: -32700: {[line: 1, column: 34], "missing terminator: end (for \"do\" starting at line 1)", ""}

@NJichev
Copy link
Collaborator Author

NJichev commented Jan 18, 2024

Thanks for testing it out, I'll add those as test scenarios.

The first scenario no idea why it didn't work, I'll have to take a look, also I'll do a better job of reporting the errors.
The second scenario I think it's an easy fix - I just have to add do/end as tokens to the bracket counting as well and I think it'll work fine, since currently it doesn't know to get more pieces of code until it compiles the case.
I will think of similar cases as this one

@mhanberg
Copy link
Collaborator

You can use the error tolerant parser be been building (Spitfire) in Next LS now, so it might make this easier.

@NJichev NJichev force-pushed the add-pipe-code-actions branch 3 times, most recently from 94b3e06 to bd17ae2 Compare February 21, 2024 16:36
@mhanberg
Copy link
Collaborator

CleanShot.2024-02-24.at.17.39.17.mp4

found a case where it leaves a stray variable above the function it un-pipes

@mhanberg
Copy link
Collaborator

Also that one was a little odd in that it un-piped the begging of the pipe chain instead of the one under the cursor.

I would have expected this more or less

Enum.map(
  file |> NextLS.ASTHelpers.Variables.list_variable_references({line, col}),
  fn {_name, {startl..endl, startc..endc}} ->
    [file, startl, endl, startc, endc]
end)

I'm not sure if that is enough to stop this from merging, can improve it later

@mhanberg
Copy link
Collaborator

also putting the cursor here results in the error

#    👇
Enum.map(
  NextLS.ASTHelpers.Variables.list_variable_references(file, {line, col}),
  fn {_name, {startl..endl, startc..endc}} ->
    [file, startl, endl, startc, endc]
  end
)

[Error] [NextLS] ** (ArithmeticError) bad argument in arithmetic expression

@mhanberg
Copy link
Collaborator

I'll see if i can't get some time to address those test cases tonight

@mhanberg
Copy link
Collaborator

I have some local changes but haven't finished them yet, I'll try to get that done tomorrow.

@mhanberg mhanberg changed the title feat: add initial from and to pipe command versions feat: to-pipe command Feb 25, 2024
@mhanberg mhanberg force-pushed the add-pipe-code-actions branch 3 times, most recently from 36cc9ba to 231e84f Compare February 25, 2024 19:57
@mhanberg
Copy link
Collaborator

Fixing a test and the nix flake and then it'll be mergeable.

I decided to include the Sourceror library as it has a lot of functions around positions that I was about to remake. As well as its zipper implementation which is useful

@mhanberg
Copy link
Collaborator

Also I broke out from-pipe into a second PR

Co-authored-by: Mitchell Hanberg <[email protected]>
@mhanberg mhanberg merged commit cfa7eb2 into elixir-tools:main Feb 26, 2024
13 checks passed
@NJichev NJichev deleted the add-pipe-code-actions branch February 26, 2024 03:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

to/from pipe
2 participants