codemirror / dev

Development repository for the CodeMirror editor project
https://codemirror.net/
Other
5.76k stars 368 forks source link

Adding multi-line block comments for Python #1372

Closed kolibril13 closed 5 months ago

kolibril13 commented 5 months ago

Describe the issue

Currently, it's not possible to toggle multi line block-comments in JupyterLab on non-US keyboard layouts. It would be amazing if this feature could be added to codemirror, as this would help a lot of people all around the world to quickly comment/uncomment their code blocks in JuyterLab. Here is the corresponding comment in JupyterLab: https://github.com/jupyterlab/jupyterlab/issues/16186#issuecomment-2067649537 Thanks for considering this issue! cc. @krassowski

Browser and platform

JupyterLab

Reproduction link

No response

marijnh commented 5 months ago

I wasn't aware Python has block comments. What exactly do you mean?

krassowski commented 5 months ago

To clarify, this has nothing to do with keyboard layouts.

The problem is that the block comment command is no-op in languages where the block comment tokens are not defined.

The first question is whether you would consider having block comment syntax using string literals (""") for lang-python. This is technically not a comment syntax, but is de facto used as such, especially to create function-level comments (docstrings).

Currently lang-python defines commentTokens: {line: "#"} but does not define the block comment.

On JupyterLab repo a frequent request is to make the block comment work for Python. Naively, this would be a small change:

- commentTokens: {line: "#"}
+ commentTokens: {line: "#", block: {open: '"""', close: '"""'}}

but I am not sure if having the same opening and closing syntax for block comments is supported.

An alternative idea would be to fallback from the block comment command to use line comment if block comment is not defined.

marijnh commented 5 months ago

I don't want to set string syntax as comment syntax (though you could do so locally if you want, using pythonLanguage.data.of({commentTokens: ...})). I'm not sure this has nothing to do with key bindings. You could bind toggleComment instead of toggleBlockComment, couldn't you?

krassowski commented 5 months ago

though you could do so locally if you want, using pythonLanguage.data.of({commentTokens: ...})

Neat, thank you for the hint!

You could bind toggleComment instead of toggleBlockComment, couldn't you?

Users often use both Python and JavaScript and another language in the same notebook (Python for computation, JS for visualisation), so they do want to have separate shortcuts for different kinds of comments.

We could write a command which decides based on the language to use one or the other (or more specifically to do that based on whether language data at a given position includes commentTokens.block). The question is whether this should be default for toggleBlockComment but my guess is that it does not need to be in CodeMirror core as writing a custom command here is simple enough and can be done with public API.

marijnh commented 5 months ago

I agree. Given that there's already toggle comment commands for block, line, and any type, I don't want to make the comment-type-specific commands also generic. You'll have to write a custom command, but it can be pretty simple, just running the commands from @codemirror/command in the order you prefer until one returns true.