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

Fix indentation with OnTypeFormatting method for \n #2922

Merged
merged 14 commits into from
May 30, 2024
Merged

Conversation

lionel-
Copy link
Contributor

@lionel- lionel- commented Apr 29, 2024

Adds an LSP method for OnTypeFormatting to correct the indentation of the frontend.

Addresses #1880
Addresses #2764
Addresses #2707

See companion PR posit-dev/ark#329 (to be merged first) for details.

Intent

Fix remaining indentation issues that we haven't been able to fix due to the limitations of the regexp-based indentation rules of VS Code.

Approach

This PR enables the formatOnType feature in the positron-r extension to let Ark correct problematic indentation based on the up-to-date tree-sitter representation of the documents. When the user hits enter, an LSP request is sent to format around \n. If the ideal indentation differs from what we managed to do with regular expression, Ark sends a correction.

The correction is noticeable, i.e. you can see the cursor move more or less quickly. I haven't find this too distracting in my limited usage. This is also the approach taken for the Python extension with the proprietary pylance server providing indent corrections, and it's been working sufficiently well for them. If that is not sufficient for us, one potential approach would be to move the formatting engine to the renderer process, perhaps in the form of a wasm plugin, but that approach has lots of unknowns.

To work around the ordering issue of our LSP server that we rencently discovered (see #2692 (comment)), the LSP request is sent via a custom OnTypeFormatting provider for \n that sends a custom formatting request to Ark. This allows sending a version ID for the document to detect an ordering problem. In that case we give up on the indent correction to prevent corruption of the document on the frontend side.

The integration tests have been updated to record the correction in snapshots if there is any. This required adding a waitLsp() method to synchronise the frontend and backend. In addition we're polling for runtime discovery to finish. It would be nice to have some better way of waiting for this in the future.

QA notes

The indentation bugs in the linked issues should be fixed at top-level as well as in nested code, e.g. inside functions or other constructs.

@lionel- lionel- requested a review from DavisVaughan April 29, 2024 16:35
Copy link
Contributor

@DavisVaughan DavisVaughan left a comment

Choose a reason for hiding this comment

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

This half looks good! On to ark...

extensions/positron-r/src/formatting.ts Outdated Show resolved Hide resolved
new RequestType('ark/internal/onTypeFormatting');

export function registerOnTypeFormatter(client: LanguageClient): Disposable {
const rSel = { scheme: 'file', language: 'r' } as vscode.DocumentSelector;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should you be using these document selections instead?

{ language: 'r', scheme: 'untitled' },
{ language: 'r', scheme: 'inmemory' }, // Console
{ language: 'r', pattern: '**/*.{r,R}' },
{ language: 'r', pattern: '**/*.{qmd,Qmd}' },
{ language: 'r', pattern: '**/*.{rmd,Rmd}' },

i.e. does it work in the console / do we want it to? does it work in untitled files?

(we should probably try and put that list of document selectors somewhere that can be shared between this and the LSP if we decide to use them)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok this definitely doesn't work in untitled files so an update is definitely needed here!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! We now reuse the same selectors as the LSP.

This still doesn't work in Quarto documents, I've opened an issue: quarto-dev/quarto#432

Copy link
Contributor Author

@lionel- lionel- May 3, 2024

Choose a reason for hiding this comment

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

Somehow it doesn't work in the console. Would be nice if it did but I can't see this ever being a priority? Well I guess it depends on the workflow, I'm always surprised when I see folks entering multi-line code in the console, but I do see it sometimes (mostly python folks?). I tend to always write non-trivial code in a script and evaluate from there.

Copy link
Contributor

Choose a reason for hiding this comment

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

@lionel- I bet you need to add FormatOnType.ID to our Console's set of editor contributions, like I did in #1675 for parameter hints

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good hunch, unfortunately that didn't work.

extensions/positron-r/src/formatting.ts Outdated Show resolved Hide resolved
extensions/positron-r/src/formatting.ts Outdated Show resolved Hide resolved
@lionel- lionel- merged commit 0ed87bf into main May 30, 2024
1 check passed
@lionel- lionel- deleted the feature/indent branch May 30, 2024 19:36
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.

2 participants