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

code actions for dsls #402

Merged
merged 88 commits into from
Sep 27, 2024
Merged

code actions for dsls #402

merged 88 commits into from
Sep 27, 2024

Conversation

jurgenvinju
Copy link
Member

@jurgenvinju jurgenvinju commented Jul 2, 2024

This PR connects the codeActions LSP server service for the parametrized LSP server (so for DLSs).

  • A CodeActionContributor is of type list[Command] (list[Tree] focus) and registered with data LanguageService = codeActionContributor(CodeActionContributor actions). The "focus" tells us which parse trees are active on the given line and column of the current cursor, ordered with the most specific (smallest) trees first and ending always with the start tree.

Pico example, where we are looking for a cursor position over an IdType somewhere in a start[Program], and we register the command removeDecl which can be interpreter later:

list[Command] picoActions([*_, IdType x, *_, start[Program] program]) 
    = [removeDecl(program, x, title="remove <x>")];
  • Normal Commands are used just like the lenses. They need to be evaluated by the commandExecutor contribution.
  • Additionally, every Message can now have a keyword parameter: data Message(list[Command] fixes=[]). Every message that is registred with the LSP client (e.g. via a Summary), will lead to a contributed code action when the cursor is at the at location of the info, warning or error message.
  • demo::lang::pico::LanguageServer demonstrates these two different ways of contributing actions (see above). Below the Message alternative:
s.messages += {<src, error("<id> is not defined", src, 
                                               fixes=[changeToFix(src, existing<0>, title="Change to <existing<0>>") | existing <- defs])
                             > 
                            | <src, id> <- uses, id notin defs<0>};

The error constructor now receives an additional fixes keyword field with a list of options on how to fix the broken variable reference. Each generated command fixes the bug in its own way (with a different variable name to use).

@jurgenvinju
Copy link
Member Author

Will post some screenshots later. This is still a draft.

@jurgenvinju jurgenvinju self-assigned this Jul 2, 2024
Copy link

sonarcloud bot commented Jul 2, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

@jurgenvinju
Copy link
Member Author

lightbulb appears next to error message:

image

CMD+dot activates menu:

image

Selecting the right option fixes the issue:

image

Code for the quickfix:

// attaches multiple quickfix command options to every error (one for every proposed new name):
 s.messages += {<src, error("<id> is not defined", src, fixes=[changeToFix(src, existing<0>, title="Change to <existing<0>>") | existing <- defs])> 
                  | <src, id> <- uses, id notin defs<0>};

// executes the command:
value picoCommands(changeToFix(loc src, str newName)) {
    applyDocumentsEdits([changed(src.top, [replace(src, newName)])]);
    return ("result": true);
}

@jurgenvinju
Copy link
Member Author

Looking for suggestions on how to improve the asynchronous code and suggestions how to test the UI part of this. It's most LSP integration code. The algorithm for generating focus lists could be tested as a unit.

@jurgenvinju jurgenvinju marked this pull request as ready for review July 5, 2024 20:56
@DavyLandman DavyLandman marked this pull request as ready for review September 27, 2024 08:04
Copy link
Member

@DavyLandman DavyLandman left a comment

Choose a reason for hiding this comment

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

I think this code is ready for merging. @jurgenvinju when all the test has passed, you do the honors 🎉

Copy link

sonarcloud bot commented Sep 27, 2024

@jurgenvinju jurgenvinju merged commit b0ec081 into main Sep 27, 2024
12 checks passed
@jurgenvinju jurgenvinju deleted the code-actions-for-dsls branch September 27, 2024 15:46
@jurgenvinju
Copy link
Member Author

Done. This was a very nice collab @DavyLandman

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.

3 participants