Skip to content

Conversation

@Sanjai-Shaarugesh
Copy link

Adds comment toggle functionality to the editor, similar to VSCode and Zed

Changes

  • Added Ctrl+/ to toggle line comments
  • Added Ctrl+Shift+A to toggle block comments
  • Updated keyboard shortcuts dialog with new shortcuts
  • Supports single and multi-line selections
  • Preserves indentation when commenting

Closes #67

Copy link
Owner

@sonnyp sonnyp left a comment

Choose a reason for hiding this comment

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

Good job!

src/editor.js Outdated
Comment on lines 53 to 57
// Check for Ctrl+Shift+A (block comment toggle)
if (ctrl_pressed && shift_pressed && (keyval === Gdk.KEY_A || keyval === Gdk.KEY_a)) {
toggleBlockComment(buffer, comment_prefix || "#");
return true; // Event handled
}
Copy link
Owner

Choose a reason for hiding this comment

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

I don't understand what block comment is about.

This is what happens on multiple selected lines

Screencast.From.2026-01-05.19-45-45.webm

What this meant for languages like JavaScript with block comments?

/*
 1
 2
*/

I don't think that's useful in Commit but I would love to have it in Workbench :)

Copy link
Author

Choose a reason for hiding this comment

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

I'll remove the Ctrl+Shift+A block comment functionality

@@ -1,56 +1,54 @@
using Gtk 4.0;

Copy link
Owner

Choose a reason for hiding this comment

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

please leave and follow indentation style

Comment on lines 29 to 33
ShortcutsShortcut {
accelerator: "<Shift><Control>a";
title: _("Toggle Block Comment");
}
ShortcutsShortcut {
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
ShortcutsShortcut {
accelerator: "<Shift><Control>a";
title: _("Toggle Block Comment");
}
ShortcutsShortcut {
ShortcutsShortcut {
accelerator: "<Shift><Control>a";
title: _("Toggle Block Comment");
}
ShortcutsShortcut {

const event_controller = new Gtk.EventControllerKey();
source_view.add_controller(event_controller);

event_controller.connect("key-pressed", (controller, keyval, keycode, state) => {
Copy link
Owner

Choose a reason for hiding this comment

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

If we don't know the comment prefix, let's return

Suggested change
event_controller.connect("key-pressed", (controller, keyval, keycode, state) => {
event_controller.connect("key-pressed", (controller, keyval, keycode, state) => {
if (!comment_prefix) {
return false; // Event not handled
}


// Check for Ctrl+/ (line comment toggle)
if (ctrl_pressed && !shift_pressed && keyval === Gdk.KEY_slash) {
toggleLineComment(buffer, comment_prefix || "#");
Copy link
Owner

Choose a reason for hiding this comment

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

Not this function job :)

Suggested change
toggleLineComment(buffer, comment_prefix || "#");
toggleLineComment(buffer, comment_prefix);

src/editor.js Outdated

// Check for Ctrl+Shift+A (block comment toggle)
if (ctrl_pressed && shift_pressed && (keyval === Gdk.KEY_A || keyval === Gdk.KEY_a)) {
toggleBlockComment(buffer, comment_prefix || "#");
Copy link
Owner

Choose a reason for hiding this comment

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

ditto

Suggested change
toggleBlockComment(buffer, comment_prefix || "#");
toggleBlockComment(buffer, comment_prefix);

@Sanjai-Shaarugesh
Copy link
Author

Hey! I've removed the Ctrl+Shift+A block comment functionality as requested.

The code now only has Ctrl+/ for toggling line comments, which:

  • Works on single or multiple selected lines
  • Automatically detects if lines are commented and toggles them
  • Preserves indentation and skips empty lines
  • Cleans up the space after comment prefix when uncommenting

It uses the existing comment_prefix so it should work across different file types.

Let me know if you need any other changes!

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.

Add shortcuts to comment/uncomment lines

2 participants