From a28600c55a4b1a3595cc689967df4ce37deba763 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Thu, 29 Oct 2026 02:01:18 +0800 Subject: [PATCH] feat(editor): Show the enclosing declarations with Context Info (#161) View > Context Info (Ctrl+Shift+Q on macOS, Alt+Q on Windows) shows the class, method and block header lines around the caret in a hint, which helps when they are scrolled out of view. --- src-tauri/src/menu.rs | 1 + src/editor/core/actions.ts | 17 +++++++++++++++++ src/platform/keymaps/intellij-macos.ts | 1 + src/platform/keymaps/intellij-windows.ts | 1 + src/platform/keymaps/vscode-macos.ts | 1 + src/platform/locales/en.json | 1 + src/platform/locales/text/zh.json | 4 +++- src/platform/locales/zh.json | 1 + 8 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src-tauri/src/menu.rs b/src-tauri/src/menu.rs index 360a02740..909118a44 100644 --- a/src-tauri/src/menu.rs +++ b/src-tauri/src/menu.rs @@ -442,6 +442,7 @@ pub fn spec(os: Os) -> Vec { act("code.quickDocumentation", "Quick Documentation"), act("code.expressionTypeInfo", "Expression Type Info"), act("code.showErrorDescription", "Error Description"), + act("editor.contextInfo", "Context Info"), act("code.showIntentions", "Show Context Actions"), act("code.generate", "Generate…"), act("code.overrideMethods", "Override Methods…"), diff --git a/src/editor/core/actions.ts b/src/editor/core/actions.ts index d2304b86f..75a52f743 100644 --- a/src/editor/core/actions.ts +++ b/src/editor/core/actions.ts @@ -52,6 +52,7 @@ import { } from "./cm/folding"; import { reverseLines, sortLines } from "./cm/lines"; import { customRegions } from "./cm/regions"; +import { enclosingHeaders } from "./cm/stickyScroll"; import { moveElementLeft, moveElementRight } from "./cm/moveElement"; import { caretAnchor, openPopup } from "@/editor/lsp/popupStore"; import { @@ -328,6 +329,18 @@ function completeCurrentStatement(view: EditorView, slot: EditorSlot) { }); } +/** + * View › Context Info (⌃⇧Q / Alt+Q): the declaration lines around the caret + * (class, method, block headers), most useful when they are scrolled away. + */ +function showContextInfo(view: EditorView) { + const state = view.state; + const line = state.doc.lineAt(state.selection.main.head).number; + const headers = enclosingHeaders(state, line); + const text = headers.map((n) => state.doc.line(n).text.trimEnd()).join("\n"); + void executeAction("editor.showHint", { text: text || "No context information here" }); +} + /** Navigate › Custom Folding (⌥⌘.): the file's custom regions; picking one moves the caret to it. */ function showCustomRegions(view: EditorView) { const regions = customRegions(view.state.doc); @@ -574,6 +587,10 @@ export function registerEditorActions(): () => void { category: "Editor Actions", }, ), + text("editor.contextInfo", "Context Info", (v) => showContextInfo(v), { + category: "View", + keywords: ["enclosing method", "enclosing class", "where am I"], + }), text("navigate.customRegion", "Custom Folding…", (v) => showCustomRegions(v), { category: "Navigate", keywords: ["region", "editor-fold", "go to region"], diff --git a/src/platform/keymaps/intellij-macos.ts b/src/platform/keymaps/intellij-macos.ts index d8af5b09c..e19f23231 100644 --- a/src/platform/keymaps/intellij-macos.ts +++ b/src/platform/keymaps/intellij-macos.ts @@ -123,6 +123,7 @@ export const intellijMacos: Record = { "editor.codeBlockEnd": ["Meta+Alt+]"], "editor.codeBlockStartWithSelection": ["Meta+Alt+Shift+["], "editor.codeBlockEndWithSelection": ["Meta+Alt+Shift+]"], + "editor.contextInfo": ["Ctrl+Shift+Q"], "navigate.customRegion": ["Meta+Alt+."], "code.expandToLevel1": ["Meta+* 1"], "code.expandToLevel2": ["Meta+* 2"], diff --git a/src/platform/keymaps/intellij-windows.ts b/src/platform/keymaps/intellij-windows.ts index 3c1ef2a9a..ace2711b2 100644 --- a/src/platform/keymaps/intellij-windows.ts +++ b/src/platform/keymaps/intellij-windows.ts @@ -124,6 +124,7 @@ export const intellijWindows: Record = { "editor.codeBlockEnd": ["Ctrl+]"], "editor.codeBlockStartWithSelection": ["Ctrl+Shift+["], "editor.codeBlockEndWithSelection": ["Ctrl+Shift+]"], + "editor.contextInfo": ["Alt+Q"], "navigate.customRegion": ["Ctrl+Alt+."], "code.expandToLevel1": ["Ctrl+* 1"], "code.expandToLevel2": ["Ctrl+* 2"], diff --git a/src/platform/keymaps/vscode-macos.ts b/src/platform/keymaps/vscode-macos.ts index a3f2b5e04..a0ed485e2 100644 --- a/src/platform/keymaps/vscode-macos.ts +++ b/src/platform/keymaps/vscode-macos.ts @@ -28,6 +28,7 @@ export const vscodeMacos: Record = { "navigate.callHierarchy": ["Alt+Shift+H"], "navigate.showFilePath": [], "navigate.customRegion": [], + "editor.contextInfo": [], // editor.action.wordHighlight.next / prev "navigate.nextHighlightedUsage": ["F7"], "navigate.prevHighlightedUsage": ["Shift+F7"], diff --git a/src/platform/locales/en.json b/src/platform/locales/en.json index 3c2084be7..8c4182d0b 100644 --- a/src/platform/locales/en.json +++ b/src/platform/locales/en.json @@ -431,6 +431,7 @@ "menu.code.expandAllToLevel4": "4", "menu.code.expandToLevel5": "5", "menu.code.expandAllToLevel5": "5", + "menu.editor.contextInfo": "Context Info", "run.services.configurationCount": "{count} configurations", "search.bookmarks.count": "{count, plural, one {# bookmark} other {# bookmarks}}", "settings.zoom.hint": "Change with {zoomIn} or {zoomOut}. Set to 100% with {reset}" diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 3c4ed8d72..76a1b4963 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -4605,5 +4605,7 @@ "Copies the change at the caret from the left side to the right one": "将光标处的变更从左侧复制到右侧", "Copies the change at the caret from the right side to the left one": "将光标处的变更从右侧复制到左侧", "Compare Next File": "比较下一个文件", - "Compare Previous File": "比较上一个文件" + "Compare Previous File": "比较上一个文件", + "Context Info": "上下文信息", + "No context information here": "此处没有上下文信息" } diff --git a/src/platform/locales/zh.json b/src/platform/locales/zh.json index da443b0c9..52855c677 100644 --- a/src/platform/locales/zh.json +++ b/src/platform/locales/zh.json @@ -431,6 +431,7 @@ "menu.code.expandAllToLevel4": "4", "menu.code.expandToLevel5": "5", "menu.code.expandAllToLevel5": "5", + "menu.editor.contextInfo": "上下文信息", "run.services.configurationCount": "{count} 个配置", "search.bookmarks.count": "{count} 个书签", "settings.zoom.hint": "使用 {zoomIn} 或 {zoomOut} 更改,使用 {reset} 重置为 100%"