Skip to content

Toggle comments#3047

Open
jramosg wants to merge 20 commits intoBetterThanTomorrow:devfrom
jramosg:toggle-comments
Open

Toggle comments#3047
jramosg wants to merge 20 commits intoBetterThanTomorrow:devfrom
jramosg:toggle-comments

Conversation

@jramosg
Copy link
Contributor

@jramosg jramosg commented Feb 13, 2026

What has changed?

  • Created calva.toggleLineComment with same keybinding as editor.action.commentLine
  • Created toggleLineCommentCommand wich is executed with calva.toggleLineComment and wraps editor.action.commentLine with the extra feature of indenting commented/uncommented lines.
  • Created integration tests

Fixes #2872

My Calva PR Checklist

I have:

  • Read How to Contribute.
  • Directed this pull request at the dev branch. (Or have specific reasons to target some other branch.)
  • Made sure I have changed the PR base branch, so that it is not published. (Sorry for the nagging.)
  • Made sure there is an issue registered with a clear problem statement that this PR addresses, (created the issue if it was not present).
    • Updated the [Unreleased] entry in CHANGELOG.md, linking the issue(s) that the PR is addressing.
  • Figured if anything about the fix warrants tests on Mac/Linux/Windows/Remote/Whatever, and either tested it there if so, or mentioned it in the PR.
  • Added to or updated docs in this branch, if appropriate
  • Tests
    • Tested the particular change
    • Figured if the change might have some side effects and tested those as well.
  • Formatted all JavaScript and TypeScript code that was changed. (use the prettier extension or run npm run prettier-format)
  • Confirmed that there are no linter warnings or errors (use the eslint extension, run npm run eslint before creating your PR, or run npm run eslint-watch to eslint as you go).

This function wraps the default comment line command to ensure that
commented lines maintain proper Clojure-aware indentation. It calculates
the necessary indent fixes after commenting and applies them to the
affected lines, improving the alignment of code structures like
association arguments.
This commit introduces a new command to toggle line comments in
Clojure files. The command is registered in the package.json and
associated with the keybinding Ctrl+/ for ease of use. This
enhancement improves the editing experience for users by allowing
quick commenting and uncommenting of lines.

Fixes BetterThanTomorrow#2872
This commit introduces a suite of integration tests for the toggle
comment feature in the editor. The tests cover various scenarios,
including commenting and uncommenting lines with correct indentation,
handling multiple cursors, and ensuring alignment in nested forms.
These tests aim to improve the reliability and correctness of the
toggle comment functionality.
@jramosg jramosg requested a review from PEZ February 13, 2026 07:28
@netlify
Copy link

netlify bot commented Feb 13, 2026

Deploy Preview for calva-docs ready!

Name Link
🔨 Latest commit 1f42cae
🔍 Latest deploy log https://app.netlify.com/projects/calva-docs/deploys/6991a0efcb4dc200086bdb1f
😎 Deploy Preview https://deploy-preview-3047--calva-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

…r behavior in edit.ts:

- Added early returns for non-Clojure/no-op cases.
- Made affected-line handling deterministic (validated + sorted line numbers).
- Simplified indent-fix edit application by applying fix.delta directly (no intermediate TextEdit array).
Introduce a new keybinding for toggling line comments that preserves
Clojure-aware indentation. This enhancement improves usability by
allowing users to comment or uncomment lines while maintaining
structural integrity in their code.
This commit introduces an interface and a function to calculate
indent fixes for lines in a document. The new functionality allows
for determining the necessary adjustments to line indentation,
facilitating better formatting and code readability.
@PEZ
Copy link
Collaborator

PEZ commented Feb 13, 2026

Thanks for doing this! We should probably be overriding the built-in comment toggle command for people who run with calva.paredit.hijackVSCodeDefaults enabled.

await new Promise((resolve) => setTimeout(resolve, 20 * pauseMs));
assert.equal(
await toggleCommentUsingActiveEditor('(defn foo []• |(println "test"))'),
'(defn foo []• |;; (println "test"))'
Copy link
Collaborator

Choose a reason for hiding this comment

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

This breaks the structure of the document. The end result needs to be:

(defn foo []•  ;; |(println "test")•  )

There's a paredit function today which knows how to do this correctly: insertSemiColon.

@PEZ
Copy link
Collaborator

PEZ commented Feb 13, 2026

This command should to be structural, I think. It doesn't make sense with a Paredit command that breaks structure. 😄

Clarify the behavior of the Toggle Line Comment feature
to specify how it handles single selections and multi-cursor
cases, ensuring users understand the structural indentation
preservation and semicolon insertion.
@jramosg
Copy link
Contributor Author

jramosg commented Feb 13, 2026

I now use insertSemiColon for single selection lines and for multiple selection I apply editor.action.commentLine and then restore to structural clojure aware indentation


it('should uncomment a line and restore correct indentation', async () => {
assert.equal(
await toggleCommentUsingActiveEditor('(defn foo []• ;; |(println "test"))'),
Copy link
Collaborator

Choose a reason for hiding this comment

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

It hurts my eyes a bit with the unstructured code here. 😄 But it's good that we handle it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is the uncomment case, it's testing how would be aligned after uncommenting that unstructured code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the test above is the comment case, where it throws the ) to the next line

Copy link
Collaborator

Choose a reason for hiding this comment

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

I know. I guess I could see it as healing the broken code. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😬

@jramosg
Copy link
Contributor Author

jramosg commented Feb 13, 2026

screen-capture.webm

@PEZ
Copy link
Collaborator

PEZ commented Feb 13, 2026

I now use insertSemiColon for single selection lines and for multiple selection I apply editor.action.commentLine and then restore to structural clojure aware indentation

That's similar to what insertSemiColon does. Same day we should make both commands multi-cursor compliant! 💪

package.json Outdated
"command": "calva.toggleLineComment",
"key": "ctrl+/",
"mac": "cmd+/",
"when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && !editorReadOnly"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should also check that the user wants us to highjack built-in shortcuts. There's a config for that, and prior art how to check the config from the when-expression.

@maxrothman
Copy link
Contributor

maxrothman commented Feb 15, 2026

Fixes #2872

Whoops, it was already linked 😅

Clarify the behavior of the Toggle Line Comment feature
to emphasize Clojure-aware behavior and reformatting
of enclosing forms, enhancing user understanding of
its functionality.
Enhanced the conditions for the toggle line comment command to include
the new configuration option for paredit. Added a new keybinding for
inserting a semicolon with specific conditions to improve user
experience in Clojure editing.
This commit introduces tests for the insertSemiColon function in the
paredit util. The tests verify that a semicolon is correctly inserted
at the cursor without breaking the structure, and that a newline is
added when necessary to preserve the document's structure.
Enhance the insertSemiColon function to maintain line indentation
when inserting a semicolon. This change ensures that the code
structure remains intact and improves readability.
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