Skip to content

add toggleMultilineStringLiteral command #422

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

Conversation

devhawk
Copy link
Contributor

@devhawk devhawk commented Apr 5, 2025

This PR creates a command to toggle lines in the current selection to be part of a multiline string literal. The command adds or removes the multiline string token from each line as appropriate.

I do a lot of SQL work and tend to copy/paste multiline string literals into my zig source code from other sources. But I got tired of manually adding the multiline string token \\ to each line.

Since there aren't any other stand-alone commands in the project, I added the command function to the extension file. I'm guessing that's not the best location, but I figured better to put it there and move it to elsewhere based on feedback.

I mapped this command by default to Ctrl-M Ctrl-S (for Multiline String). I think of this command similar to the add/remove line comment commands that are mapped to Ctrl-K Ctrl-C / Ctrl-K Ctrl-U. However, I didn't think that removing multiline string literal tokens would be that common scenario, so I created a single toggle command instead of separate add/remove commands. Happy to change this to separate add/remove commands based on feedback.

Copy link
Member

@Vexu Vexu left a comment

Choose a reason for hiding this comment

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

Looks good!

Empty lines could be handled better so that

    a

    b

becomes

    \\a
    \\
    \\b

instead of

    \\a
\\
    \\b

But this can also be fixed by formatting the document so it's not critical.

@devhawk
Copy link
Contributor Author

devhawk commented Apr 6, 2025

thanks! I can fix that. Also, I need to pick a new default keyboard shortcut. Binding this command to Ctrl-M Ctrl-S breaks the default binding of Ctrl-M to Toggle Tab Key Moves Focus.

@Techatrix
Copy link
Collaborator

The VS Code API provides a DocumentPasteEditProvider that can be used to customize the behavior when a user copy pastes into a document. This could be used to detect when pasting into a multi line string literal and automatically convert the pasted content. Because users don’t need to be aware of the new command being added, this approach could be more accessible because it requires less knowledge of the extension.

@devhawk
Copy link
Contributor Author

devhawk commented Apr 6, 2025

The VS Code API provides a DocumentPasteEditProvider that can be used to customize the behavior when a user copy pastes into a document. This could be used to detect when pasting into a multi line string literal and automatically convert the pasted content. Because users don’t need to be aware of the new command being added, this approach could be more accessible because it requires less knowledge of the extension.

I'll take a look.

@Vexu
Copy link
Member

Vexu commented Apr 6, 2025

The VS Code API provides a DocumentPasteEditProvider that can be used to customize the behavior when a user copy pastes into a document. This could be used to detect when pasting into a multi line string literal and automatically convert the pasted content. Because users don’t need to be aware of the new command being added, this approach could be more accessible because it requires less knowledge of the extension.

I think it would still be useful to have the command even if without a default key bind.

@devhawk
Copy link
Contributor Author

devhawk commented Apr 7, 2025

I took a look at DocumentPasteEditProvider and its official sample. I don't think it's going to work for this feature.

Yes, it would be possible for a DocumentPasteEditProvider to add multiline string literal tokens when pasting. But when would/should the provider do that? There's no context in the source document, the clipboard contents or the destination document that the provider could use as a definitive signal that the tokens should be added. Then you're dependent on some kind of specific user clipboard action such as the Paste As command, which would be even less discoverable than a new command.

If it's ok w/ you @Techatrix, I'd rather just leave this as a simple command and skip implementing a paste provider.

@Techatrix
Copy link
Collaborator

But when would/should the provider do that?

When pasting into an already existing multi line string literal.

If it's ok w/ you @Techatrix, I'd rather just leave this as a simple command and skip implementing a paste provider.

No objections. As suggested before, there is not reason why there couldn't be both a command and a DocumentPasteEditProvider. I will look into the latter myself as a possible follow-up PR.

@Vexu Vexu merged commit 70c2cc5 into ziglang:master Apr 9, 2025
1 check 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.

3 participants