Skip to content

Parametric: formatting - #677

Merged
DavyLandman merged 65 commits into
mainfrom
feature/lsp-formatting
Sep 2, 2026
Merged

Parametric: formatting#677
DavyLandman merged 65 commits into
mainfrom
feature/lsp-formatting

Conversation

@toinehartman

@toinehartman toinehartman commented Jul 11, 2025

Copy link
Copy Markdown
Member
  • Implementing the formatting and rangeFormatting LSP APIs. Both use the same contribution, where for (whole-file) formatting _range == _input@\loc.

Design:

data LanguageService = formatting(list[TextEdit](Focus _focus, FormattingOptions _opts) formattingService);

data FormattingOptions(
        int tabSize = 4
      , bool insertSpaces = true
      , bool trimTrailingWhitespace = false
      , bool insertFinalNewline = false
      , bool trimFinalNewlines = false
) = formattingOptions();

Closes #130.

  • added formatting support for Rascal as well, since that shares the essential new focus computation based on a selection range with the current PR.

@toinehartman
toinehartman force-pushed the feature/lsp-formatting branch from 79abb8f to f0930d1 Compare July 11, 2025 09:07
Comment thread rascal-lsp/src/main/rascal/library/util/LanguageServer.rsc
Comment thread rascal-lsp/src/main/rascal/library/util/LanguageServer.rsc Outdated
@toinehartman
toinehartman requested a review from DavyLandman July 14, 2025 17:19
@sonarqubecloud

Copy link
Copy Markdown

@DavyLandman DavyLandman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see this is a bit of a challenging API, especially if we want to make this more user-friendly for DSL users.

I have the feeling we should involve @jurgenvinju who has been on the topic of formatting/pretty-printing for quite some time already. I suspect that we're missing some opportunities of matching this up with something already in the standard library, or something that we might should add to the standard library first.

Comment thread rascal-lsp/src/main/rascal/library/demo/lang/pico/LanguageServer.rsc Outdated
Comment thread rascal-lsp/src/main/rascal/library/util/LanguageServer.rsc Outdated
Comment thread rascal-lsp/src/main/rascal/library/util/LanguageServer.rsc Outdated
Comment thread rascal-lsp/src/main/rascal/library/util/LanguageServer.rsc Outdated
@DavyLandman

Copy link
Copy Markdown
Member

This PR is missing the adjacent: rangeFormatting functionality.

@toinehartman
toinehartman force-pushed the feature/lsp-formatting branch 3 times, most recently from 4b58c98 to 4aca661 Compare August 18, 2025 14:50
Comment thread rascal-lsp/src/main/rascal/library/util/Format.rsc Outdated

@DavyLandman DavyLandman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is going in the right direction, but I'm not sure what I should have been reviewing, the implementation or the design?

Comment thread rascal-lsp/src/main/rascal/library/util/Format.rsc Outdated
Comment thread rascal-lsp/src/main/rascal/library/demo/lang/pico/LanguageServer.rsc Outdated
Comment thread rascal-lsp/src/main/rascal/library/util/LanguageServer.rsc Outdated
Comment thread rascal-lsp/src/main/rascal/library/util/LanguageServer.rsc Outdated
Comment thread rascal-lsp/src/main/rascal/library/util/LanguageServer.rsc Outdated
@toinehartman
toinehartman force-pushed the feature/lsp-formatting branch from 2b7fb47 to e05f700 Compare August 25, 2025 15:02
@toinehartman
toinehartman changed the base branch from main to migrate-dap August 25, 2025 15:02
@toinehartman
toinehartman force-pushed the feature/lsp-formatting branch 2 times, most recently from 9e7fe1a to 523c2fe Compare August 26, 2025 09:41
Base automatically changed from migrate-dap to main August 27, 2025 09:25
@toinehartman
toinehartman force-pushed the feature/lsp-formatting branch from 523c2fe to ab448fc Compare August 27, 2025 12:06
@toinehartman
toinehartman marked this pull request as ready for review August 29, 2025 12:35
@jurgenvinju

Copy link
Copy Markdown
Member

Is there an opportunity to use the Focus abstraction for this contribution? Especially for the range alternative.

That way language engineers don't have to go search for the right trees anymore, and they could easily support only a few top-level types for starters, and they could easily recover the required indentation level from the parent tree's layout siblings.

@toinehartman

toinehartman commented Aug 29, 2025

Copy link
Copy Markdown
Member Author

Is there an opportunity to use the Focus abstraction for this contribution? Especially for the range alternative.

@jurgenvinju I thought about this, but the tricky part there is that a range does not necessarily correspond to an exact tree. Even if we give a focus tree that ends at the largest tree encapsulating the range, there would still be the need to filter the edits so they are not outside of the given range.

@toinehartman
toinehartman force-pushed the feature/lsp-formatting branch 2 times, most recently from 7a9d722 to 01d9853 Compare September 1, 2025 12:05
@sonarqubecloud

sonarqubecloud Bot commented Jun 8, 2026

Copy link
Copy Markdown

@github-advanced-security github-advanced-security AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ESLint found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

@sonarqubecloud

Copy link
Copy Markdown

@toinehartman

Copy link
Copy Markdown
Member Author

@jurgenvinju and I made sure that all the changes necessary to format Rascal code are now available in #1102. This PR will be left draft for a while until we are ready to clean this up and resume work on DSL formatting.

DavyLandman added a commit that referenced this pull request Sep 2, 2026
This work by @toinehartman and YT introduces the Rascal formatter to
VScode.

* [x] links the Rascal formatter from the stdlib to the `formatDocument`
and `formatSelection` capabilities in LSP for Rascal
* [x] maps all FormattingOptions, taken from: #677 
* [x] support for formatting only a selection via the `Focus` mechanism,
also taken from #677
* [x] works with subTreeEditFormatter from `util::Formatters` correctly
* [x] adds two UI tests, one for whole file formatting and one for
selection formatting
@DavyLandman
DavyLandman merged commit d50db56 into main Sep 2, 2026
13 of 17 checks passed
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.

Expose LSP formatter (pretty-printer) api via LanguageService

7 participants