Skip to content

Commit 4d356a1

Browse files
committed
feat(editor): 命令面板新增「转到匹配括号」
用 @codemirror/language 的 matchBrackets 取光标前后括号的配对并跳转, 无匹配时提示;归入「代码」命令分组。
1 parent 609151f commit 4d356a1

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/App.vue

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ import {useLanguageRegistry} from './composables/useLanguageRegistry'
542542
import {useWorkspace} from './composables/useWorkspace'
543543
import {useTextCommands} from './composables/useTextCommands'
544544
import {useBookmarks} from './composables/useBookmarks'
545-
import {foldAll, unfoldAll} from '@codemirror/language'
545+
import {foldAll, unfoldAll, matchBrackets} from '@codemirror/language'
546546
import {useGitPermalink} from './composables/useGitPermalink'
547547
import {useRevealInTree} from './composables/useRevealInTree'
548548
import {useWorkspaceRoots} from './composables/useWorkspaceRoots'
@@ -1173,6 +1173,23 @@ const convertIndentation = (toTabs: boolean) => {
11731173
toast.success(t('app.indentConverted'))
11741174
}
11751175
1176+
// 转到匹配括号:取光标前后的括号,跳到其配对处
1177+
const goToMatchingBracket = () => {
1178+
const view = editorView.value
1179+
if (!view) {
1180+
return
1181+
}
1182+
const pos = view.state.selection.main.head
1183+
const m = matchBrackets(view.state, pos, -1) || matchBrackets(view.state, pos, 1)
1184+
if (m && m.matched && m.end) {
1185+
view.dispatch({selection: {anchor: m.end.from}, scrollIntoView: true})
1186+
view.focus()
1187+
}
1188+
else {
1189+
toast.info(t('app.noMatchingBracket'))
1190+
}
1191+
}
1192+
11761193
// AI 自然语言生成 / 选区改写
11771194
const showGenerate = ref(false)
11781195
const generateSelection = ref('')
@@ -2235,6 +2252,7 @@ const paletteCommands = computed<PaletteCommand[]>(() => [
22352252
{id: 'indentToTabs', label: t('command.indentToTabs'), group: t('command.groupText'), icon: Eraser, run: () => convertIndentation(true)},
22362253
{id: 'foldAll', label: t('command.foldAll'), group: t('command.groupCode'), icon: FoldVertical, run: () => { if (editorView.value) foldAll(editorView.value) }},
22372254
{id: 'unfoldAll', label: t('command.unfoldAll'), group: t('command.groupCode'), icon: UnfoldVertical, run: () => { if (editorView.value) unfoldAll(editorView.value) }},
2255+
{id: 'goToMatchingBracket', label: t('command.goToMatchingBracket'), group: t('command.groupCode'), icon: Code2, run: () => goToMatchingBracket()},
22382256
{id: 'toggleBookmark', label: t('command.toggleBookmark'), group: t('command.groupBookmark'), icon: Bookmark, hint: hintOf('toggleBookmark'), run: () => toggleBookmark()},
22392257
{id: 'nextBookmark', label: t('command.nextBookmark'), group: t('command.groupBookmark'), icon: Bookmark, run: () => nextBookmark()},
22402258
{id: 'prevBookmark', label: t('command.prevBookmark'), group: t('command.groupBookmark'), icon: Bookmark, run: () => prevBookmark()},

src/i18n/locales/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@
696696
"revealInTree": "Reveal in file tree",
697697
"foldAll": "Fold all",
698698
"unfoldAll": "Unfold all",
699+
"goToMatchingBracket": "Go to matching bracket",
699700
"groupCode": "Code",
700701
"toggleBookmark": "Toggle bookmark",
701702
"nextBookmark": "Next bookmark",
@@ -1336,6 +1337,7 @@
13361337
"pathCopied": "Path copied",
13371338
"copiedMarkdown": "Copied as Markdown code block",
13381339
"indentConverted": "Indentation converted",
1340+
"noMatchingBracket": "No matching bracket at cursor",
13391341
"clipboardEmpty": "Clipboard is empty",
13401342
"clipboardReadFailed": "Failed to read clipboard: ",
13411343
"copyFailed": "Copy failed: ",

src/i18n/locales/zh-CN.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@
696696
"revealInTree": "在文件树中定位",
697697
"foldAll": "折叠全部代码",
698698
"unfoldAll": "展开全部代码",
699+
"goToMatchingBracket": "转到匹配括号",
699700
"groupCode": "代码",
700701
"toggleBookmark": "切换书签",
701702
"nextBookmark": "下一处书签",
@@ -1336,6 +1337,7 @@
13361337
"pathCopied": "已复制路径",
13371338
"copiedMarkdown": "已复制为 Markdown 代码块",
13381339
"indentConverted": "已转换缩进",
1340+
"noMatchingBracket": "光标处没有可匹配的括号",
13391341
"clipboardEmpty": "剪贴板为空",
13401342
"clipboardReadFailed": "读取剪贴板失败: ",
13411343
"copyFailed": "复制失败: ",

0 commit comments

Comments
 (0)