Skip to content

Format content pasted into string literals #2703

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

Merged
merged 1 commit into from
Dec 22, 2022

Conversation

datho7561
Copy link
Contributor

@datho7561 datho7561 commented Sep 27, 2022

Testing this PR requires using vscode-insiders (until VS Code 1.74.0 is released).

Uses the proposed API to modify pasted strings. It invokes jdt.ls's java.edit.handlePasteEvent command when vscode-java receives a paste event. When pasting into a string literal, jdt.ls modifies the content that's inserted so that the string content reflects the original value of the copied text. It uses string concatenation instead of the newer Java multiline string feature to represent multiline text.

Requires eclipse-jdtls/eclipse.jdt.ls#2349
Closes #1249

@datho7561
Copy link
Contributor Author

datho7561 commented Oct 13, 2022

In order to use this PR:

  • Make sure you are using VS Code Insiders
  • Run npx vscode-dts main before launching. This will prompt to replace the current vscode type definitions with the proposed API changes
  • Set "editor.experimental.pasteActions.enabled": true, in settings.json

@datho7561
Copy link
Contributor Author

datho7561 commented Oct 13, 2022

Here is a demo:

paste-string-demo

@rgrunber
Copy link
Member

Please make sure this doesn't affect text blocks (eg. """ ... """ )

* @param context the extension context
*/
export function registerPasteEventHandlers(context: ExtensionContext) {
if (languages["registerDocumentPasteEditProvider"]){
Copy link
Member

@rgrunber rgrunber Nov 9, 2022

Choose a reason for hiding this comment

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

@datho7561 @fbricon Looking at https://github.com/eclipse-theia/theia/blob/7abca9d57c966d621f2acc26008cf1a9e454949b/packages/plugin-ext/src/plugin/plugin-context.ts#L674 , I don't see registerDocumentPasteEditProvider, so doesn't that imply this check would safely handle the case in such clients ?

@datho7561 , can you confirm the extension installs just fine on Theia with enableProposedApi set the way we do here. If so I would think it should be fine.

Update : Nearly forgot, to truly test this, we can't use VS Code Insiders, because that's not what Theia would use. Just the released VS Code. While it seems it does contain the proposed API, David mentioned he couldn't actually get it working there, while in VS Code Insiders it works just fine. So it would be difficult to test this thoroughly, which is why I suggested just checking if it can install without issue.

Copy link
Collaborator

Choose a reason for hiding this comment

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

languages["registerDocumentPasteEditProvider"] is probably correct, but yeah the concern is what's the behavior of theia when enableProposedApi is on

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From my testing in Theia, no warnings appear and the language server starts as normal, but none of the features I introduced are present (paste works exactly as it did before my change).

@datho7561 datho7561 force-pushed the 1249-paste-into-string branch 4 times, most recently from 436e114 to df3fa47 Compare November 28, 2022 19:20
@datho7561 datho7561 changed the title POC of formatting content pasted into strings Format content pasted into string literals Nov 28, 2022
@datho7561 datho7561 marked this pull request as ready for review November 28, 2022 20:50
@datho7561 datho7561 force-pushed the 1249-paste-into-string branch from df3fa47 to 70b6930 Compare November 29, 2022 18:05
@datho7561
Copy link
Contributor Author

Thanks for the review @CsCherrYY !

@datho7561 datho7561 force-pushed the 1249-paste-into-string branch 2 times, most recently from e418663 to 79c724f Compare December 1, 2022 13:19
@CsCherrYY
Copy link
Contributor

I just checked the behaviours in different VS Code versions:

1.65.2 (the olderest version matches engine):
The extension work well without paste handler, and there is an error about the missing proposed API:
image

1.73.1 (newest stable):
The extension work well without paste handler, and there is an error about the proposed API is not permitted (since redhat.java is not present at the whitelist in this release):
image

in the next stable release (suppose 1.74.0) our extension will work with the paste handler and there should not be any error. I'm considering if it's possible to update the engine in the extension, to restrict the new version is only available to new engine users. Is there any consideration from Theia side? @rgrunber

@CsCherrYY
Copy link
Contributor

looks like the newest VS Code release (1.74.0) includes the allowance of the proposed API of redhat.java, @datho7561 could you please help verify if this PR works as expected?

@datho7561 datho7561 force-pushed the 1249-paste-into-string branch from 79c724f to 7a9cc40 Compare December 12, 2022 16:58
@datho7561
Copy link
Contributor Author

As is, paste events don't seem to work with this PR in VS Code 1.74.0. I don't get any error pop ups or logs to the extension host complaining, but the pasted content doesn't get modified. I've rebased my PRs to make it easier to test out. I'll try a few things like changing the engine version to see if that makes a difference.

@datho7561
Copy link
Contributor Author

datho7561 commented Dec 12, 2022

I tried updating the engine version, but that didn't help. I also confirmed that the product.json contains the change to allow vscode-java to use the proposed api.

@datho7561 datho7561 force-pushed the 1249-paste-into-string branch from 7a9cc40 to 58d9fbb Compare December 12, 2022 19:26
@datho7561
Copy link
Contributor Author

It looks like the Markdown paste feature (where when you paste an image, it creates the image file in the workspace folder and adds a markdown image tag that references the file) also only works on Insiders.

@CsCherrYY
Copy link
Contributor

Did you set editor.experimental.pasteActions.enabled to true?

@datho7561
Copy link
Contributor Author

Did you set editor.experimental.pasteActions.enabled to true?

Nope, I forgot. Thanks for your help, it's working!

Copy link
Contributor

@CsCherrYY CsCherrYY left a comment

Choose a reason for hiding this comment

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

LGTM!

}
};

const pasteResponse: PDocumentPasteEdit = await commands.executeCommand(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.HANDLE_PASTE_EVENT, pasteEventParams);
Copy link
Member

Choose a reason for hiding this comment

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

Calling JSON.stringify(pasteEventParams) will save some code on the language server side deserialization. If you look around the codebase, you'll see we use this in many places. I can make the change.

- Invoke jdt.ls's `java.edit.handlePasteEvent` command when vscode-java
  receives a paste event

Requires eclipse-jdtls/eclipse.jdt.ls#2349
Closes redhat-developer#1249

Signed-off-by: David Thompson <davthomp@redhat.com>
@rgrunber rgrunber force-pushed the 1249-paste-into-string branch from 58d9fbb to 5575d68 Compare December 22, 2022 01:08
@rgrunber rgrunber merged commit f93cc8b into redhat-developer:master Dec 22, 2022
@datho7561 datho7561 deleted the 1249-paste-into-string branch January 3, 2023 13:32
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.

paste a multi-line string should create a java string
4 participants