Skip to content

feat: make CodeEditor toggle-comment shortcut configurable#7896

Open
jespermeller wants to merge 2 commits intousebruno:mainfrom
jespermeller:configurable-toggle-comment-key
Open

feat: make CodeEditor toggle-comment shortcut configurable#7896
jespermeller wants to merge 2 commits intousebruno:mainfrom
jespermeller:configurable-toggle-comment-key

Conversation

@jespermeller
Copy link
Copy Markdown

@jespermeller jespermeller commented Apr 30, 2026

Fixes: #7874

Heavily build with AI, but i ran it and it worked. I could change keybindings and use it in the json editor.

Description

Contribution Checklist:

  • I've used AI significantly to create this pull request
  • The pull request only addresses one issue or adds one feature.
  • The pull request does not introduce any breaking changes
  • I have added screenshots or gifs to help explain the change if applicable.
  • I have read the contribution guidelines.
  • Create an issue and link to the pull request.

Note: Keeping the PR small and focused helps make it easier to review and merge. If you have multiple changes you want to make, please consider submitting them as separate pull requests.

Publishing to New Package Managers

Please see here for more information.

Summary by CodeRabbit

  • New Features
    • Code editor comment-toggle shortcut now respects users’ custom hotkey preferences (platform-aware).
    • Editor hotkey mappings added with sensible defaults for comment toggling and logic to derive platform-appropriate shortcuts.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 30, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: a1171af7-9b8d-4e82-a7ee-572bd0512d19

📥 Commits

Reviewing files that changed from the base of the PR and between 8c65336 and 616cfa7.

📒 Files selected for processing (2)
  • packages/bruno-app/src/components/CodeEditor/index.js
  • packages/bruno-app/src/providers/Hotkeys/keyMappings.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/bruno-app/src/components/CodeEditor/index.js

Walkthrough

The CodeEditor comment-toggle shortcut is made dynamic: a new utility resolves platform-aware CodeMirror key strings from Redux hotkey preferences, and CodeEditor is connected to Redux to receive and apply the computed commentToggleKey (with forwardRef).

Changes

Cohort / File(s) Summary
Hotkey Mapping Configuration
packages/bruno-app/src/providers/Hotkeys/keyMappings.js
Added Editor keybindings (default toggleComment) and getCodeMirrorKeyForAction to merge user/default bindings, pick platform-specific combo, normalize modifiers/special keys, and return CodeMirror-compatible key strings (or null).
CodeEditor Redux Integration
packages/bruno-app/src/components/CodeEditor/index.js
Replaced hardcoded comment shortcut with a toggleComment method; compute/apply extraKeys initially and on prop updates; connect component to Redux via mapStateToProps to inject commentToggleKey; export connected component with forwardRef.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CodeMirror
    participant CodeEditor
    participant Redux

    User->>CodeMirror: press shortcut
    CodeMirror->>CodeEditor: invoke bound key handler
    CodeEditor->>Redux: read commentToggleKey (injected prop)
    Redux-->>CodeEditor: returns resolved CodeMirror key string
    CodeEditor-->>CodeMirror: toggleComment using JSON/LD+JSON tokens
    CodeMirror-->>User: comments toggled
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • helloanoop
  • lohit-bruno
  • naman-bruno
  • bijin-bruno
  • sid-bruno

Poem

Keys that once were fixed now flow,
Redux whispers which combos to know.
CodeMirror listens, toggles the line,
JSON comments fall into align.
Cheers to shortcuts, neat and fine! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: make CodeEditor toggle-comment shortcut configurable' clearly and concisely describes the main change: making a previously hardcoded shortcut configurable.
Linked Issues check ✅ Passed The PR successfully addresses issue #7874 by implementing a configurable toggle-comment shortcut, allowing users with different keyboard layouts to customize the binding.
Out of Scope Changes check ✅ Passed All changes are scoped to making the toggle-comment shortcut configurable: CodeEditor integration with Redux preferences, key mapping logic, and dynamic key binding updates.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a configurable keybinding for “Toggle Comment” in the CodeEditor, so users (e.g. with Nordic/ISO layouts) can customize the shortcut via Preferences instead of relying on a hard-coded Cmd-/ / Ctrl-/.

Changes:

  • Added a new Editor keybinding section with a toggleComment action.
  • Introduced getCodeMirrorKeyForAction to translate stored keybinding format into a CodeMirror extraKeys key string.
  • Wired CodeEditor to Redux preferences to apply the configured toggle-comment shortcut.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
packages/bruno-app/src/providers/Hotkeys/keyMappings.js Adds toggleComment default binding and a helper to convert stored keybindings to CodeMirror key strings.
packages/bruno-app/src/components/CodeEditor/index.js Switches CodeEditor export to a connected component and uses the configured key for toggleComment in CodeMirror extraKeys.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/bruno-app/src/components/CodeEditor/index.js Outdated
Comment thread packages/bruno-app/src/providers/Hotkeys/keyMappings.js
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/bruno-app/src/components/CodeEditor/index.js (1)

92-124: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Comment-toggle hotkey does not react to preference changes after mount.

extraKeys is initialized once (Line 92 onward), but commentToggleKey updates are never reconciled in componentDidUpdate (Line 223 onward). If the user changes keybindings while this editor instance is mounted, the old shortcut remains active.

Proposed fix
 class CodeEditor extends React.Component {
+  toggleComment = () => {
+    if (['application/ld+json', 'application/json'].includes(this.props.mode)) {
+      this.editor.toggleComment({ lineComment: '//', blockComment: '/*' });
+    } else {
+      this.editor.toggleComment();
+    }
+  };

   componentDidMount() {
@@
-    const toggleComment = () => {
-      if (['application/ld+json', 'application/json'].includes(this.props.mode)) {
-        this.editor.toggleComment({ lineComment: '//', blockComment: '/*' });
-      } else {
-        this.editor.toggleComment();
-      }
-    };
@@
-        ...(this.props.commentToggleKey ? { [this.props.commentToggleKey]: toggleComment } : {}),
+        ...(this.props.commentToggleKey ? { [this.props.commentToggleKey]: this.toggleComment } : {}),
@@
   componentDidUpdate(prevProps) {
@@
+    if (this.props.commentToggleKey !== prevProps.commentToggleKey && this.editor) {
+      const extraKeys = { ...(this.editor.getOption('extraKeys') || {}) };
+      if (prevProps.commentToggleKey) {
+        delete extraKeys[prevProps.commentToggleKey];
+      }
+      if (this.props.commentToggleKey) {
+        extraKeys[this.props.commentToggleKey] = this.toggleComment;
+      }
+      this.editor.setOption('extraKeys', extraKeys);
+    }
+
     this.ignoreChangeEvent = false;
   }

Also applies to: 223-280

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/bruno-app/src/components/CodeEditor/index.js` around lines 92 - 124,
The CodeEditor mounts extraKeys once (extraKeys) so changes to
props.commentToggleKey aren’t applied—update componentDidUpdate to detect when
this.props.commentToggleKey != prevProps.commentToggleKey and reconcile the
editor keymap by removing the old mapping and adding the new one (or rebuild and
call editor.setOption('extraKeys', updatedExtraKeys)); specifically update the
CodeEditor lifecycle method componentDidUpdate to modify the CodeMirror instance
(editor) to point the new key to toggleComment (and remove the previous key) so
the shortcut reflects preference changes while mounted.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/bruno-app/src/providers/Hotkeys/keyMappings.js`:
- Around line 184-189: getCodeMirrorKeyForAction currently assumes (isMac ?
binding.mac : binding.windows) is always a string and calls .split(), which can
throw if binding.mac/windows is undefined; update the function to guard this
value before splitting (e.g. compute const raw = isMac ? binding.mac :
binding.windows and if (!raw) return null or use a safe default like '') then
call raw.split('+bind+').filter(Boolean); refer to getCodeMirrorKeyForAction,
getMergedKeyBindings, binding and isMac to locate the change and ensure
consumers don't break on malformed persisted preferences.

---

Outside diff comments:
In `@packages/bruno-app/src/components/CodeEditor/index.js`:
- Around line 92-124: The CodeEditor mounts extraKeys once (extraKeys) so
changes to props.commentToggleKey aren’t applied—update componentDidUpdate to
detect when this.props.commentToggleKey != prevProps.commentToggleKey and
reconcile the editor keymap by removing the old mapping and adding the new one
(or rebuild and call editor.setOption('extraKeys', updatedExtraKeys));
specifically update the CodeEditor lifecycle method componentDidUpdate to modify
the CodeMirror instance (editor) to point the new key to toggleComment (and
remove the previous key) so the shortcut reflects preference changes while
mounted.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 292f7028-d442-499b-a9a3-2c4c05918e26

📥 Commits

Reviewing files that changed from the base of the PR and between 8269d51 and 8c65336.

📒 Files selected for processing (2)
  • packages/bruno-app/src/components/CodeEditor/index.js
  • packages/bruno-app/src/providers/Hotkeys/keyMappings.js

Comment thread packages/bruno-app/src/providers/Hotkeys/keyMappings.js
- Reconcile editor's extraKeys when commentToggleKey changes after mount
- Guard getCodeMirrorKeyForAction against missing platform binding
- Normalize special-key tokens (enter, esc, arrow*, etc.) to CodeMirror names

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

export const getCodeMirrorKeyForAction = (action, userKeyBindings) => {
const binding = getMergedKeyBindings(userKeyBindings)[action];
if (!binding) return null;
const isMac = navigator.platform.toLowerCase().includes('mac');
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

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

getCodeMirrorKeyForAction uses navigator.platform directly. This duplicates OS detection logic used elsewhere (utils/common/platform.isMacOS) and will throw if navigator is unavailable (e.g., certain test/runtime contexts). Consider switching to isMacOS() (or at least guarding with typeof navigator !== 'undefined') so this helper is safe and consistent across the app.

Suggested change
const isMac = navigator.platform.toLowerCase().includes('mac');
const platform = typeof navigator !== 'undefined' && typeof navigator.platform === 'string' ? navigator.platform : '';
const isMac = platform.toLowerCase().includes('mac');

Copilot uses AI. Check for mistakes.
Comment on lines +363 to +367
const mapStateToProps = (state) => ({
commentToggleKey: getCodeMirrorKeyForAction('toggleComment', state.app.preferences?.keyBindings)
});

export default connect(mapStateToProps, null, null, { forwardRef: true })(CodeEditor);
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

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

CodeEditor is now wrapped with react-redux connect, which means it cannot be rendered outside a Redux <Provider>. There is an existing Jest test that renders <CodeEditor /> without a store; this change will break that test (and any other standalone usage). Consider exporting the unconnected class (named export) for tests, or updating tests/consumers to wrap in a Provider with a minimal store.

Copilot uses AI. Check for mistakes.
Comment on lines +363 to +366
const mapStateToProps = (state) => ({
commentToggleKey: getCodeMirrorKeyForAction('toggleComment', state.app.preferences?.keyBindings)
});

Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

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

The app has a preferences.keybindingsEnabled toggle (used by HotkeysProvider/useKeybinding to disable shortcuts), but CodeEditor will still bind the comment-toggle shortcut even when keybindings are disabled. Since toggleComment is now part of KEY_BINDING_SECTIONS, consider also selecting keybindingsEnabled here and returning null for commentToggleKey when disabled so the preference toggle consistently disables this shortcut too.

Suggested change
const mapStateToProps = (state) => ({
commentToggleKey: getCodeMirrorKeyForAction('toggleComment', state.app.preferences?.keyBindings)
});
const mapStateToProps = (state) => {
const keybindingsEnabled = state.app.preferences?.keybindingsEnabled;
return {
commentToggleKey: keybindingsEnabled === false
? null
: getCodeMirrorKeyForAction('toggleComment', state.app.preferences?.keyBindings)
};
};

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't comment out using shortcut described in ealier release (Nordic/ISO layout)

2 participants