diff --git a/.agents/notes/implemented/feature/2026-11-16-copy-editor-reference-location.md b/.agents/notes/implemented/feature/2026-11-16-copy-editor-reference-location.md new file mode 100644 index 0000000000..e894253f36 --- /dev/null +++ b/.agents/notes/implemented/feature/2026-11-16-copy-editor-reference-location.md @@ -0,0 +1,42 @@ +# Agent 笔记:编辑器复制代码引用位置 + +状态:已实现 + +## 先说结论 + +Viva 编辑器的右键菜单提供复制源码位置,默认把所点行复制到剪贴板。有语言服务器同步提供的文档符号时,Rust Core 把包、类、方法及原始签名组成引用;没有可靠符号时只复制文件路径和行号,不推测源码。macOS 与 Windows 共用实现,避免两端产生不同格式。 + +## 问题 + +#2015 需要从编辑器快速得到方法引用及行号。原有标签页“复制路径/引用”只处理文件路径;从文件名和源码文本自己猜方法签名会在重载方法、嵌套类和未保存编辑时产生错误引用。 + +## 决策 + +- 使用语言服务器协议(LSP)的 `documentSymbols`:先确认当前文档已同步,随后由现有异步请求链获取符号;优先使用与文档修订相同的 Structure 缓存。 +- 桌面端只捕获右键坐标、管理请求生命周期并写入系统剪贴板;格式规则属于 Rust Core 的 `editor.copyReferenceLocation`。 +- Core 只消费上游提供的符号名称、类型、作用域及方法签名,没有可靠信息就按 `文件路径#L行号` 回退。用户看到的一基行号由 Core 统一处理。 +- 旧文档或项目、关闭的标签、后续编辑以及新一次复制都不能接收过期操作。插件会话失败与缺少符号不阻断文件路径回退。 +- 等语言服务器的文档符号最多 3 秒(`referenceSymbolsTimeout`)。提交请求有 `semanticStartTimeout`,但回答没有期限;正在索引或卡住的服务器会让菜单项点了没有结果。超时后取消请求、记录日志,按没有服务器时的规则复制 `文件路径#L行号`。 +- 右键坐标只属于它打开的那一次菜单:选中“复制源码位置”时使用;菜单没选这一项就关闭(Esc、点别处、选其他项)时丢弃。之后用 Shift+F10 打开的菜单读取光标位置,不会复制很久以前右键的行。 +- 菜单复用现有共享 i18n 的“复制源码位置”(`native.copySourceLocation`),而不是在桌面端增加无法生成的硬编码界面文案。 + +## 考虑过的备选方案 + +- **直接解析 Java 源文件提取类/方法**:不采用。需要为每门语言维护解析规则,还会误识别重载方法与泛型,违反 Core 不特判语言的约束。 +- **直接复制文件行号,不请求 LSP**:只作为缺失服务器的回退;否则无法实现 Issue 要求的符号级引用。 +- **把格式规则写入 Go/Viva**:不采用。相同功能在两个平台的语义必须由共享 Core 定义。 + +## 后果 + +右键复制不启动新的常驻服务,也不阻塞编辑器线程。符号质量取决于语言服务器提供的名字与签名;有些服务器不提供包名或方法参数时,输出会相应变短。未保存修改尚未完成 LSP 同步时,优先保证位置正确并回退文件路径,不能拿旧符号当新信息。 + +## 验证 + +- Core: `cargo test -p lithe-editor`;`cargo test -p lithe-core copy_reference_location`。 +- Viva: `scripts/desktop/desktop-go.sh test ./internal/workbench -run 'TestCopyReferenceLocation|TestEditorContextMenuCopiesReference|TestEditorContextMenuForgetsPress' -count=1`,其中包括服务器不回答时超时回退,以及右键菜单关闭后改用光标位置。 +- 视觉: `LITHE_VISUAL_OUTPUT_DIR=.artifacts/issue-2015 scripts/desktop/desktop-go.sh test -tags 'vivahead lithevisual' ./internal/workbench -run '^TestVisualCopyReferenceLocation$' -count=1`。 +- 两个平台原生集成与粘贴结果仍需实际执行,不能以静态源代码检查替代。 + +## 适用范围 + +`desktop/internal/workbench/copy_reference.go`、`desktop/internal/workbench/editor_menu.go`、`desktop/internal/core/reference_location.go`、`rust/crates/editor/src/reference_location.rs`、`rust/contracts/rust-core-api.md`。 diff --git a/desktop/internal/core/reference_location.go b/desktop/internal/core/reference_location.go new file mode 100644 index 0000000000..ab880deafb --- /dev/null +++ b/desktop/internal/core/reference_location.go @@ -0,0 +1,29 @@ +package core + +import "context" + +// ReferenceLocationRequest is a snapshot of the requested caret and the LSP +// symbols synchronized to that document revision. Coordinates are zero-based +// UTF-16 positions, while the result's human-readable line is one-based. +type ReferenceLocationRequest struct { + Path string `json:"path"` + Line int `json:"line"` + UTF16Column int `json:"utf16Column"` + Symbols []DocumentSymbol `json:"symbols"` +} + +type ReferenceLocationResult struct { + Reference string `json:"reference"` + Source string `json:"source"` +} + +// CopyReferenceLocation uses the shared Rust Core formatter. The desktop host +// does not interpret package, type, method names or signature conventions. +func (c *Client) CopyReferenceLocation(ctx context.Context, request ReferenceLocationRequest) (ReferenceLocationResult, error) { + if request.Symbols == nil { + request.Symbols = []DocumentSymbol{} + } + var result ReferenceLocationResult + err := c.Execute(ctx, "editor.copyReferenceLocation", request, &result) + return result, err +} diff --git a/desktop/internal/workbench/copy_reference.go b/desktop/internal/workbench/copy_reference.go new file mode 100644 index 0000000000..8ef6211ad1 --- /dev/null +++ b/desktop/internal/workbench/copy_reference.go @@ -0,0 +1,196 @@ +package workbench + +import ( + "context" + "time" + + "github.com/1lck/Lithe-IDEA/desktop/internal/core" + "github.com/1lck/Lithe-IDEA/desktop/internal/i18n" + "github.com/vivaui/viva/app" +) + +// A secondary press chooses the line under the pointer, not the previous +// keyboard caret. The press belongs to the menu it opens: the menu's item +// consumes the coordinate after the popup closes, and a menu closed without it +// drops the coordinate (referenceMenuPainted), so a menu Shift+F10 opens later +// reads the caret. Each operation owns a revision and an identity token. +type referenceClick struct { + documentID int + line, column int + // menuShown says the menu the press opened has been painted. + menuShown bool +} + +type referenceRequest struct { + project *project + documentID, revision int + line, column int + path string + pending *semanticPending + // wait ends the wait for pending's symbols (awaitReferenceSymbols); it is stopped when + // the symbols arrive or the request is replaced. + wait *time.Timer +} + +// referenceSymbolsTimeout bounds how long copying waits for the language server's symbols. +// Submitting the request is bounded by semanticStartTimeout, but nothing bounds the answer: a +// server that is indexing or stuck would leave the menu choice without a result. Past this +// wait the request is cancelled and the file and line are copied, as without a server. +const referenceSymbolsTimeout = 3 * time.Second + +type copyReferenceState struct { + click *referenceClick + request *referenceRequest + // symbolsWait overrides referenceSymbolsTimeout for tests. + symbolsWait time.Duration +} + +type referenceKernel interface { + CopyReferenceLocation(context.Context, core.ReferenceLocationRequest) (core.ReferenceLocationResult, error) +} + +func (a *App) copyReferenceLocation(p *project, d *document) { + if p == nil || d == nil || d.virtual || d.path == "" || a.project != p { + return + } + kernel, ok := a.kernel.(referenceKernel) + if !ok { + return + } + a.performed("editor.copyReferenceLocation", surfaceContextMenu) + line, column := d.view.CaretUTF16() + if click := a.editor.copyReference.click; click != nil && click.documentID == d.id { + line, column = click.line, click.column + } + a.editor.copyReference.click = nil + if previous := a.editor.copyReference.request; previous != nil { + previous.stopWait() + if previous.pending != nil { + a.cancelSemantic(previous.pending) + } + } + request := &referenceRequest{ + project: p, documentID: d.id, revision: d.view.Revision(), + line: line, column: column, path: d.path, + } + a.editor.copyReference.request = request + + // A loaded Structure result can be used only for exactly this document + // revision. Otherwise ask the same LSP documentSymbols feature directly; + // never use a stale tree from a previous document or unsaved edit. + outline := &a.editor.outline + if outline.project == p && outline.documentID == d.id && outline.loaded && + outline.revision == request.revision { + a.finishReferenceLocation(request, outline.symbols, kernel) + return + } + server, offered := a.editorServer(d, featureDocumentSymbols) + if offered && server != nil && a.semanticDocumentReady(d) { + request.pending = a.submitEditorRequest(pluginSemanticCopyReference, p, d, server, core.PluginSemanticRequest{}) + a.awaitReferenceSymbols(request) + return + } + // An unavailable or unsynchronized language server does not block copying: + // the shared Core produces a path reference instead of guessing symbols. + a.finishReferenceLocation(request, nil, kernel) +} + +// referenceMenuPainted follows the editor menu of d in the paint that declared it: a press on +// d is kept while the menu it opened shows and dropped once that menu has closed. +func (a *App) referenceMenuPainted(d *document, shown bool) { + click := a.editor.copyReference.click + if click == nil || click.documentID != d.id { + return + } + switch { + case shown: + click.menuShown = true + case click.menuShown: + a.editor.copyReference.click = nil + } +} + +// awaitReferenceSymbols cancels request's symbols request once referenceSymbolsTimeout passes +// without an answer and copies the reference without symbols. +func (a *App) awaitReferenceSymbols(request *referenceRequest) { + wait := a.editor.copyReference.symbolsWait + if wait <= 0 { + wait = referenceSymbolsTimeout + } + pending := request.pending + request.wait = time.AfterFunc(wait, func() { + a.post(func(a *App) { + if a.editor.copyReference.request != request || request.pending != pending { + return + } + a.backgroundFailed("editor_copy_reference_symbols", context.DeadlineExceeded) + a.cancelSemantic(pending) + a.applyReferenceSymbols(pending, core.BackendEvent{}, true) + }) + }) +} + +// stopWait ends the wait for the symbols, if one is running. +func (r *referenceRequest) stopWait() { + if r.wait != nil { + r.wait.Stop() + r.wait = nil + } +} + +// applyReferenceSymbols is reached through the ordinary semantic event router. +// Failed, unsupported or malformed symbols retain a useful file/line fallback. +func (a *App) applyReferenceSymbols(pending *semanticPending, event core.BackendEvent, failed bool) { + request := a.editor.copyReference.request + if request == nil || request.pending != pending { + return + } + request.pending = nil + request.stopWait() + p, d := a.currentSemanticDocument(request.documentID) + if p != request.project || d == nil || d.view.Revision() != request.revision { + // The symbols describe a text the user has since changed or closed: nothing is copied. + a.editor.copyReference.request = nil + return + } + var symbols []core.DocumentSymbol + if !failed { + var err error + symbols, err = core.PluginDocumentSymbols(event) + if err != nil { + a.backgroundFailed("editor_copy_reference_symbols", err) + } + } + if kernel, ok := a.kernel.(referenceKernel); ok { + a.finishReferenceLocation(request, symbols, kernel) + } +} + +func (a *App) finishReferenceLocation(request *referenceRequest, symbols []core.DocumentSymbol, kernel referenceKernel) { + a.background(request.project, kernelCallTimeout, func(ctx context.Context) func(*App) { + result, err := kernel.CopyReferenceLocation(ctx, core.ReferenceLocationRequest{ + Path: request.path, Line: request.line, UTF16Column: request.column, Symbols: symbols, + }) + return func(a *App) { + if a.editor.copyReference.request != request || a.project != request.project { + return + } + d := request.project.docs.find(request.documentID) + if d == nil || d.view.Revision() != request.revision || + request.project.selectedDocument(request.project.docs.group) != d { + a.editor.copyReference.request = nil + return + } + a.editor.copyReference.request = nil + if err != nil { + a.operationFailed("editor_copy_reference", err) + a.notifyError(a.text.Errors.Core(err.Error())) + return + } + if err := app.SetClipboardText(result.Reference); err != nil { + a.operationFailed("clipboard_copy", err) + a.notifyError(i18n.SharedFormat(a.language(), "desktop.files.copyFailed", map[string]string{"reason": err.Error()})) + } + } + }) +} diff --git a/desktop/internal/workbench/copy_reference_test.go b/desktop/internal/workbench/copy_reference_test.go new file mode 100644 index 0000000000..a03f4c63de --- /dev/null +++ b/desktop/internal/workbench/copy_reference_test.go @@ -0,0 +1,171 @@ +package workbench + +import ( + "os" + "path/filepath" + "strconv" + "strings" + "testing" + "time" + + "github.com/1lck/Lithe-IDEA/desktop/internal/core" + "github.com/vivaui/viva/gfx" +) + +func TestCopyReferenceLocationWithoutServerCopiesFileLine(t *testing.T) { + h := newHarness(t, &testKernel{Client: core.NewClient()}) + root := t.TempDir() + h.open(root) + path := filepath.Join(root, "reference.go") + if err := os.WriteFile(path, []byte("package main\nfunc work() {}\n"), 0600); err != nil { + t.Fatal(err) + } + p := h.app.project + h.app.openFile(p, path) + h.settleChain() + d := p.docs.byPath(path) + if d == nil { + t.Fatal("document not opened") + } + d.view.GoToLine(2) + h.app.copyReferenceLocation(p, d) + h.await("copy file and line", func() bool { + return h.driver.Clipboard() == path+"#L2" + }) +} + +func TestCopyReferenceLocationUsesLanguageSymbols(t *testing.T) { + f := newSemanticFixture(t) + f.server.features = map[string]bool{featureDocumentSymbols: true} + f.d.view.SetSelection(0, 0) + f.h.app.copyReferenceLocation(f.p, f.d) + f.h.await("reference symbol request submitted", func() bool { + req := f.h.app.editor.copyReference.request + return req != nil && req.pending != nil && req.pending.operation != "" + }) + pending := f.h.app.editor.copyReference.request.pending + f.deliver(t, pending.operation, pluginMethodDocumentSymbols, symbolsResult(t, + symbolAt("DemoClass", 5, 0, 1, + symbolAt("goHome(String,Integer)", 6, 0, 1)), + )) + f.h.await("symbol reference request settled", func() bool { + return f.h.app.editor.copyReference.request == nil + }) + if got := f.h.driver.Clipboard(); got != "DemoClass#goHome(String,Integer)#L1" { + t.Fatalf("copied %q, want method reference", got) + } +} + +func TestCopyReferenceLocationDiscardsOldDocumentRevision(t *testing.T) { + f := newSemanticFixture(t) + f.server.features = map[string]bool{featureDocumentSymbols: true} + f.h.app.copyReferenceLocation(f.p, f.d) + f.h.await("stale-reference symbol request submitted", func() bool { + req := f.h.app.editor.copyReference.request + return req != nil && req.pending != nil && req.pending.operation != "" + }) + pending := f.h.app.editor.copyReference.request.pending + f.h.app.insertForTest(f.d, "changed") + f.deliver(t, pending.operation, pluginMethodDocumentSymbols, symbolsResult(t, + symbolAt("Outdated", 5, 0, 1), + )) + f.h.settleChain() + if got := f.h.driver.Clipboard(); strings.Contains(got, "Outdated") { + t.Fatalf("stale reference copied: %q", got) + } + if f.h.app.editor.copyReference.request != nil { + t.Fatal("a reference request for an edited document stays pending") + } +} + +func TestEditorContextMenuCopiesReferenceAtSecondaryPress(t *testing.T) { + h := newHarness(t, &testKernel{Client: core.NewClient()}) + root := t.TempDir() + h.open(root) + path := filepath.Join(root, "reference.txt") + if err := os.WriteFile(path, []byte("one\ntwo\nthree\n"), 0600); err != nil { + t.Fatal(err) + } + p := h.app.project + h.app.openFile(p, path) + h.settleChain() + d := p.docs.byPath(path) + if d == nil { + t.Fatal("document missing") + } + bounds := h.topHit(d.hit) + at := center(bounds) + line, _, ok := d.view.PositionAt(at.X, at.Y) + if !ok { + t.Fatal("editor has not painted") + } + h.rightClickAt(bounds) + h.clickNamed(h.shared("native.copySourceLocation")) + h.await("right-click location copied", func() bool { + return h.driver.Clipboard() == path+"#L"+strconv.Itoa(line+1) + }) +} + +// A language server that never answers does not hold the copy back: once the wait for its +// symbols runs out, the request is cancelled and the file and line are copied, as without a +// server. +func TestCopyReferenceLocationFallsBackWhenSymbolsNeverArrive(t *testing.T) { + f := newSemanticFixture(t) + f.server.features = map[string]bool{featureDocumentSymbols: true} + f.h.app.editor.copyReference.symbolsWait = time.Millisecond + f.d.view.SetSelection(0, 0) + f.h.app.copyReferenceLocation(f.p, f.d) + f.h.await("file and line copied without symbols", func() bool { + return f.h.driver.Clipboard() == f.d.path+"#L1" + }) + if f.h.app.editor.copyReference.request != nil { + t.Fatal("the reference request stays after its fallback was copied") + } + if len(f.h.app.semantics.pending) != 0 { + t.Fatalf("the unanswered symbols request stays pending: %+v", f.h.app.semantics.pending) + } +} + +// A secondary press belongs to the menu it opens. Once that menu closes without a choice, a +// menu opened from the keyboard copies the caret's line, not the old pointer position. +func TestEditorContextMenuForgetsPressAfterItCloses(t *testing.T) { + h := newHarness(t, &testKernel{Client: core.NewClient()}) + root := t.TempDir() + h.open(root) + path := filepath.Join(root, "reference.txt") + if err := os.WriteFile(path, []byte("one\ntwo\nthree\n"), 0600); err != nil { + t.Fatal(err) + } + p := h.app.project + h.app.openFile(p, path) + h.settleChain() + d := p.docs.byPath(path) + if d == nil { + t.Fatal("document missing") + } + bounds := h.topHit(d.hit) + at := center(bounds) + line, _, ok := d.view.PositionAt(at.X, at.Y) + if !ok || line == 0 { + t.Fatalf("the press lands on line %d (painted %v); want a line after the first", line, ok) + } + h.rightClickAt(bounds) + if err := h.driver.Key(gfx.KeyEscape, 0); err != nil { + t.Fatal(err) + } + h.settle() + if h.shows(h.shared("native.copySourceLocation")) { + t.Fatal("Escape left the editor menu open") + } + d.view.SetSelection(0, 0) + h.app.focusDocument(d.id) + h.settle() + if err := h.driver.Key(gfx.KeyF10, gfx.ModifierShift); err != nil { + t.Fatal(err) + } + h.settle() + h.clickNamed(h.shared("native.copySourceLocation")) + h.await("caret line copied", func() bool { + return h.driver.Clipboard() == path+"#L1" + }) +} diff --git a/desktop/internal/workbench/copy_reference_visual_test.go b/desktop/internal/workbench/copy_reference_visual_test.go new file mode 100644 index 0000000000..489588a37c --- /dev/null +++ b/desktop/internal/workbench/copy_reference_visual_test.go @@ -0,0 +1,113 @@ +//go:build vivahead && lithevisual + +package workbench + +import ( + "os" + "path/filepath" + "testing" + + "github.com/1lck/Lithe-IDEA/desktop/internal/core" + "github.com/1lck/Lithe-IDEA/desktop/internal/store" + "github.com/vivaui/viva/app" + "github.com/vivaui/viva/headless" +) + +// TestVisualCopyReferenceLocation captures the actual editor's secondary-click +// menu and the result pasted into an editor via the primary+V shortcut. +// Dark/light x Chinese/English are independent sessions and OS-free. +func TestVisualCopyReferenceLocation(t *testing.T) { + out := os.Getenv("LITHE_VISUAL_OUTPUT_DIR") + if out == "" { + t.Skip("set LITHE_VISUAL_OUTPUT_DIR") + } + if err := os.MkdirAll(out, 0755); err != nil { + t.Fatal(err) + } + for _, look := range []struct{ theme, language string }{ + {"dark", "zh-Hans"}, {"dark", "en"}, + {"light", "zh-Hans"}, {"light", "en"}, + } { + t.Run(look.theme+"-"+look.language, func(t *testing.T) { + prefs := store.At(t.TempDir()) + if err := prefs.Save(settingsFile, settings{ + Theme: themeChoice(look.theme), Language: look.language, + DisableAutoSave: true, + }); err != nil { + t.Fatal(err) + } + root := filepath.Join(t.TempDir(), "ReferenceDemo") + if err := os.MkdirAll(root, 0700); err != nil { + t.Fatal(err) + } + path := filepath.Join(root, "DemoClass.java") + source := "package com.example;\n\npublic class DemoClass {\n void goHome(String city, Integer count) {\n System.out.println(city);\n }\n}\n" + if err := os.WriteFile(path, []byte(source), 0600); err != nil { + t.Fatal(err) + } + var session *headless.Session + h := newHarnessWithDriver(t, Options{ + Kernel: &testKernel{Client: core.NewClient()}, + Store: prefs, Languages: []string{look.language}, + }, func(config app.Config) (*app.Driver, func(), error) { + config.Width, config.Height = 1440, 900 + var err error + session, err = headless.Open(config, nil, headless.Options{}) + if err != nil { + return nil, nil, err + } + return session.Driver(), session.Close, nil + }) + h.open(root) + p := h.app.project + h.app.openFile(p, path) + h.settleChain() + d := p.docs.byPath(path) + if d == nil { + t.Fatal("editor not opened") + } + d.view.GoToLine(5) + h.app.focusDocument(d.id) + h.settleChain() + h.rightClickAt(h.topHit(d.hit)) + if !h.shows(h.shared("native.copySourceLocation")) { + t.Fatal("reference copy action missing from context menu") + } + if err := session.Screenshot(filepath.Join(out, "reference-menu-"+look.theme+"-"+look.language+".png")); err != nil { + t.Fatal(err) + } + // PositionAt the secondary press controls the reference's line, not + // the caret set above. Copying a plain file is a valid fallback. + h.clickNamed(h.shared("native.copySourceLocation")) + h.await("reference on clipboard", func() bool { + return len(h.driver.Clipboard()) >= len(path)+3 && h.driver.Clipboard()[:len(path)+2] == path+"#L" + }) + reference := h.driver.Clipboard() + if reference[:len(path)] != path || reference[len(path):len(path)+2] != "#L" { + t.Fatalf("unexpected clipboard reference %q", reference) + } + resultPath := filepath.Join(root, "pasted-result.txt") + if err := os.WriteFile(resultPath, []byte(""), 0600); err != nil { + t.Fatal(err) + } + h.app.openFile(p, resultPath) + h.settleChain() + resultDoc := p.docs.byPath(resultPath) + if resultDoc == nil { + t.Fatal("result editor not opened") + } + h.app.focusDocument(resultDoc.id) + h.settleChain() + handled, err := h.driver.ShortcutChord(primaryModifier(), 'v') + if err != nil || !handled { + t.Fatalf("clipboard paste shortcut handled=%v error=%v", handled, err) + } + h.await("paste result appears", func() bool { + return resultDoc.view.TextSnapshot().String() == reference + }) + if err := session.Screenshot(filepath.Join(out, "reference-pasted-"+look.theme+"-"+look.language+".png")); err != nil { + t.Fatal(err) + } + }) + } +} diff --git a/desktop/internal/workbench/editor_features.go b/desktop/internal/workbench/editor_features.go index a28c3cae81..a77265031e 100644 --- a/desktop/internal/workbench/editor_features.go +++ b/desktop/internal/workbench/editor_features.go @@ -6,10 +6,11 @@ package workbench type editorFeatures struct { nav navigationHistoryState // requests counts the editor's own language requests, which name them (editor_requests.go). - requests uint64 - outline outlineState - refs referencesState - encoding encodingState - lsp lspLogState - tokens semanticTokensState + requests uint64 + outline outlineState + copyReference copyReferenceState + refs referencesState + encoding encodingState + lsp lspLogState + tokens semanticTokensState } diff --git a/desktop/internal/workbench/editor_menu.go b/desktop/internal/workbench/editor_menu.go index e952b8b440..16c2e56aae 100644 --- a/desktop/internal/workbench/editor_menu.go +++ b/desktop/internal/workbench/editor_menu.go @@ -49,6 +49,10 @@ func (a *App) editorMenuItems(c ui.C, p *project, d *document) func(*ui.MenuItem } } menu.Separator() + if menu.Item(i18n.Shared(language, "native.copySourceLocation")).Clicked() { + c.After(func() { a.copyReferenceLocation(p, d) }) + } + menu.Separator() if menu.Item(i18n.Shared(language, "desktop.editorMenu.toggleComment")).Keys(primary, "/").Disabled(readOnly).Clicked() { c.After(func() { a.runLineOperation(p, d, core.ToggleLineComment) }) } diff --git a/desktop/internal/workbench/editor_requests.go b/desktop/internal/workbench/editor_requests.go index d1d35a928c..d414c97430 100644 --- a/desktop/internal/workbench/editor_requests.go +++ b/desktop/internal/workbench/editor_requests.go @@ -16,6 +16,7 @@ const ( // pluginSemanticDocumentSymbols and pluginSemanticTokens are the operations of the // outline and the semantic colors; their methods are what Core reports on completion. pluginSemanticDocumentSymbols = "documentSymbols" + pluginSemanticCopyReference = "copyReferenceSymbols" pluginSemanticTokens = "semanticTokens" pluginMethodDocumentSymbols = "textDocument/documentSymbol" pluginMethodSemanticTokens = "textDocument/semanticTokens/full" @@ -56,6 +57,9 @@ func (a *App) submitEditorRequest(kind semanticKind, p *project, d *document, se } a.semantics.pending = append(a.semantics.pending, pending) request.Operation, request.URI = string(kind), d.sync.uri + if kind == pluginSemanticCopyReference { + request.Operation = pluginSemanticDocumentSymbols + } a.background(p, semanticStartTimeout, func(ctx context.Context) func(*App) { operation, err := a.plugins.RequestLanguageSession(ctx, server.session, request) return func(a *App) { a.semanticRequestStarted(pending, operation, err, ctx.Err()) } @@ -74,6 +78,8 @@ func (a *App) applyEditorEvent(pending *semanticPending, event core.BackendEvent switch pending.kind { case pluginSemanticDocumentSymbols: a.applyOutline(pending, event, failed) + case pluginSemanticCopyReference: + a.applyReferenceSymbols(pending, event, failed) case pluginSemanticTokens: a.applySemanticTokens(pending, event, failed) case pluginSemanticFoldingRanges: diff --git a/desktop/internal/workbench/editor_view.go b/desktop/internal/workbench/editor_view.go index d0988e343a..02e19f077f 100644 --- a/desktop/internal/workbench/editor_view.go +++ b/desktop/internal/workbench/editor_view.go @@ -180,7 +180,12 @@ func (a *App) documentView(c ui.C, group string, d *document) { a.linkSync(c, p, d) } if p := a.project; p != nil && !d.virtual { - ui.ContextMenu(c.Key("menu"+name), box, a.editorMenuItems(c, p, d)) + items, shown := a.editorMenuItems(c, p, d), false + ui.ContextMenu(c.Key("menu"+name), box, func(menu *ui.MenuItems) { + shown = true + items(menu) + }) + a.referenceMenuPainted(d, shown) at := d.actionPanel.menuAt ui.ContextMenuAt(c.Key("lanemenu"+name), box, &d.actionPanel.menuOpen, gfx.Point{X: at.X, Y: at.Y + at.Height}, a.editorActionLaneMenu(c, p, d)) a.gitGutterPeek(c, p, d, box) @@ -257,6 +262,11 @@ func (a *App) documentKey(p *project, d *document, event gfx.KeyEvent) gfx.Event // editor: the press on a text and the moves and the release of the selection it starts, // wherever they go. func (a *App) documentPointer(p *project, d *document, event gfx.PointerEvent) gfx.EventResult { + if event.Kind == gfx.PointerDown && event.Button == gfx.PointerButtonSecondary { + if line, column, ok := d.view.PositionAt(event.X, event.Y); ok { + a.editor.copyReference.click = &referenceClick{documentID: d.id, line: line, column: column} + } + } // A semantic popup or request belongs to the caret and symbol it was opened at; any // pointer work that moved either closes it before the frame paints. defer a.pluginSemanticsCaretSync() diff --git a/desktop/internal/workbench/pluginsemantics.go b/desktop/internal/workbench/pluginsemantics.go index 5db8717d34..c11253100d 100644 --- a/desktop/internal/workbench/pluginsemantics.go +++ b/desktop/internal/workbench/pluginsemantics.go @@ -110,7 +110,7 @@ func (k semanticKind) method() string { return pluginMethodRangeFormatting case pluginSemanticRename: return pluginMethodRename - case pluginSemanticDocumentSymbols: + case pluginSemanticDocumentSymbols, pluginSemanticCopyReference: return pluginMethodDocumentSymbols case pluginSemanticTokens: return pluginMethodSemanticTokens @@ -882,6 +882,9 @@ func (a *App) semanticRequestStarted(pending *semanticPending, operation string, if err != nil { a.removePending(pending) a.settleMergeSide(pending.join, true, nil, false) + if pending.kind == pluginSemanticCopyReference { + a.applyReferenceSymbols(pending, core.BackendEvent{}, true) + } if errors.Is(err, context.Canceled) || errors.Is(ctxErr, context.Canceled) { return } diff --git a/scripts/desktop/build-desktop-core.sh b/scripts/desktop/build-desktop-core.sh index bbe50a2627..1a34ccf8d3 100755 --- a/scripts/desktop/build-desktop-core.sh +++ b/scripts/desktop/build-desktop-core.sh @@ -27,10 +27,13 @@ native_path() { fi } -# Lithe-core-rust #73 branch at 10fd6f8 (Core main 0d6e16c merged in, #77 squash-merged): lazy -# project-tree reads ask Git for the tracked directories of all ignored siblings in bounded -# batches instead of one ls-files per directory, keeping the per-directory fallback, nested -# repositories and cancellation. Switch to the Core main commit once #73 is merged. +# Lithe-core-rust main at 6a29169, #70 squash-merged on top of c928514 (#73): +# editor.copyReferenceLocation formats the innermost document symbol a language server reports +# around the caret, with a one-based line, and falls back to the file path when none contains it; +# children may be null as the Go host sends them (#2015). c928514 is #73 squash-merged on top of +# 0d6e16c (#77): lazy project-tree reads ask Git for the tracked directories of all ignored +# siblings in bounded batches instead of one ls-files per directory, keeping the per-directory +# fallback, nested repositories and cancellation. # Lithe-core-rust main at 84735ea, #76 squash-merged on top of ac8825b: build.modelTree lays out # the Build tool window's tree of a decoded model for every build tool, projects nested by # parentID, each with its task folders (by category, else by group), dependencies, targets and @@ -175,7 +178,7 @@ PINNED_CORE_REPOSITORY="https://github.com/Lithe-IDEA/Lithe-core-rust.git" # the contract, plugin guides, reference and SDKs live only in shared/plugins; on top of #16 (reported Git queries folded in the console), # #15 (git.write cherryPick, revert and reset fields), #14 (word-level changed ranges of diff # rows), #13, #8, #7, #6 and #4. -PINNED_CORE_REVISION="10fd6f8ae847868a7d5255195bf1a5b9ef84844f" +PINNED_CORE_REVISION="6a29169d030d18709563780db317ab96dcad0f87" PROFILE="debug" while [[ $# -gt 0 ]]; do case "$1" in diff --git a/shared/platform-feature-matrix/features/editor-copy-reference-location.json b/shared/platform-feature-matrix/features/editor-copy-reference-location.json new file mode 100644 index 0000000000..b19693a3aa --- /dev/null +++ b/shared/platform-feature-matrix/features/editor-copy-reference-location.json @@ -0,0 +1,31 @@ +{ + "id": "editor-copy-reference-location", + "area": "编辑器", + "group": "导航", + "capability": "从编辑器右键菜单复制符号与代码行引用", + "macos": { + "implementationStatus": "implemented", + "verificationStatus": "pending", + "evidence": [ + "desktop/internal/workbench/copy_reference.go", + "desktop/internal/workbench/editor_menu.go", + "desktop/internal/workbench/copy_reference_test.go", + "desktop/internal/core/reference_location.go", + "desktop/internal/workbench/copy_reference_visual_test.go" + ] + }, + "windows": { + "implementationStatus": "implemented", + "verificationStatus": "pending", + "evidence": [ + "desktop/internal/workbench/copy_reference.go", + "desktop/internal/workbench/editor_menu.go", + "desktop/internal/workbench/copy_reference_test.go", + "desktop/internal/core/reference_location.go", + "desktop/internal/workbench/copy_reference_visual_test.go" + ] + }, + "owner": "Editor", + "notes": "通过 Viva 共用菜单复制右键所在行的引用;Rust Core 负责从同步的 documentSymbols 生成包/类/方法签名引用,没有可靠符号时回退为文件路径与行号。菜单复用现有“复制源码位置”中英文词条;格式化命令由 Core #70 提供,桌面端固定 Core main 6a29169。", + "verification": "Core 共享 fixture 和 Rust 单测;Viva Go 测试覆盖菜单、剪贴板、语言服务器回复、无服务器回退、服务器超时不回答时的回退、右键菜单关闭后改用光标位置和过期结果。双端原生验收、视觉截图待运行。 Viva Headless 测试定义在 copy_reference_visual_test.go,执行后输出四组菜单与真实粘贴截图。" +}