Add optional automatic bracket pairing to the code editor
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 48/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- javascript
- 领域
- frontend
调研方向
Start by reading web/editor.js for indentNewline and indentSelection, then web/interview.js for event handling and applyIndent. Define the setting, edit behavior, and pair tracking boundaries before implementation, including selection wrapping, delimiter skipping, paired deletion, persistence, IME and paste handling, and conservative comment or string behavior. Done means the requested bracket features preserve indentation and normal undo/redo behavior.
由索引模型根据 Issue 内容生成。
描述
Problem
While writing C++ code in CodeTrial, typing an opening delimiter such as (, {, or [ does not insert its matching closing delimiter. I need to type both sides manually.
During an interview, this adds repetitive typing and can interrupt the flow of explaining my solution while coding.
Current behavior
Typing ( produces:
(|
Here, | represents the cursor.
Expected behavior
With automatic bracket pairing enabled, typing ( should produce:
(|)
The same behavior should apply to {} and [].
This proposal concerns basic editing assistance, not code generation or solution suggestions.
Existing implementation
The current editor already supports indentation:
web/editor.jsprovidesindentNewlineandindentSelection.web/interview.jsconnects these helpers to editor events.indentNewlinerecognizes matching delimiters when arranging a newline between an existing pair, but does not insert a missing closing delimiter.
This feature could build on those editing helpers while remaining separate from automatic indentation.
Proposed behavior
- Add an “Auto-close brackets” setting.
- When enabled, insert the matching closing delimiter and leave the cursor between the pair.
- When text is selected, wrap the selection with the chosen pair.
- When the cursor is immediately before an automatically inserted closing delimiter, typing that delimiter should move past it instead of inserting a duplicate.
- Pressing Backspace between an untouched, automatically inserted empty pair should remove both characters.
- Preserve the existing newline indentation behavior.
The setting should be remembered across page reloads. My preference is to enable it by default while allowing users to disable it for plain-editor practice.
Suggested implementation
Editing helpers
Add pure functions to web/editor.js that compute the updated text and selection without directly changing the DOM.
For example, a helper could receive the current text, selection range, and opening delimiter, then return:
- Updated text.
- Updated selection start and end.
- Information about the automatically inserted pair, if applicable.
The delimiter mapping would be:
const BRACKET_PAIRS = {
"(": ")",
"[": "]",
"{": "}",
};
Event handling
In web/interview.js, handle direct delimiter input through beforeinput, alongside the existing newline handling.
The handler should:
- Respect the user setting.
- Ignore IME composition.
- Avoid transforming paste operations.
- Only intercept supported, cancelable input events.
- Apply edits through an undo-preserving editing path.
The existing applyIndent helper could be generalized to apply editor edits, rather than adding a separate implementation that directly replaces the textarea value.
Pair tracking
Track automatically inserted pairs so that closing-delimiter skipping and paired deletion do not accidentally affect unrelated, manually entered characters.
Update or invalidate tracked pairs after edits. Clear stale tracking when the editor content is replaced or the programming language changes.
Language context
Avoid inserting unwanted pairs inside comments or string literals.
The existing indentation helper does not maintain full multiline lexical state, so it should not be treated as a complete parser for this purpose. A conservative first implementation could limit pairing to contexts it can confidently identify.
Requested features
I would like the editor to support the following features:
- Automatically insert the matching closing bracket when typing
(,[, or{, with the cursor placed between the pair. - Wrap selected text in a bracket pair without replacing the selected content.
- Allow typing past an automatically inserted closing bracket without creating a duplicate.
- Remove both brackets when pressing Backspace inside an automatically inserted empty pair.
- Keep the existing automatic indentation when pressing Enter between braces.
- Provide a setting to enable or disable automatic bracket pairing.
It would also be helpful to preserve normal undo/redo behavior and avoid interfering with IME input, pasted text, comments, or strings.
Related work
Issue #61 discusses syntax-aware automatic indentation:
https://github.com/sysprog21/codetrial/issues/61
This proposal addresses inserting matching delimiters while typing, which is separate from indentation after pressing Enter.
- 主要语言
- Rust
- 星标
- 160
- 派生
- 64
- 平均合并
- 1 天 9 小时
- 30 天内合并 PR
- 114
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
sysprog21/codetrial 的其他 Issue
-
Highlight the active line in the code editor可能已有人在做 @ArthurArthurArthur0817 于 7 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
sysprog21/codetrial#247 · 3 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
Include local time in downloaded report filenames可能已有人在做 @106081011 今天认领。 未关闭
sysprog21/codetrial#309 · 1 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
Avoid automatic bracket pairing inside comments and strings可能已有人在做 @Jonathas2127 今天认领。 未关闭
sysprog21/codetrial#307 · 2 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
enhancement
难度 5/5 一周以上 新手友好度 15/100
维护者通常 1 天内回复
-
Preserve test run history可能已有人在做 @yunhsuan0510 今天认领。 未关闭
难度 3/5 1-2 天 新手友好度 62/100
sysprog21/codetrial#301 · 2 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
查看 sysprog21/codetrial 的全部 Issue
相似的 Issue
-
ai_p1 comp-message-queues
难度 2/5 1-3 小时 新手友好度 72/100
ClickHouse/ClickHouse#125890 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 66/100
storytold/photocraft#5710 ·
维护者通常 1 天内回复
-
Two tests fail on a Windows checkout: a CRLF parse in feature_walk_tests, an 8.3 TEMP path in libraries_tests可能已有人在做 @CamWight 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
bherbruck/solvecraft#1186 · 1 条评论 ·
-
area: screenshot audit bug good first issue priority: P3 security severity: info type: correctness
难度 2/5 1-3 小时 新手友好度 70/100
pierruno-labs/prowl#31 ·
-
难度 2/5 1-3 小时 新手友好度 70/100
limitlessprojects/Go-Hybrid#50 ·
维护者通常 1 天内回复