From 425c4dbd5230959b5cab39b77e33fe6da87e88bb Mon Sep 17 00:00:00 2001 From: Mucheen <1528136628@qq.com> Date: Thu, 19 Nov 2026 16:49:42 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix(editor):=20=E5=AE=8C=E6=95=B4=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E8=A1=A5=E5=85=A8=E5=80=99=E9=80=89=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E5=92=8C=E7=AD=BE=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...19-viva-completion-signature-visibility.md | 36 +++++ .../internal/workbench/lsp_actions_test.go | 27 ++++ desktop/internal/workbench/pluginsemantics.go | 1 - .../workbench/pluginsemantics_test.go | 101 +++++++++++- .../workbench/pluginsemantics_view.go | 147 +++++++++++++++--- desktop/internal/workbench/signature_help.go | 97 ++++++++---- .../internal/workbench/visual_capture_test.go | 46 ++++++ .../features/lsp-completion-hover.json | 4 +- .../features/lsp-signature-help.json | 2 +- 9 files changed, 404 insertions(+), 57 deletions(-) create mode 100644 .agents/notes/implemented/bug-fix/2026-11-19-viva-completion-signature-visibility.md diff --git a/.agents/notes/implemented/bug-fix/2026-11-19-viva-completion-signature-visibility.md b/.agents/notes/implemented/bug-fix/2026-11-19-viva-completion-signature-visibility.md new file mode 100644 index 0000000000..cbd255ba28 --- /dev/null +++ b/.agents/notes/implemented/bug-fix/2026-11-19-viva-completion-signature-visibility.md @@ -0,0 +1,36 @@ +# Agent 笔记:补全名称和长签名的显示空间 + +状态:已实现 + +## 先说结论 + +补全列表优先给候选名称留出宽度,便于区分前缀相同的函数。签名在列表中放不下时,选中项的完整内容会在列表底部换行,超出详情区域时可以滚动。方向键切换候选时同步更新详情。 + +## 问题 + +固定宽度的补全弹窗在同一行压缩名称和签名。两个候选可能只显示相同的名称前缀,调用参数和返回类型也可能藏在省略号后。 + +## 决策 + +- 工作台使用实际字体测量可见候选,按窗口可用宽度限制弹窗大小。候选行保留单行,先压缩签名摘要,再在极窄窗口中省略摘要。 +- 列表放不下选中项的全部文字时,在有界高度的详情区域绘制完整文字。选中详情可滚动,键盘焦点留在编辑器。 +- 参数信息根据补全弹窗当帧的实际位置和尺寸放置。窗口不足以同时容纳两个弹窗时,优先保留当前可操作的补全列表。 + +## 考虑过的备选方案 + +- 给所有弹窗改用更大的固定宽度。窄窗口和更长的签名仍会遇到截断。 +- 在每个候选行换行显示签名。可见候选行数会随内容变化,用户逐项浏览时需要追踪位置。 + +## 后果 + +常用函数名可以直接在列表中辨认。长签名的详情会临时占用额外高度,窗口边缘的弹窗会调整位置。布局测量只遍历当前可见候选。 + +## 验证 + +- `scripts/desktop/desktop-go.sh test ./internal/workbench -run 'TestCompletionPopup|TestLSPSignatureHelpKeepsCompletionPreviewVisible'` 验证候选名称、方向键切换、滚动到长签名末尾、回车插入和参数信息的位置。 +- `TestVisualCompletionPopup` 通过 Viva 无头窗口截图,覆盖常规及窄窗口和中英文深浅主题。 + +## 适用范围 + +- `desktop/internal/workbench/pluginsemantics_view.go` +- `desktop/internal/workbench/signature_help.go` diff --git a/desktop/internal/workbench/lsp_actions_test.go b/desktop/internal/workbench/lsp_actions_test.go index 1b3f30dd1c..7a2e9a3ed8 100644 --- a/desktop/internal/workbench/lsp_actions_test.go +++ b/desktop/internal/workbench/lsp_actions_test.go @@ -150,6 +150,33 @@ func TestLSPSignatureHelpShortcutPresentsAndDismisses(t *testing.T) { } } +func TestLSPSignatureHelpKeepsCompletionPreviewVisible(t *testing.T) { + f := newSemanticFixture(t) + if err := f.h.driver.Resize(350, 600); err != nil { + t.Fatal(err) + } + f.openFunctionCompletions(t) + f.server.features = map[string]bool{featureSignatureHelp: true} + f.h.app.startPluginSignatureHelp(f.p, f.d, 0, 0, "") + f.waitForRequests(t, 2) + f.deliver(t, "op-2", pluginMethodSignatureHelp, json.RawMessage(`{ + "signatureHelp": { + "signatures": [{"label":"call(arg: string)", "documentation":"Active call.", + "parameters": [{"documentation":"Active argument.", "byteStart":5, "byteEnd":8}]}], + "activeSignature":0, "activeParameter":0 + } + }`)) + completion := f.h.hitLabeled(f.h.shared("desktop.semantics.completion")) + parameters := f.h.hitLabeled(f.h.shared("desktop.semantics.signatureHelp")) + if completion.X < parameters.Right() && parameters.X < completion.Right() && + completion.Y < parameters.Bottom() && parameters.Y < completion.Bottom() { + t.Fatalf("completion %v and parameter information %v overlap", completion, parameters) + } + if !f.h.showsWrapped(completionTimeoutDetail) || !f.h.shows("Active argument.") { + t.Fatal("the selected completion and current parameter details should both be visible") + } +} + func TestLSPInlayHintsReleaseWhenDisabled(t *testing.T) { f := newSemanticFixture(t) f.h.app.editorPreferences.InlayHints = true diff --git a/desktop/internal/workbench/pluginsemantics.go b/desktop/internal/workbench/pluginsemantics.go index 97b62378ba..e5d6104e3c 100644 --- a/desktop/internal/workbench/pluginsemantics.go +++ b/desktop/internal/workbench/pluginsemantics.go @@ -78,7 +78,6 @@ const ( // operation kind is enough; a newer request cancels the older one. maxSemanticPending = 3 - semanticPopupWidth = float32(360) semanticDefinitionWidth = float32(480) semanticRowHeight = float32(24) semanticPopupPad = float32(4) diff --git a/desktop/internal/workbench/pluginsemantics_test.go b/desktop/internal/workbench/pluginsemantics_test.go index f0dd13937f..d1b1d55046 100644 --- a/desktop/internal/workbench/pluginsemantics_test.go +++ b/desktop/internal/workbench/pluginsemantics_test.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "runtime" + "strings" "sync" "testing" @@ -61,7 +62,11 @@ type semanticFixture struct { func newSemanticFixture(t *testing.T) *semanticFixture { t.Helper() kernel := &semanticTestKernel{testKernel: &testKernel{Client: core.NewClient()}} - h := newHarness(t, kernel) + return openSemanticFixture(t, newHarness(t, kernel), kernel) +} + +func openSemanticFixture(t *testing.T, h *harness, kernel *semanticTestKernel) *semanticFixture { + t.Helper() h.open(t.TempDir()) service, err := pluginservice.Open(kernel, h.store, t.TempDir()) if err != nil { @@ -336,6 +341,100 @@ func TestExplicitCompletionAcceptsThroughCoreEdits(t *testing.T) { } } +const ( + completionTimeoutLabel = "WithTimeout" + completionCauseLabel = "WithTimeoutCause" + completionTimeoutDetail = "func(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc)" + completionCauseDetail = "func(parent context.Context, timeout time.Duration, cause error) (context.Context, context.CancelFunc)" +) + +func (f *semanticFixture) openFunctionCompletions(t *testing.T) { + t.Helper() + f.openCompletions(t, + core.PluginSemanticCompletionItem{Label: completionTimeoutLabel, Detail: completionTimeoutDetail}, + core.PluginSemanticCompletionItem{Label: completionCauseLabel, Detail: completionCauseDetail}, + ) +} + +func (f *semanticFixture) openCompletions(t *testing.T, items ...core.PluginSemanticCompletionItem) { + t.Helper() + f.d.view.GoToNavigation(0, 0, false) + if err := f.h.driver.Key(gfx.KeySpace, gfx.ModifierControl); err != nil { + t.Fatal(err) + } + f.waitForRequests(t, 1) + f.deliver(t, "op-1", pluginMethodCompletion, completionResult(t, items...)) +} + +func TestCompletionPopupKeepsFunctionNamesAndShowsSelectedSignature(t *testing.T) { + f := newSemanticFixture(t) + if err := f.h.driver.Resize(350, 600); err != nil { + t.Fatal(err) + } + f.openFunctionCompletions(t) + snapshot := f.h.driver.Snapshot() + for _, name := range []string{completionTimeoutLabel, completionCauseLabel} { + var row gfx.Rect + for _, hit := range snapshot.Hits { + if hit.Label == name && hit.Role == gfx.RoleOption { + row = hit.Bounds + } + } + found := false + for _, label := range snapshot.Labels { + if label.Value == name && label.Bounds.X >= row.X && label.Bounds.Right() <= row.Right() && + label.Bounds.Y >= row.Y && label.Bounds.Bottom() <= row.Bottom() { + found = true + } + } + if !found { + t.Errorf("candidate row did not paint the full name %q", name) + } + } + if !f.h.showsWrapped(completionTimeoutDetail) || f.h.showsWrapped(completionCauseDetail) { + t.Error("the first candidate's complete signature should be readable") + } + if err := f.h.driver.Key(gfx.KeyDown, 0); err != nil { + t.Fatal(err) + } + if !f.h.showsWrapped(completionCauseDetail) || f.h.showsWrapped(completionTimeoutDetail) { + t.Error("the complete signature should follow keyboard selection") + } + if focus := f.h.driver.Snapshot().Focused; focus != snapshot.Focused || focus.IsZero() { + t.Fatalf("focus changed from %v to %v while choosing a candidate", snapshot.Focused, focus) + } + if err := f.h.driver.Key(gfx.KeyEnter, 0); err != nil { + t.Fatal(err) + } + f.h.settle() + if got := f.d.view.TextSnapshot().String(); got != "WithTimeoutCauseone\n" { + t.Fatalf("accepted text = %q", got) + } +} + +func TestCompletionPopupCanScrollToTheEndOfALongSignature(t *testing.T) { + f := newSemanticFixture(t) + if err := f.h.driver.Resize(350, 600); err != nil { + t.Fatal(err) + } + const tail = "finalParameter bool)" + detail := "func(" + strings.Repeat("parameter distinguished.package.Type, ", 16) + tail + f.openCompletions(t, core.PluginSemanticCompletionItem{Label: "call", Detail: detail}) + if f.h.showsWrapped(tail) { + t.Fatal("the end of the long signature should start outside the preview") + } + completion := f.h.hitLabeled(f.h.shared("desktop.semantics.completion")) + if err := f.h.driver.Scroll(gfx.ScrollEvent{ + X: completion.X + completion.Width/2, Y: completion.Bottom() - 12, + DeltaY: 600, Precise: true, + }); err != nil { + t.Fatal(err) + } + if !f.h.showsWrapped(tail) { + t.Fatal("scrolling the preview did not reveal the end of the signature") + } +} + func TestCompletionSnippetIsConvertedByCore(t *testing.T) { f := newSemanticFixture(t) f.d.view.GoToNavigation(0, 0, false) diff --git a/desktop/internal/workbench/pluginsemantics_view.go b/desktop/internal/workbench/pluginsemantics_view.go index 0cd78b44f9..bc2bc66576 100644 --- a/desktop/internal/workbench/pluginsemantics_view.go +++ b/desktop/internal/workbench/pluginsemantics_view.go @@ -1,11 +1,28 @@ package workbench import ( + "math" + "strings" + "github.com/1lck/Lithe-IDEA/desktop/internal/i18n" "github.com/vivaui/viva/gfx" "github.com/vivaui/viva/ui" ) +const ( + semanticRowPad = 8 + semanticRowGap = 8 + semanticListGap = 1 + completionMinWidth = 360 + completionMaxWidth = 640 + completionLabelSize = 12 + completionDetailSize = 11 + completionPreviewLines = 3 + completionPreviewLineHeight = 18 + completionPreviewPadY = 4 + completionPreviewMaxHeight = completionPreviewLines*completionPreviewLineHeight + 2*completionPreviewPadY +) + // semanticPopups declares the open rename dialog or the definition, completion // or hover popup. The popups are placed at the caret and never take the // keyboard: the editor keeps it, and pluginSemanticsKey moves their selection. @@ -19,6 +36,7 @@ func (a *App) semanticPopups(c ui.C) { if a.project == nil || state.project != a.project { return } + var completionBounds gfx.Rect switch { case state.rename.open: a.renameDialog(c) @@ -27,12 +45,12 @@ func (a *App) semanticPopups(c ui.C) { case state.definition.open: a.definitionPopup(c) case state.completion.open: - a.completionPopup(c) + completionBounds = a.completionPopup(c) case state.hover.open: a.hoverPopup(c) } if !state.rename.open && !state.codeActions.open && !state.definition.open { - a.signatureHelpPopup(c) + a.signatureHelpPopup(c, completionBounds) a.inlayHintHoverPopup(c) } } @@ -52,7 +70,7 @@ func (a *App) semanticPopup(c ui.C, x, y, width, maxHeight float32, label string // semanticRow declares one choice of a popup and reports whether it was clicked. func (a *App) semanticRow(c ui.C, index int, label string, selected bool, content func(c ui.C)) bool { rc := c.KeyInt(index) - row := ui.Row(rc).FillWidth().Height(semanticRowHeight).Items(ui.Center).PadX(8).Gap(8). + row := ui.Row(rc).FillWidth().Height(semanticRowHeight).Items(ui.Center).PadX(semanticRowPad).Gap(semanticRowGap). Radius(a.theme.Metrics.RadiusItem()). HitWith(ui.ID(rc), label, gfx.HitOptions{Role: gfx.RoleOption, SkipFocus: true, Cursor: gfx.PointerCursorHand}). Hover(ui.Look{}.Background(a.colors.Hover)).Pressed(ui.Look{}.Background(a.colors.Active)) @@ -96,40 +114,129 @@ func (a *App) definitionPopup(c ui.C) { } } -func (a *App) completionPopup(c ui.C) { +type completionRowLayout struct { + nameWidth, detailWidth float32 + detail string + multiline bool +} + +func (a *App) completionPopup(c ui.C) gfx.Rect { state := &a.semantics.completion p, d := a.currentSemanticDocument(state.documentID) if d == nil || p != a.project || d.view.Revision() != state.revision { a.closeSemanticCompletion() - return + return gfx.Rect{} } anchor, ok := d.caretView().CaretRect() if !ok { a.closeSemanticCompletion() - return + return gfx.Rect{} + } + + bounds := c.Bounds().Inset(gfx.All(4)) + const rowInsets = 2 * (semanticPopupPad + semanticRowPad) + if bounds.Width <= rowInsets || bounds.Height < semanticRowHeight+2*semanticPopupPad { + return gfx.Rect{} } first, last := semanticWindow(state.selected, len(state.items)) - x, y := popupOrigin(anchor.X, anchor.Y+anchor.Height+2, semanticPopupWidth, c.Bounds()) + if first == last { + return gfx.Rect{} + } + // Reserve a footer's worth of space in short windows so the selected row and its + // details can both be shown. Normal windows retain the full candidate window. + previewHeight := min(float32(completionPreviewMaxHeight), max(0, bounds.Height-semanticRowHeight-2*semanticPopupPad-semanticListGap)) + maxRows := max(1, int((bounds.Height-previewHeight-2*semanticPopupPad)/(semanticRowHeight+semanticListGap))) + if last-first > maxRows { + first = min(max(first, state.selected-maxRows+1), last-maxRows) + last = first + maxRows + } + + canvas := c.Canvas() + var rows [visibleSemanticRows]completionRowLayout + desiredWidth := float32(completionMinWidth) + for i, item := range state.items[first:last] { + detail, _, multiline := strings.Cut(item.Detail, "\n") + nameWidth := float32(math.Ceil(float64(canvas.TextWidth(item.Label, completionLabelSize)))) + detailWidth := float32(math.Ceil(float64(canvas.TextWidth(detail, completionDetailSize)))) + rows[i] = completionRowLayout{nameWidth: nameWidth, detailWidth: detailWidth, detail: detail, multiline: multiline} + rowWidth := rowInsets + nameWidth + if detailWidth > 0 { + rowWidth += semanticRowGap + detailWidth + } + desiredWidth = max(desiredWidth, rowWidth) + } + width := min(desiredWidth, completionMaxWidth, bounds.Width) + textWidth := width - rowInsets + + selected := state.items[state.selected] + selectedRow := rows[state.selected-first] + var previewLabel, previewDetail string + if selectedRow.nameWidth > textWidth { + previewLabel, previewDetail = selected.Label, selected.Detail + } else if selectedRow.multiline || selectedRow.detailWidth > 0 && selectedRow.nameWidth+semanticRowGap+selectedRow.detailWidth > textWidth { + previewDetail = selected.Detail + } + preview := [2]string{previewLabel, previewDetail} + + height := 2*semanticPopupPad + float32(last-first)*(semanticRowHeight+semanticListGap) - semanticListGap + showPreview := (previewLabel != "" || previewDetail != "") && previewHeight >= completionPreviewLineHeight+2*completionPreviewPadY + if showPreview { + var scratch [completionPreviewLines]string + style := gfx.TextStyle{Size: completionLabelSize} + var lineCount int + for _, part := range preview { + lines := canvas.WrapLinesStyle(scratch[:0], part, style, textWidth, len(scratch)-lineCount, nil) + lineCount += len(lines) + } + previewHeight = min(previewHeight, float32(lineCount)*completionPreviewLineHeight+2*completionPreviewPadY) + height += previewHeight + semanticListGap + } + x := min(max(anchor.X, bounds.X), bounds.Right()-width) + y := anchor.Bottom() + 2 + if y+height > bounds.Bottom() && anchor.Y-2-bounds.Y > bounds.Bottom()-y { + y = anchor.Y - 2 - height + } + y = min(max(y, bounds.Y), bounds.Bottom()-height) + chosen := -1 - a.semanticPopup(c, x, y, semanticPopupWidth, float32(visibleSemanticRows)*semanticRowHeight+2*semanticPopupPad, - i18n.Shared(a.language(), "desktop.semantics.completion"), semanticPopupPad, func() { - ui.Column(c).FillWidth().Gap(1).Children(func() { - for i := first; i < last; i++ { - item := state.items[i] - if a.semanticRow(c, i, item.Label, i == state.selected, func(rc ui.C) { - ui.Text(rc, item.Label).FontSize(12).Ellipsis() - if item.Detail != "" { - ui.Text(rc, item.Detail).FontSize(11).Color(a.colors.Subtle).Ellipsis().Grow(1) - } - }) { - chosen = i + language := a.language() + a.semanticPopup(c, x, y, width, height, i18n.Shared(language, "desktop.semantics.completion"), semanticPopupPad, func() { + ui.Column(c).FillWidth().Gap(semanticListGap).Children(func() { + for i, item := range state.items[first:last] { + index := first + i + row := rows[i] + if a.semanticRow(c, index, item.Label, index == state.selected, func(rc ui.C) { + nameWidth := min(row.nameWidth, textWidth) + ui.Text(rc, item.Label).FontSize(completionLabelSize).Width(nameWidth).Shrink(0).Ellipsis() + if row.detail != "" && textWidth-nameWidth-semanticRowGap >= min(row.detailWidth, completionDetailSize*3) { + ui.Text(rc, row.detail).FontSize(completionDetailSize).Color(a.colors.Subtle). + Grow(1).Shrink(1).MinWidth(0).Ellipsis() } + }) { + chosen = index } - }) + } }) + if showPreview { + pc := c.Part("completion-preview").KeyInt(state.selected) + ui.Divider(pc).Height(semanticListGap) + ui.Scroll(pc).FillWidth().Height(previewHeight).Scrollbars(). + ScrollbarLabels(i18n.Shared(language, "desktop.editor.verticalScrollbar"), i18n.Shared(language, "desktop.editor.horizontalScrollbar")). + Children(func() { + ui.Column(pc).FillWidth().PadX(semanticRowPad).PadY(completionPreviewPadY).Children(func() { + for _, part := range preview { + if part != "" { + ui.Wrap(pc, part).FillWidth().FontSize(completionLabelSize).LineHeight(completionPreviewLineHeight) + } + } + }) + }) + } + }) if chosen >= 0 { c.After(func() { a.acceptPluginCompletion(chosen) }) } + return gfx.Rect{X: x, Y: y, Width: width, Height: height} } // renameDialog is the thin view over renameState: it shows the name field and diff --git a/desktop/internal/workbench/signature_help.go b/desktop/internal/workbench/signature_help.go index 4688891a54..b8e628f8cf 100644 --- a/desktop/internal/workbench/signature_help.go +++ b/desktop/internal/workbench/signature_help.go @@ -317,7 +317,7 @@ func (a *App) signatureHelpKey(event gfx.KeyEvent) gfx.EventResult { return gfx.EventResult{} } -func (a *App) signatureHelpPopup(c ui.C) { +func (a *App) signatureHelpPopup(c ui.C, completion gfx.Rect) { st := &a.semantics.signatureHelp if !st.open || st.help == nil || len(st.help.Signatures) == 0 { return @@ -361,55 +361,86 @@ func (a *App) signatureHelpPopup(c ui.C) { sigDoc = truncateDisplayText(*sig.Documentation, 200) } - hasDocs := paramDoc != "" || sigDoc != "" - estimatedHeight := float32(34) - if hasDocs { - estimatedHeight += 26 - if paramDoc != "" && sigDoc != "" { - estimatedHeight += 18 - } + const ( + padX = 10 + padY = 6 + gap = 4 + headerHeight = 22 + docLineHeight = 18 + paramDocSize = 11.5 + signatureDocSize = 11 + ) + bounds := c.Bounds().Inset(gfx.All(4)) + if bounds.Width <= 2*padX { + st.rect = gfx.Rect{} + return } - bounds := c.Bounds() charWidth := max(d.view.CellWidth(), 7.5) - labelRunes := float32(len([]rune(sig.Label))) + labelRunes := float32(utf8.RuneCountInString(sig.Label)) overloadRoom := float32(28) if len(st.help.Signatures) > 1 { overloadRoom = 96 } - width := min(max(labelRunes*charWidth+overloadRoom, signatureHelpMinWidth), min(signatureHelpMaxWidth, bounds.Width-8)) - - // Prefer above the caret so completion (placed below the caret) is never occluded. - y := anchor.Y - estimatedHeight - 4 - x := min(max(anchor.X, bounds.X+4), max(bounds.X+bounds.Width-width-4, bounds.X+4)) - if y < bounds.Y+4 { - if a.semantics.completion.open { - compX, compY := popupOrigin(anchor.X, anchor.Y+anchor.Height+2, semanticPopupWidth, bounds) - compRows := min(len(a.semantics.completion.items), visibleSemanticRows) - compH := float32(compRows)*semanticRowHeight + 2*semanticPopupPad - // Place beside completion if there is horizontal room, else below completion. - if compX+semanticPopupWidth+8+width <= bounds.X+bounds.Width-4 { - x = compX + semanticPopupWidth + 8 - y = max(anchor.Y+anchor.Height+2, bounds.Y+4) - } else { - y = min(compY+compH+4, max(bounds.Y+bounds.Height-estimatedHeight-4, bounds.Y+4)) + width := min(max(labelRunes*charWidth+overloadRoom, signatureHelpMinWidth), signatureHelpMaxWidth, bounds.Width) + height := float32(headerHeight + 2*padY) + hasDocs := paramDoc != "" || sigDoc != "" + if hasDocs { + height += gap + 1 + var scratch [8]string + for _, doc := range []struct { + text string + size float32 + }{{paramDoc, paramDocSize}, {sigDoc, signatureDocSize}} { + if doc.text != "" { + lines := c.Canvas().WrapLinesStyle(scratch[:0], doc.text, gfx.TextStyle{Size: doc.size}, + width-2*padX, utf8.RuneCountInString(doc.text), nil) + height += float32(len(lines))*docLineHeight + gap } - } else { - y = min(anchor.Y+anchor.Height+4, max(bounds.Y+bounds.Height-estimatedHeight-4, bounds.Y+4)) } } - st.rect = gfx.Rect{X: x, Y: y, Width: width, Height: estimatedHeight} + if height > bounds.Height { + st.rect = gfx.Rect{} + return + } + x := min(max(anchor.X, bounds.X), bounds.Right()-width) + y := anchor.Y - height - 4 + if completion.Height == 0 { + if y < bounds.Y { + y = min(anchor.Bottom()+4, bounds.Bottom()-height) + } + } else if y < bounds.Y || (x < completion.Right() && x+width > completion.X && + y < completion.Bottom() && y+height > completion.Y) { + const popupGap = 8 + // The completion popup can grow a footer or move above the caret. Use its + // current placement when choosing where parameter information will fit. + switch { + case completion.Right()+popupGap+width <= bounds.Right(): + x, y = completion.Right()+popupGap, completion.Y + case completion.X-popupGap-width >= bounds.X: + x, y = completion.X-popupGap-width, completion.Y + case completion.Bottom()+popupGap+height <= bounds.Bottom(): + y = completion.Bottom() + popupGap + case completion.Y-popupGap-height >= bounds.Y: + y = completion.Y - popupGap - height + default: + st.rect = gfx.Rect{} + return + } + } + y = min(max(y, bounds.Y), bounds.Bottom()-height) + st.rect = gfx.Rect{X: x, Y: y, Width: width, Height: height} lang := a.language() title := i18n.Shared(lang, "desktop.semantics.signatureHelp") stepDelta := 0 ui.Stack(c.Part("signature-help-layer")).Fill().Children(func() { sc := c.Part("signature-help-card") - ui.Column(sc).Absolute().Left(x).Top(y).Width(width).PadX(10).PadY(6).Gap(4). + ui.Column(sc).Absolute().Left(x).Top(y).Width(width).Height(height).PadX(padX).PadY(padY).Gap(gap). Background(a.theme.Popover).Border(1, a.theme.Border). Radius(a.theme.Metrics.RadiusPopup()).Shadow(gfx.ShadowMD). HitWith(ui.ID(sc), title, gfx.HitOptions{Role: gfx.RoleGroup, SkipFocus: true}). Children(func() { - ui.Row(sc).FillWidth().Items(ui.Center).Gap(6).Children(func() { + ui.Row(sc).FillWidth().Height(headerHeight).Items(ui.Center).Gap(6).Children(func() { ui.Row(sc).Grow(1).Shrink(1).MinWidth(0).Items(ui.Center).Gap(0).Clip().Children(func() { if prefix != "" { ui.Text(sc, prefix).Mono().FontSize(12).Color(a.colors.Subtle).Shrink(1).Ellipsis() @@ -460,10 +491,10 @@ func (a *App) signatureHelpPopup(c ui.C) { if hasDocs { ui.Row(sc).FillWidth().Height(1).Background(a.theme.Border) if paramDoc != "" { - ui.Wrap(sc, paramDoc).FontSize(11.5).Color(a.colors.Text) + ui.Wrap(sc, paramDoc).FillWidth().FontSize(paramDocSize).LineHeight(docLineHeight).Color(a.colors.Text) } if sigDoc != "" { - ui.Wrap(sc, sigDoc).FontSize(11).Color(a.colors.Subtle) + ui.Wrap(sc, sigDoc).FillWidth().FontSize(signatureDocSize).LineHeight(docLineHeight).Color(a.colors.Subtle) } } }) diff --git a/desktop/internal/workbench/visual_capture_test.go b/desktop/internal/workbench/visual_capture_test.go index 4bd7e9af41..72eac4018d 100644 --- a/desktop/internal/workbench/visual_capture_test.go +++ b/desktop/internal/workbench/visual_capture_test.go @@ -61,6 +61,52 @@ func TestVisualGradleFileIcons(t *testing.T) { } } +func TestVisualCompletionPopup(t *testing.T) { + out := os.Getenv("LITHE_VISUAL_OUTPUT_DIR") + if out == "" { + t.Skip("set LITHE_VISUAL_OUTPUT_DIR") + } + if err := os.MkdirAll(out, 0o755); err != nil { + t.Fatal(err) + } + for _, scene := range []struct { + theme, language string + width int + }{ + {"light", "en-US", 1000}, + {"dark", "zh-Hans", 1000}, + {"light", "zh-Hans", 350}, + {"dark", "en-US", 350}, + } { + t.Run(scene.theme+"-"+scene.language, func(t *testing.T) { + prefs := store.At(t.TempDir()) + if err := prefs.Save(settingsFile, map[string]string{"theme": scene.theme}); err != nil { + t.Fatal(err) + } + kernel := &semanticTestKernel{testKernel: &testKernel{Client: core.NewClient()}} + var session *headless.Session + h := newHarnessWithDriver(t, Options{Kernel: kernel, Store: prefs, Languages: []string{scene.language}}, func(config app.Config) (*app.Driver, func(), error) { + config.Width, config.Height = scene.width, 600 + config.MinWidth = min(config.MinWidth, scene.width) + var err error + session, err = headless.Open(config, nil, headless.Options{}) + if err != nil { + return nil, nil, err + } + return session.Driver(), session.Close, nil + }) + f := openSemanticFixture(t, h, kernel) + f.openFunctionCompletions(t) + if err := h.driver.Key(gfx.KeyDown, 0); err != nil { + t.Fatal(err) + } + if err := session.Screenshot(filepath.Join(out, "completion-"+scene.theme+"-"+scene.language+".png")); err != nil { + t.Fatal(err) + } + }) + } +} + // TestVisualWorkbench uses the archive's repository in a private temporary checkout. // No installed plugins, user settings, native windows or network services are used. func TestVisualWorkbench(t *testing.T) { diff --git a/shared/platform-feature-matrix/features/lsp-completion-hover.json b/shared/platform-feature-matrix/features/lsp-completion-hover.json index af077fb4fb..4aedebd78e 100644 --- a/shared/platform-feature-matrix/features/lsp-completion-hover.json +++ b/shared/platform-feature-matrix/features/lsp-completion-hover.json @@ -8,6 +8,7 @@ "verificationStatus": "pending", "evidence": [ "desktop/internal/workbench/pluginsemantics.go", + "desktop/internal/workbench/pluginsemantics_view.go", "desktop/internal/workbench/pluginsemantics_hover.go", "desktop/internal/workbench/pluginsemantics_hover_test.go", "desktop/internal/workbench/pluginsemantics_test.go", @@ -21,6 +22,7 @@ "verificationStatus": "pending", "evidence": [ "desktop/internal/workbench/pluginsemantics.go", + "desktop/internal/workbench/pluginsemantics_view.go", "desktop/internal/workbench/pluginsemantics_hover.go", "desktop/internal/workbench/pluginsemantics_hover_test.go", "desktop/internal/workbench/pluginsemantics_test.go", @@ -30,7 +32,7 @@ ] }, "owner": "Language Tooling", - "verification": "Viva(2026-11-16,#1995):指针停留触发 Hover 的等待从 500 ms 缩短到 300 ms,与 Zed、VS Code 的默认值一致;F1、离开即取消和同一字符内不重复请求的行为不变。在 macOS 打包应用里接真实语言服务器实测,指针落到字符上到弹窗出现的中位数为 360 ms(改动前 562 ms,各 5 次,扣除等待后都约 60 ms)。Windows 共用同一份 Viva 代码,原生窗口验收待做。 Core 6a14126(2026-11-16,Lithe-core-rust #61,#1995):LSP 引擎为每个语言服务器会话保留悬浮答案;同一文档版本的同一位置,或答案 range 在同一行内覆盖的位置,再次悬浮时直接返回保留的答案,不再发给服务器。每个会话最多 32 个答案(最久没用的先丢),单个答案超过 64 KiB 不保留,空答案、错误和缓存清空之后才到达的答案不保留;文档打开、修改、保存、关闭,工作区文件变化,配置或索引排除项变化,executeCommand,插件后端的 languageServer/request 与 languageServer/notify,宿主通知写给插件后端,服务器发来的任何请求或 $/progress end,以及会话停止、崩溃或重启时清空。Core 的单元测试和脚本化服务器会话测试覆盖命中、范围、淘汰和清空,两端共用同一个 Core;原生窗口验收待做。 Viva(2026-11-16,#1995):指针离开文本后忘掉停留过的字符:按 Esc、移出窗口、移到标签栏等其他控件、滚动编辑器后回到同一字符,会重新请求并显示 Hover;离开时还在 500 ms 等待中的 Hover 不再发出请求;没有 Hover 的字符内移动仍不重复请求。无头测试覆盖四种离开方式,原生窗口验收待做。 Viva(2026-11-14):Hover 弹窗按 Markdown 渲染(签名代码块、分隔线、正文、示例、链接),代码块用编辑器同一套语法着色,http/https/mailto 链接点击后用系统浏览器打开;宽度在 280–620 之间按内容收窄,超过 420 高度时可滚动。弹窗贴在指针所在行的下沿、左缘对齐指针;指针停在同一符号范围内不重新请求,移入弹窗时保持打开,离开符号和弹窗 400 ms 后才关闭,期间回到符号或弹窗会取消关闭。无头测试覆盖渲染、位置、保持、延迟关闭和代码着色,原生窗口验收待做。 Viva(2026-11-10):输入单个标识符字符或 . : > 后防抖(120 ms)自动请求补全,继续输入、移动光标或编辑会取消并丢弃过期结果;服务器声明的 triggerCharacters 尚未由 Core 暴露,暂用固定成员访问字符集。 Viva(2026-11-09):Hover 支持 F1 与 500 ms 指针停留,补全由 Control+Space 显式触发,上下键选择、回车/Tab 通过 Core 的 lsp.applyTextEdits 与 lsp.plainSnippet 接受;无头 Viva 窗口使用真实 clangd 隔离插件验证了 Hover 内容、补全列表和接受后的文本与光标位置。连续输入自动重新请求、isIncomplete 刷新、触发字符和多来源合并尚未接入,因此保持部分实现。 用测试内联的合成语言服务器插件验证补全、Hover、排序、超时和空结果。验证 initialize、didChangeConfiguration 与 workspace/configuration 三个配置入口一致,父级和叶级 section 均保留插件声明的其他配置。 连续输入时保留 isIncomplete 并通过 LSP triggerKind=3 重新请求候选;验证空未完整列表、完整/空响应、触发字符、快速修改及文档关闭/会话替换后的过期结果。 #1662 LSP-09:多个补全来源的合并由 Core 的 lsp.mergeCompletions 决定:同一来源的候选全部保留(同名不同签名的重载不再被丢弃),低优先级来源中与高优先级来源同 label 的候选被遮蔽。Windows 只有一个补全来源,原样显示服务器列表,不调用该命令。macOS 删除私有的 Go/Swift/Rust/Python/JS/TS 关键词表,关键词改由语言服务器或插件提供,与 Windows 一致。在 macOS 打开有重载方法的文件,确认同名重载全部出现;未运行语言服务器的 Go 文件不再出现内置关键词候选,当前文件标识符仍会出现。", + "verification": "使用合成语言服务返回名称相近、签名较长的候选。在常规及窄窗口检查完整名称,窄窗口中用方向键切换并阅读换行的完整签名,按回车确认插入所选候选。检查补全弹窗与参数信息同时可见时的位置。 Viva(2026-11-16,#1995):指针停留触发 Hover 的等待从 500 ms 缩短到 300 ms,与 Zed、VS Code 的默认值一致;F1、离开即取消和同一字符内不重复请求的行为不变。在 macOS 打包应用里接真实语言服务器实测,指针落到字符上到弹窗出现的中位数为 360 ms(改动前 562 ms,各 5 次,扣除等待后都约 60 ms)。Windows 共用同一份 Viva 代码,原生窗口验收待做。 Core 6a14126(2026-11-16,Lithe-core-rust #61,#1995):LSP 引擎为每个语言服务器会话保留悬浮答案;同一文档版本的同一位置,或答案 range 在同一行内覆盖的位置,再次悬浮时直接返回保留的答案,不再发给服务器。每个会话最多 32 个答案(最久没用的先丢),单个答案超过 64 KiB 不保留,空答案、错误和缓存清空之后才到达的答案不保留;文档打开、修改、保存、关闭,工作区文件变化,配置或索引排除项变化,executeCommand,插件后端的 languageServer/request 与 languageServer/notify,宿主通知写给插件后端,服务器发来的任何请求或 $/progress end,以及会话停止、崩溃或重启时清空。Core 的单元测试和脚本化服务器会话测试覆盖命中、范围、淘汰和清空,两端共用同一个 Core;原生窗口验收待做。 Viva(2026-11-16,#1995):指针离开文本后忘掉停留过的字符:按 Esc、移出窗口、移到标签栏等其他控件、滚动编辑器后回到同一字符,会重新请求并显示 Hover;离开时还在 500 ms 等待中的 Hover 不再发出请求;没有 Hover 的字符内移动仍不重复请求。无头测试覆盖四种离开方式,原生窗口验收待做。 Viva(2026-11-14):Hover 弹窗按 Markdown 渲染(签名代码块、分隔线、正文、示例、链接),代码块用编辑器同一套语法着色,http/https/mailto 链接点击后用系统浏览器打开;宽度在 280–620 之间按内容收窄,超过 420 高度时可滚动。弹窗贴在指针所在行的下沿、左缘对齐指针;指针停在同一符号范围内不重新请求,移入弹窗时保持打开,离开符号和弹窗 400 ms 后才关闭,期间回到符号或弹窗会取消关闭。无头测试覆盖渲染、位置、保持、延迟关闭和代码着色,原生窗口验收待做。 Viva(2026-11-10):输入单个标识符字符或 . : > 后防抖(120 ms)自动请求补全,继续输入、移动光标或编辑会取消并丢弃过期结果;服务器声明的 triggerCharacters 尚未由 Core 暴露,暂用固定成员访问字符集。 Viva(2026-11-09):Hover 支持 F1 与 500 ms 指针停留,补全由 Control+Space 显式触发,上下键选择、回车/Tab 通过 Core 的 lsp.applyTextEdits 与 lsp.plainSnippet 接受;无头 Viva 窗口使用真实 clangd 隔离插件验证了 Hover 内容、补全列表和接受后的文本与光标位置。连续输入自动重新请求、isIncomplete 刷新、触发字符和多来源合并尚未接入,因此保持部分实现。 用测试内联的合成语言服务器插件验证补全、Hover、排序、超时和空结果。验证 initialize、didChangeConfiguration 与 workspace/configuration 三个配置入口一致,父级和叶级 section 均保留插件声明的其他配置。 连续输入时保留 isIncomplete 并通过 LSP triggerKind=3 重新请求候选;验证空未完整列表、完整/空响应、触发字符、快速修改及文档关闭/会话替换后的过期结果。 #1662 LSP-09:多个补全来源的合并由 Core 的 lsp.mergeCompletions 决定:同一来源的候选全部保留(同名不同签名的重载不再被丢弃),低优先级来源中与高优先级来源同 label 的候选被遮蔽。Windows 只有一个补全来源,原样显示服务器列表,不调用该命令。macOS 删除私有的 Go/Swift/Rust/Python/JS/TS 关键词表,关键词改由语言服务器或插件提供,与 Windows 一致。在 macOS 打开有重载方法的文件,确认同名重载全部出现;未运行语言服务器的 Go 文件不再出现内置关键词候选,当前文件标识符仍会出现。", "notes": "Viva 已接入显式补全与接受(含 Core 的 snippet 转换)以及 Hover(F1 与指针停留);isIncomplete 连续请求、触发字符和多来源合并尚未接入。无头 clangd 测试不是原生窗口验收,平台验证保持 pending。 自动触发已接入,触发字符集暂为固定值,待 Core 暴露服务器声明。", "legacy": { "revision": "5a54c41aa37a4e6e6b68918a4b05765b9dd07a36", diff --git a/shared/platform-feature-matrix/features/lsp-signature-help.json b/shared/platform-feature-matrix/features/lsp-signature-help.json index 81ea8bfab2..e7cb7165c6 100644 --- a/shared/platform-feature-matrix/features/lsp-signature-help.json +++ b/shared/platform-feature-matrix/features/lsp-signature-help.json @@ -22,6 +22,6 @@ ] }, "owner": "Editor / Language Tooling", - "verification": "以声明该能力的合成语言服务器验证参数信息快捷键、触发字符、当前参数高亮、重载切换、离开调用和 Esc 关闭。", + "verification": "以声明该能力的合成语言服务器验证参数信息快捷键、触发字符、当前参数高亮、重载切换、离开调用和 Esc 关闭。 在窄窗口同时打开带长签名详情的补全弹窗,确认两块内容都可见且弹窗互不遮挡。", "notes": "Viva 在 macOS 和 Windows 共享实现。原生平台验证待补。" } From bdba5212588302bc6091ccb7fbf8e37ebb9089a2 Mon Sep 17 00:00:00 2001 From: Mucheen <1528136628@qq.com> Date: Thu, 19 Nov 2026 17:10:12 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix(ci):=20=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E9=93=BE=E8=AE=BE=E7=BD=AE=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/settingswindow/toolchains_state.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/desktop/internal/settingswindow/toolchains_state.go b/desktop/internal/settingswindow/toolchains_state.go index eb88e7ee1b..a0cdc76b8c 100644 --- a/desktop/internal/settingswindow/toolchains_state.go +++ b/desktop/internal/settingswindow/toolchains_state.go @@ -376,14 +376,14 @@ type toolchainState struct { trustWanted bool trusting bool - runtimeEffect *ui.EffectState[toolchain.Snapshot, int] - runtimeKey int - runtimeScope string - runtimeSnapshot toolchain.Snapshot - runtimeTool toolchain.Tool + runtimeEffect *ui.EffectState[toolchain.Snapshot, int] + runtimeKey int + runtimeScope string + runtimeSnapshot toolchain.Snapshot + runtimeTool toolchain.Tool preparingRuntime bool - runtimeProgress atomic.Pointer[pluginruntime.Progress] - invalidate func() + runtimeProgress atomic.Pointer[pluginruntime.Progress] + invalidate func() // failed is the last write, check or trust request's error; a read's error is its scope's. failed error From 8a1d04743ca02d583395299f6c06c48decaa4033 Mon Sep 17 00:00:00 2001 From: Mucheen <1528136628@qq.com> Date: Thu, 19 Nov 2026 19:04:59 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix(editor):=20=E4=BF=AE=E5=A4=8D=E8=A1=A5?= =?UTF-8?q?=E5=85=A8=E8=AF=A6=E6=83=85=E6=B5=8B=E9=87=8F=E3=80=81=E5=BC=B9?= =?UTF-8?q?=E7=AA=97=E9=81=AE=E6=8C=A1=E5=92=8C=E9=95=BF=E7=AD=BE=E5=90=8D?= =?UTF-8?q?=E5=8D=A1=E9=A1=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...19-viva-completion-signature-visibility.md | 13 +- .github/workflows/desktop-ci.yml | 2 +- .../internal/workbench/lsp_actions_test.go | 59 +++++ .../workbench/pluginsemantics_test.go | 127 ++++++++++ .../workbench/pluginsemantics_view.go | 224 +++++++++++++----- desktop/internal/workbench/signature_help.go | 79 +++--- .../internal/workbench/visual_capture_test.go | 33 ++- .../features/lsp-completion-hover.json | 2 +- .../features/lsp-signature-help.json | 2 +- 9 files changed, 426 insertions(+), 115 deletions(-) diff --git a/.agents/notes/implemented/bug-fix/2026-11-19-viva-completion-signature-visibility.md b/.agents/notes/implemented/bug-fix/2026-11-19-viva-completion-signature-visibility.md index cbd255ba28..158c2542e0 100644 --- a/.agents/notes/implemented/bug-fix/2026-11-19-viva-completion-signature-visibility.md +++ b/.agents/notes/implemented/bug-fix/2026-11-19-viva-completion-signature-visibility.md @@ -12,23 +12,24 @@ ## 决策 -- 工作台使用实际字体测量可见候选,按窗口可用宽度限制弹窗大小。候选行保留单行,先压缩签名摘要,再在极窄窗口中省略摘要。 -- 列表放不下选中项的全部文字时,在有界高度的详情区域绘制完整文字。选中详情可滚动,键盘焦点留在编辑器。 -- 参数信息根据补全弹窗当帧的实际位置和尺寸放置。窗口不足以同时容纳两个弹窗时,优先保留当前可操作的补全列表。 +- 工作台使用实际字体测量候选,行内省略、详情触发和参数文档换行均使用扣除边框和内边距后的宽度。参数文档的卡片高度包含边框。 +- 列表放不下选中项的全部文字时,在有界高度的详情区域展示完整文字。超长文本交由 Viva 虚拟列表按块换行,滚动条可直接定位到后面的内容,键盘焦点留在编辑器。 +- 补全根据光标上下的可用高度限制候选行数,键盘导航始终能带入当前选中项。参数信息同时避开补全弹窗和当前编辑行。窗口空间只够一个弹窗时保留补全列表。 ## 考虑过的备选方案 - 给所有弹窗改用更大的固定宽度。窄窗口和更长的签名仍会遇到截断。 - 在每个候选行换行显示签名。可见候选行数会随内容变化,用户逐项浏览时需要追踪位置。 +- 用一个换行控件排版超长签名。控件会在首帧同步排版整段文本,压缩可见高度也无法减少这部分耗时。 ## 后果 -常用函数名可以直接在列表中辨认。长签名的详情会临时占用额外高度,窗口边缘的弹窗会调整位置。布局测量只遍历当前可见候选。 +常用函数名可以直接在列表中辨认。详情会临时占用额外高度,光标附近空间有限时会少显示几项候选。超长详情的虚拟列表在接近文本块边界时可能多换一行。 ## 验证 -- `scripts/desktop/desktop-go.sh test ./internal/workbench -run 'TestCompletionPopup|TestLSPSignatureHelpKeepsCompletionPreviewVisible'` 验证候选名称、方向键切换、滚动到长签名末尾、回车插入和参数信息的位置。 -- `TestVisualCompletionPopup` 通过 Viva 无头窗口截图,覆盖常规及窄窗口和中英文深浅主题。 +- `scripts/desktop/desktop-go.sh test ./internal/workbench -run 'TestCompletionPopup|TestLSPSignatureHelp'` 验证完整签名、键盘焦点、超长签名测量和末尾滚动、弹窗避让光标及参数文档的卡片边界。 +- `TestVisualCompletionPopup` 通过 Viva 无头窗口截图,覆盖常规及窄窗口、中英文深浅主题,以及光标位于中部或靠近底边的场景。 ## 适用范围 diff --git a/.github/workflows/desktop-ci.yml b/.github/workflows/desktop-ci.yml index 504753d0ee..4cf9e178ed 100644 --- a/.github/workflows/desktop-ci.yml +++ b/.github/workflows/desktop-ci.yml @@ -178,7 +178,7 @@ jobs: LITHE_VISUAL_OUTPUT_DIR: ${{ github.workspace }}/.artifacts/lsp-editor-visual run: | ./scripts/desktop/desktop-go.sh test -v -tags 'vivahead lithevisual' ./internal/workbench \ - -run '^(TestLSP|TestVisualLSPActions$)' -count=1 -timeout=120s + -run '^(TestLSP|TestCompletionPopup|TestVisual(LSPActions|CompletionPopup)$)' -count=1 -timeout=120s - name: Upload LSP editor screenshots if: ${{ !cancelled() && steps.lsp-visual.outcome != 'skipped' }} diff --git a/desktop/internal/workbench/lsp_actions_test.go b/desktop/internal/workbench/lsp_actions_test.go index 7a2e9a3ed8..43b0beb461 100644 --- a/desktop/internal/workbench/lsp_actions_test.go +++ b/desktop/internal/workbench/lsp_actions_test.go @@ -6,6 +6,7 @@ import ( "errors" "os" "path/filepath" + "strings" "sync" "testing" "time" @@ -177,6 +178,64 @@ func TestLSPSignatureHelpKeepsCompletionPreviewVisible(t *testing.T) { } } +func TestLSPSignatureHelpStaysClearOfCaretWhenCompletionIsAbove(t *testing.T) { + f := newSemanticFixture(t) + if err := f.h.driver.Resize(900, 600); err != nil { + t.Fatal(err) + } + caret := f.placeCompletionCaret(t, 19) + f.requestCompletions(t, longFunctionCandidates()...) + completion := f.h.hitLabeled(f.h.shared("desktop.semantics.completion")) + if completion.Bottom() > caret.Y { + t.Fatalf("expected completion %v to open above the caret %v", completion, caret) + } + f.openCompletionCaretSignature(t) + parameters := f.h.hitLabeled(f.h.shared("desktop.semantics.signatureHelp")) + if parameters.Y < caret.Bottom() { + t.Fatalf("parameters %v should fit below the caret %v", parameters, caret) + } +} + +func (f *semanticFixture) openCompletionCaretSignature(t *testing.T) { + t.Helper() + line, column := f.d.caretView().CaretUTF16() + f.server.features = map[string]bool{featureSignatureHelp: true} + f.h.app.startPluginSignatureHelp(f.p, f.d, line, column, "") + f.waitForRequests(t, 2) + f.deliver(t, "op-2", pluginMethodSignatureHelp, json.RawMessage(`{ + "signatureHelp": { + "signatures": [{"label":"call(arg: path.to.a.package.with.a.lengthy.qualified.parameter.Type)", "documentation":"Short help."}], + "activeSignature":0 + } + }`)) +} + +func TestLSPSignatureHelpMeasuresDocumentationInsideBorder(t *testing.T) { + f := newSemanticFixture(t) + f.d.view.GoToNavigation(0, 0, false) + f.server.features = map[string]bool{featureSignatureHelp: true} + f.h.app.startPluginSignatureHelp(f.p, f.d, 0, 0, "") + f.waitForRequests(t, 1) + // At 11 px with the driver's reference fonts, this needs 198.15 px. + const doc = "Calls www iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii daily." + f.deliver(t, "op-1", pluginMethodSignatureHelp, json.RawMessage(`{ + "signatureHelp": { + "signatures": [{"label":"call(arg: string)", "documentation":"`+doc+`"}], + "activeSignature":0 + } + }`)) + popup := f.h.hitLabeled(f.h.shared("desktop.semantics.signatureHelp")) + for _, label := range f.h.driver.Snapshot().Labels { + if strings.HasSuffix(label.Value, "daily.") { + if label.Bounds.Bottom() > popup.Bottom() { + t.Fatalf("documentation line %v extends below its card %v", label.Bounds, popup) + } + return + } + } + t.Fatal("last line of the parameter documentation was not drawn") +} + func TestLSPInlayHintsReleaseWhenDisabled(t *testing.T) { f := newSemanticFixture(t) f.h.app.editorPreferences.InlayHints = true diff --git a/desktop/internal/workbench/pluginsemantics_test.go b/desktop/internal/workbench/pluginsemantics_test.go index d1b1d55046..7a9944c7b1 100644 --- a/desktop/internal/workbench/pluginsemantics_test.go +++ b/desktop/internal/workbench/pluginsemantics_test.go @@ -17,6 +17,8 @@ import ( "github.com/1lck/Lithe-IDEA/desktop/internal/pluginbackend" "github.com/1lck/Lithe-IDEA/desktop/internal/pluginlsp" "github.com/1lck/Lithe-IDEA/desktop/internal/pluginservice" + "github.com/1lck/Lithe-IDEA/desktop/internal/store" + "github.com/vivaui/viva/app" "github.com/vivaui/viva/gfx" ) @@ -359,6 +361,11 @@ func (f *semanticFixture) openFunctionCompletions(t *testing.T) { func (f *semanticFixture) openCompletions(t *testing.T, items ...core.PluginSemanticCompletionItem) { t.Helper() f.d.view.GoToNavigation(0, 0, false) + f.requestCompletions(t, items...) +} + +func (f *semanticFixture) requestCompletions(t *testing.T, items ...core.PluginSemanticCompletionItem) { + t.Helper() if err := f.h.driver.Key(gfx.KeySpace, gfx.ModifierControl); err != nil { t.Fatal(err) } @@ -412,6 +419,126 @@ func TestCompletionPopupKeepsFunctionNamesAndShowsSelectedSignature(t *testing.T } } +func TestCompletionPopupSignatureAtContentWidth(t *testing.T) { + f := newSemanticFixture(t) + if err := f.h.driver.Resize(1000, 600); err != nil { + t.Fatal(err) + } + const detail = "func(parent sample.Context, delay sample.Duration) (sample.Context, sample.Cancel)" + f.openCompletions(t, core.PluginSemanticCompletionItem{Label: "CallAfter", Detail: detail}) + if !f.h.showsWrapped(detail) { + t.Fatal("the width-setting candidate's signature was clipped without a preview") + } +} + +// placeCompletionCaret sets up enough lines to place the caret away from the top +// of the document. The caller chooses the line for the window geometry under test. +func (f *semanticFixture) placeCompletionCaret(t *testing.T, line int) gfx.Rect { + t.Helper() + content := []byte(strings.Repeat("call(value)\n", 70)) + if err := os.WriteFile(f.d.path, content, 0o600); err != nil { + t.Fatal(err) + } + f.d.view.ShowContent(editorview.Prepare(content)) + f.h.settle() + f.d.view.GoToNavigation(line, 5, false) + f.h.app.invalidator.Invalidate() + if err := f.h.driver.Pump(); err != nil { + t.Fatal(err) + } + caret, ok := f.d.caretView().CaretRect() + if !ok { + t.Fatal("could not measure the caret") + } + if got, _ := f.d.caretView().CaretUTF16(); got != line { + t.Fatalf("caret ended on line %d, want %d", got, line) + } + return caret +} + +func longFunctionCandidates() []core.PluginSemanticCompletionItem { + items := make([]core.PluginSemanticCompletionItem, 10) + for i := range items { + items[i] = core.PluginSemanticCompletionItem{ + Label: fmt.Sprintf("candidate%02d", i), + Detail: "func(" + strings.Repeat("parameter distinguished.package.Type, ", 12) + "tail bool)", + } + } + return items +} + +func TestCompletionPopupStaysClearOfCaret(t *testing.T) { + f := newSemanticFixture(t) + if err := f.h.driver.Resize(900, 600); err != nil { + t.Fatal(err) + } + caret := f.placeCompletionCaret(t, 10) + f.requestCompletions(t, longFunctionCandidates()...) + popup := f.h.hitLabeled(f.h.shared("desktop.semantics.completion")) + if popup.Y < caret.Bottom() && caret.Y < popup.Bottom() { + t.Fatalf("completion %v covers the caret %v", popup, caret) + } + if err := f.h.driver.Key(gfx.KeyPageDown, 0); err != nil { + t.Fatal(err) + } + f.h.hitLabeled("candidate09") +} + +func TestCompletionPopupBoundsLongSignatureMeasurement(t *testing.T) { + kernel := &semanticTestKernel{testKernel: &testKernel{Client: core.NewClient()}} + canvas := &gfx.Canvas{} + longest := 0 + canvas.SetMeasurer(func(value string, _, _ float32) float32 { + if strings.Contains(value[:min(len(value), 40)], "distinguished.package.Type") { + longest = max(longest, len(value)) + } + return 0 // use the normal headless measurement + }) + prefs := store.At(t.TempDir()) + if err := prefs.Save(settingsFile, map[string]string{"theme": "dark"}); err != nil { + t.Fatal(err) + } + h := newHarnessWithDriver(t, Options{Kernel: kernel, Store: prefs}, func(config app.Config) (*app.Driver, func(), error) { + driver, err := app.NewDriver(config, canvas) + if err != nil { + return nil, nil, err + } + return driver, driver.Close, nil + }) + f := openSemanticFixture(t, h, kernel) + const tail = "finalParameter bool)" + detail := "func(" + strings.Repeat("parameter distinguished.package.Type, ", 4096) + tail + f.openCompletions(t, core.PluginSemanticCompletionItem{Label: "shortCall", Detail: "func()"}, + core.PluginSemanticCompletionItem{Label: "longCall", Detail: detail}) + if err := h.driver.Key(gfx.KeyDown, 0); err != nil { + t.Fatal(err) + } + if longest == 0 || longest > 2048 { + t.Fatalf("first display measured a %d-byte fragment of the %d-byte signature", longest, len(detail)) + } + if h.showsWrapped(tail) { + t.Fatal("tail should start outside the preview") + } + focus := h.driver.Snapshot().Focused + popup := h.hitLabeled(h.shared("desktop.semantics.completion")) + x, y := popup.X+popup.Width/2, popup.Bottom()-12 + if err := h.driver.Click(x, y); err != nil { + t.Fatal(err) + } + if got := h.driver.Snapshot().Focused; focus.IsZero() || got != focus { + t.Fatalf("clicking preview changed editor focus from %v to %v", focus, got) + } + for range 8 { + if err := h.driver.Scroll(gfx.ScrollEvent{X: x, Y: y, DeltaY: 1e6, Precise: true}); err != nil { + t.Fatal(err) + } + if h.showsWrapped(tail) { + return + } + } + t.Fatal("scrolling the virtual preview did not reveal the end of the signature") +} + func TestCompletionPopupCanScrollToTheEndOfALongSignature(t *testing.T) { f := newSemanticFixture(t) if err := f.h.driver.Resize(350, 600); err != nil { diff --git a/desktop/internal/workbench/pluginsemantics_view.go b/desktop/internal/workbench/pluginsemantics_view.go index bc2bc66576..022b0e2a26 100644 --- a/desktop/internal/workbench/pluginsemantics_view.go +++ b/desktop/internal/workbench/pluginsemantics_view.go @@ -3,6 +3,7 @@ package workbench import ( "math" "strings" + "unicode/utf8" "github.com/1lck/Lithe-IDEA/desktop/internal/i18n" "github.com/vivaui/viva/gfx" @@ -13,6 +14,8 @@ const ( semanticRowPad = 8 semanticRowGap = 8 semanticListGap = 1 + semanticPopupBorder = 1 + semanticPopupMargin = 4 completionMinWidth = 360 completionMaxWidth = 640 completionLabelSize = 12 @@ -21,6 +24,10 @@ const ( completionPreviewLineHeight = 18 completionPreviewPadY = 4 completionPreviewMaxHeight = completionPreviewLines*completionPreviewLineHeight + 2*completionPreviewPadY + completionPreviewMinHeight = completionPreviewLineHeight + 2*completionPreviewPadY + // Keep single-line measurement and each virtual preview block bounded, + // including when the server sends one long line without newlines. + completionTextBlockBytes = 512 ) // semanticPopups declares the open rename dialog or the definition, completion @@ -60,7 +67,7 @@ func (a *App) semanticPopups(c ui.C) { func (a *App) semanticPopup(c ui.C, x, y, width, maxHeight float32, label string, pad float32, body func()) { ui.Stack(c).Fill().Children(func() { ui.Column(c).Absolute().Left(x).Top(y).Width(width).MaxHeight(maxHeight).Pad(pad). - Background(a.theme.Popover).Border(1, a.theme.Border). + Background(a.theme.Popover).Border(semanticPopupBorder, a.theme.Border). Radius(a.theme.Metrics.RadiusPopup()).Shadow(gfx.ShadowMD). HitWith(ui.ID(c), label, gfx.HitOptions{Role: gfx.RoleMenu, SkipFocus: true}). Children(body) @@ -115,9 +122,26 @@ func (a *App) definitionPopup(c ui.C) { } type completionRowLayout struct { - nameWidth, detailWidth float32 - detail string - multiline bool + nameWidth, detailWidth float32 + name, detail string + nameClipped, moreDetail bool +} + +// completionSummary reads only a bounded first line. The preview still uses the +// original text, so even a candidate whose label contains a signature is readable. +func completionSummary(value string) (string, bool) { + end := min(len(value), completionTextBlockBytes) + if end < len(value) { + end = gfx.GraphemeStart(value[:min(end+utf8.UTFMax, len(value))], end) + // A single cluster longer than the whole budget cannot appear in a summary. + if end == 0 { + return "…", true + } + } + if line, _, found := strings.Cut(value[:end], "\n"); found { + return strings.TrimSuffix(line, "\r"), true + } + return value[:end], end < len(value) } func (a *App) completionPopup(c ui.C) gfx.Rect { @@ -133,32 +157,32 @@ func (a *App) completionPopup(c ui.C) gfx.Rect { return gfx.Rect{} } - bounds := c.Bounds().Inset(gfx.All(4)) - const rowInsets = 2 * (semanticPopupPad + semanticRowPad) - if bounds.Width <= rowInsets || bounds.Height < semanticRowHeight+2*semanticPopupPad { + bounds := c.Bounds().Inset(gfx.All(semanticPopupMargin)) + const ( + popupInsets = 2 * (semanticPopupPad + semanticPopupBorder) + rowInsets = popupInsets + 2*semanticRowPad + caretGap = 2 + rowStep = semanticRowHeight + semanticListGap + ) + above := anchor.Y - caretGap - bounds.Y + below := bounds.Bottom() - (anchor.Bottom() + caretGap) + if bounds.Width <= rowInsets || anchor.Bottom() <= bounds.Y || anchor.Y >= bounds.Bottom() || + max(above, below) < semanticRowHeight+popupInsets { return gfx.Rect{} } first, last := semanticWindow(state.selected, len(state.items)) if first == last { return gfx.Rect{} } - // Reserve a footer's worth of space in short windows so the selected row and its - // details can both be shown. Normal windows retain the full candidate window. - previewHeight := min(float32(completionPreviewMaxHeight), max(0, bounds.Height-semanticRowHeight-2*semanticPopupPad-semanticListGap)) - maxRows := max(1, int((bounds.Height-previewHeight-2*semanticPopupPad)/(semanticRowHeight+semanticListGap))) - if last-first > maxRows { - first = min(max(first, state.selected-maxRows+1), last-maxRows) - last = first + maxRows - } - canvas := c.Canvas() var rows [visibleSemanticRows]completionRowLayout desiredWidth := float32(completionMinWidth) for i, item := range state.items[first:last] { - detail, _, multiline := strings.Cut(item.Detail, "\n") - nameWidth := float32(math.Ceil(float64(canvas.TextWidth(item.Label, completionLabelSize)))) + name, nameClipped := completionSummary(item.Label) + detail, moreDetail := completionSummary(item.Detail) + nameWidth := float32(math.Ceil(float64(canvas.TextWidth(name, completionLabelSize)))) detailWidth := float32(math.Ceil(float64(canvas.TextWidth(detail, completionDetailSize)))) - rows[i] = completionRowLayout{nameWidth: nameWidth, detailWidth: detailWidth, detail: detail, multiline: multiline} + rows[i] = completionRowLayout{nameWidth, detailWidth, name, detail, nameClipped, moreDetail} rowWidth := rowInsets + nameWidth if detailWidth > 0 { rowWidth += semanticRowGap + detailWidth @@ -170,67 +194,65 @@ func (a *App) completionPopup(c ui.C) gfx.Rect { selected := state.items[state.selected] selectedRow := rows[state.selected-first] - var previewLabel, previewDetail string - if selectedRow.nameWidth > textWidth { - previewLabel, previewDetail = selected.Label, selected.Detail - } else if selectedRow.multiline || selectedRow.detailWidth > 0 && selectedRow.nameWidth+semanticRowGap+selectedRow.detailWidth > textWidth { - previewDetail = selected.Detail + var preview [2]string + if selectedRow.nameClipped || selectedRow.nameWidth > textWidth { + preview = [2]string{selected.Label, selected.Detail} + } else if selectedRow.moreDetail || selectedRow.detailWidth > 0 && selectedRow.nameWidth+semanticRowGap+selectedRow.detailWidth > textWidth { + preview[1] = selected.Detail } - preview := [2]string{previewLabel, previewDetail} - height := 2*semanticPopupPad + float32(last-first)*(semanticRowHeight+semanticListGap) - semanticListGap - showPreview := (previewLabel != "" || previewDetail != "") && previewHeight >= completionPreviewLineHeight+2*completionPreviewPadY - if showPreview { - var scratch [completionPreviewLines]string - style := gfx.TextStyle{Size: completionLabelSize} - var lineCount int - for _, part := range preview { - lines := canvas.WrapLinesStyle(scratch[:0], part, style, textWidth, len(scratch)-lineCount, nil) - lineCount += len(lines) - } - previewHeight = min(previewHeight, float32(lineCount)*completionPreviewLineHeight+2*completionPreviewPadY) + previewHeight := completionPreviewHeight(canvas, preview, textWidth) + height := popupInsets + float32(last-first)*rowStep - semanticListGap + if previewHeight > 0 { height += previewHeight + semanticListGap } + // Choose a side before limiting the row window. Clamping a too-tall + // popup after placement can make it cover the line being edited. + placeAbove := height > below && above > below + available := below + if placeAbove { + available = above + } + previewHeight = min(previewHeight, available-popupInsets-semanticRowHeight-semanticListGap) + footerHeight := float32(0) + if previewHeight >= completionPreviewMinHeight { + footerHeight = previewHeight + semanticListGap + } + maxRows := max(1, int((available-popupInsets-footerHeight+semanticListGap)/rowStep)) + from := min(max(first, state.selected-maxRows+1), max(first, last-maxRows)) + to := min(last, from+maxRows) + height = popupInsets + float32(to-from)*rowStep - semanticListGap + footerHeight x := min(max(anchor.X, bounds.X), bounds.Right()-width) - y := anchor.Bottom() + 2 - if y+height > bounds.Bottom() && anchor.Y-2-bounds.Y > bounds.Bottom()-y { - y = anchor.Y - 2 - height + y := anchor.Bottom() + caretGap + if placeAbove { + y = anchor.Y - caretGap - height } - y = min(max(y, bounds.Y), bounds.Bottom()-height) chosen := -1 language := a.language() a.semanticPopup(c, x, y, width, height, i18n.Shared(language, "desktop.semantics.completion"), semanticPopupPad, func() { ui.Column(c).FillWidth().Gap(semanticListGap).Children(func() { - for i, item := range state.items[first:last] { - index := first + i - row := rows[i] + for index := from; index < to; index++ { + item := state.items[index] + row := rows[index-first] if a.semanticRow(c, index, item.Label, index == state.selected, func(rc ui.C) { nameWidth := min(row.nameWidth, textWidth) - ui.Text(rc, item.Label).FontSize(completionLabelSize).Width(nameWidth).Shrink(0).Ellipsis() - if row.detail != "" && textWidth-nameWidth-semanticRowGap >= min(row.detailWidth, completionDetailSize*3) { + ui.Text(rc, row.name).FontSize(completionLabelSize).Width(nameWidth).Shrink(0).Ellipsis() + detailRoom := textWidth - nameWidth - semanticRowGap + if row.detail != "" && detailRoom >= min(row.detailWidth, completionDetailSize*3) { ui.Text(rc, row.detail).FontSize(completionDetailSize).Color(a.colors.Subtle). - Grow(1).Shrink(1).MinWidth(0).Ellipsis() + Width(detailRoom).Shrink(0).Ellipsis() } }) { chosen = index } } }) - if showPreview { - pc := c.Part("completion-preview").KeyInt(state.selected) - ui.Divider(pc).Height(semanticListGap) - ui.Scroll(pc).FillWidth().Height(previewHeight).Scrollbars(). - ScrollbarLabels(i18n.Shared(language, "desktop.editor.verticalScrollbar"), i18n.Shared(language, "desktop.editor.horizontalScrollbar")). - Children(func() { - ui.Column(pc).FillWidth().PadX(semanticRowPad).PadY(completionPreviewPadY).Children(func() { - for _, part := range preview { - if part != "" { - ui.Wrap(pc, part).FillWidth().FontSize(completionLabelSize).LineHeight(completionPreviewLineHeight) - } - } - }) - }) + if footerHeight > 0 { + ui.Divider(c).Height(semanticListGap) + ui.Column(c.KeyInt(state.selected)).FillWidth().Height(previewHeight).Children(func() { + a.completionPreview(c, preview, previewHeight) + }) } }) if chosen >= 0 { @@ -239,6 +261,88 @@ func (a *App) completionPopup(c ui.C) gfx.Rect { return gfx.Rect{X: x, Y: y, Width: width, Height: height} } +func completionPreviewHeight(canvas *gfx.Canvas, text [2]string, width float32) float32 { + var scratch [completionPreviewLines]string + lineCount := 0 + for _, part := range text { + if len(part) > completionTextBlockBytes { + return completionPreviewMaxHeight + } + lines := canvas.WrapLines(scratch[:0], part, completionLabelSize, gfx.WeightRegular, width, len(scratch)-lineCount) + lineCount += len(lines) + } + if lineCount == 0 { + return 0 + } + return float32(lineCount)*completionPreviewLineHeight + 2*completionPreviewPadY +} + +func (a *App) completionPreview(c ui.C, text [2]string, height float32) { + first := completionBlockCount(text[0]) + total := first + completionBlockCount(text[1]) + language := a.language() + vertical := i18n.Shared(language, "desktop.editor.verticalScrollbar") + horizontal := i18n.Shared(language, "desktop.editor.horizontalScrollbar") + if len(text[0]) <= completionTextBlockBytes && len(text[1]) <= completionTextBlockBytes { + ui.Scroll(c).FillWidth().Height(height).Scrollbars().ScrollbarLabels(vertical, horizontal).Children(func() { + ui.Column(c).FillWidth().PadX(semanticRowPad).PadY(completionPreviewPadY).Children(func() { + for _, part := range text { + if part != "" { + ui.Wrap(c, part).FillWidth().FontSize(completionLabelSize).LineHeight(completionPreviewLineHeight) + } + } + }) + }) + return + } + + list := ui.State[ui.ListState](c, "preview-blocks") + ui.List(c, list, total, func(row ui.ListRow) { + part, index := 0, row.Index + if index >= first { + part, index = 1, index-first + } + row.Element.PadX(semanticRowPad).Items(ui.Start). + HitWith(row.ID, "", gfx.HitOptions{Role: gfx.RoleGroup, SkipFocus: true}) + if row.Index == 0 { + row.Element.PadTop(completionPreviewPadY) + } + if row.Index == total-1 { + row.Element.PadBottom(completionPreviewPadY) + } + start := completionBlockStart(text[part], index) + end := completionBlockStart(text[part], index+1) + value := text[part][start:end] + ui.Wrap(row.C, value).Grow(1).MinWidth(0).FontSize(completionLabelSize).LineHeight(completionPreviewLineHeight) + }).FillWidth().Height(height).Scrollbars().ScrollbarLabels(vertical, horizontal) +} + +func completionBlockCount(value string) int { + return (len(value) + completionTextBlockBytes - 1) / completionTextBlockBytes +} + +// Block offsets are calculated independently so the scrollbar can jump without +// reading preceding text. Prefer a nearby whitespace break, then a grapheme +// boundary in the same bounded window. +func completionBlockStart(value string, index int) int { + if index == 0 { + return 0 + } + offset := index * completionTextBlockBytes + if offset >= len(value) { + return len(value) + } + const lookBehind = 64 + start := offset - lookBehind + if at := strings.LastIndexAny(value[start:offset], " \t\n"); at >= 0 { + return start + at + 1 + } + for !utf8.RuneStart(value[start]) { + start++ + } + return start + gfx.GraphemeStart(value[start:min(offset+utf8.UTFMax, len(value))], offset-start) +} + // renameDialog is the thin view over renameState: it shows the name field and // reports confirm or cancel to the rename logic. func (a *App) renameDialog(c ui.C) { diff --git a/desktop/internal/workbench/signature_help.go b/desktop/internal/workbench/signature_help.go index b8e628f8cf..81f560fe04 100644 --- a/desktop/internal/workbench/signature_help.go +++ b/desktop/internal/workbench/signature_help.go @@ -370,8 +370,9 @@ func (a *App) signatureHelpPopup(c ui.C, completion gfx.Rect) { paramDocSize = 11.5 signatureDocSize = 11 ) - bounds := c.Bounds().Inset(gfx.All(4)) - if bounds.Width <= 2*padX { + const horizontalInsets = 2 * (padX + semanticPopupBorder) + bounds := c.Bounds().Inset(gfx.All(semanticPopupMargin)) + if bounds.Width <= horizontalInsets { st.rect = gfx.Rect{} return } @@ -382,7 +383,7 @@ func (a *App) signatureHelpPopup(c ui.C, completion gfx.Rect) { overloadRoom = 96 } width := min(max(labelRunes*charWidth+overloadRoom, signatureHelpMinWidth), signatureHelpMaxWidth, bounds.Width) - height := float32(headerHeight + 2*padY) + height := float32(headerHeight + 2*(padY+semanticPopupBorder)) hasDocs := paramDoc != "" || sigDoc != "" if hasDocs { height += gap + 1 @@ -393,50 +394,24 @@ func (a *App) signatureHelpPopup(c ui.C, completion gfx.Rect) { }{{paramDoc, paramDocSize}, {sigDoc, signatureDocSize}} { if doc.text != "" { lines := c.Canvas().WrapLinesStyle(scratch[:0], doc.text, gfx.TextStyle{Size: doc.size}, - width-2*padX, utf8.RuneCountInString(doc.text), nil) + width-horizontalInsets, utf8.RuneCountInString(doc.text), nil) height += float32(len(lines))*docLineHeight + gap } } } - if height > bounds.Height { - st.rect = gfx.Rect{} + rect := signatureHelpPosition(bounds, anchor, completion, width, height) + st.rect = rect + if rect.Height == 0 { return } - x := min(max(anchor.X, bounds.X), bounds.Right()-width) - y := anchor.Y - height - 4 - if completion.Height == 0 { - if y < bounds.Y { - y = min(anchor.Bottom()+4, bounds.Bottom()-height) - } - } else if y < bounds.Y || (x < completion.Right() && x+width > completion.X && - y < completion.Bottom() && y+height > completion.Y) { - const popupGap = 8 - // The completion popup can grow a footer or move above the caret. Use its - // current placement when choosing where parameter information will fit. - switch { - case completion.Right()+popupGap+width <= bounds.Right(): - x, y = completion.Right()+popupGap, completion.Y - case completion.X-popupGap-width >= bounds.X: - x, y = completion.X-popupGap-width, completion.Y - case completion.Bottom()+popupGap+height <= bounds.Bottom(): - y = completion.Bottom() + popupGap - case completion.Y-popupGap-height >= bounds.Y: - y = completion.Y - popupGap - height - default: - st.rect = gfx.Rect{} - return - } - } - y = min(max(y, bounds.Y), bounds.Bottom()-height) - st.rect = gfx.Rect{X: x, Y: y, Width: width, Height: height} lang := a.language() title := i18n.Shared(lang, "desktop.semantics.signatureHelp") stepDelta := 0 ui.Stack(c.Part("signature-help-layer")).Fill().Children(func() { sc := c.Part("signature-help-card") - ui.Column(sc).Absolute().Left(x).Top(y).Width(width).Height(height).PadX(padX).PadY(padY).Gap(gap). - Background(a.theme.Popover).Border(1, a.theme.Border). + ui.Column(sc).Absolute().Left(rect.X).Top(rect.Y).Width(width).Height(height).PadX(padX).PadY(padY).Gap(gap). + Background(a.theme.Popover).Border(semanticPopupBorder, a.theme.Border). Radius(a.theme.Metrics.RadiusPopup()).Shadow(gfx.ShadowMD). HitWith(ui.ID(sc), title, gfx.HitOptions{Role: gfx.RoleGroup, SkipFocus: true}). Children(func() { @@ -503,3 +478,37 @@ func (a *App) signatureHelpPopup(c ui.C, completion gfx.Rect) { c.After(func() { a.stepSignatureOverload(stepDelta) }) } } + +// signatureHelpPosition keeps the active editor line visible even when the +// completion popup has moved above it. If no position fits, completion wins. +func signatureHelpPosition(bounds, caret, completion gfx.Rect, width, height float32) gfx.Rect { + const caretGap, popupGap = 4, 8 + x := min(max(caret.X, bounds.X), bounds.Right()-width) + above := caret.Y - caretGap - height + below := caret.Bottom() + caretGap + positions := [...]gfx.Point{ + {X: x, Y: above}, + {X: x, Y: below}, + {X: completion.Right() + popupGap, Y: above}, + {X: completion.X - popupGap - width, Y: above}, + {X: completion.Right() + popupGap, Y: below}, + {X: completion.X - popupGap - width, Y: below}, + {X: x, Y: max(completion.Bottom()+popupGap, below)}, + {X: x, Y: min(completion.Y-popupGap-height, above)}, + } + options := positions[:] + if completion.Height == 0 { + options = options[:2] + } + for _, origin := range options { + if origin.X < bounds.X || origin.X+width > bounds.Right() || origin.Y < bounds.Y || origin.Y+height > bounds.Bottom() { + continue + } + if completion.Height > 0 && origin.X < completion.Right() && origin.X+width > completion.X && + origin.Y < completion.Bottom() && origin.Y+height > completion.Y { + continue + } + return gfx.Rect{X: origin.X, Y: origin.Y, Width: width, Height: height} + } + return gfx.Rect{} +} diff --git a/desktop/internal/workbench/visual_capture_test.go b/desktop/internal/workbench/visual_capture_test.go index 72eac4018d..73ffe27aab 100644 --- a/desktop/internal/workbench/visual_capture_test.go +++ b/desktop/internal/workbench/visual_capture_test.go @@ -70,15 +70,18 @@ func TestVisualCompletionPopup(t *testing.T) { t.Fatal(err) } for _, scene := range []struct { - theme, language string - width int + name, theme, language string + width, caretLine int }{ - {"light", "en-US", 1000}, - {"dark", "zh-Hans", 1000}, - {"light", "zh-Hans", 350}, - {"dark", "en-US", 350}, + {"completion", "light", "en-US", 1000, 0}, + {"completion", "dark", "zh-Hans", 1000, 0}, + {"completion", "light", "zh-Hans", 350, 0}, + {"completion", "dark", "en-US", 350, 0}, + {"completion-center", "light", "en-US", 900, 10}, + {"completion-signature", "dark", "zh-Hans", 900, 19}, } { - t.Run(scene.theme+"-"+scene.language, func(t *testing.T) { + name := scene.name + "-" + scene.theme + "-" + scene.language + t.Run(name, func(t *testing.T) { prefs := store.At(t.TempDir()) if err := prefs.Save(settingsFile, map[string]string{"theme": scene.theme}); err != nil { t.Fatal(err) @@ -96,11 +99,19 @@ func TestVisualCompletionPopup(t *testing.T) { return session.Driver(), session.Close, nil }) f := openSemanticFixture(t, h, kernel) - f.openFunctionCompletions(t) - if err := h.driver.Key(gfx.KeyDown, 0); err != nil { - t.Fatal(err) + if scene.caretLine == 0 { + f.openFunctionCompletions(t) + if err := h.driver.Key(gfx.KeyDown, 0); err != nil { + t.Fatal(err) + } + } else { + f.placeCompletionCaret(t, scene.caretLine) + f.requestCompletions(t, longFunctionCandidates()...) + if scene.name == "completion-signature" { + f.openCompletionCaretSignature(t) + } } - if err := session.Screenshot(filepath.Join(out, "completion-"+scene.theme+"-"+scene.language+".png")); err != nil { + if err := session.Screenshot(filepath.Join(out, name+".png")); err != nil { t.Fatal(err) } }) diff --git a/shared/platform-feature-matrix/features/lsp-completion-hover.json b/shared/platform-feature-matrix/features/lsp-completion-hover.json index 4aedebd78e..047607f500 100644 --- a/shared/platform-feature-matrix/features/lsp-completion-hover.json +++ b/shared/platform-feature-matrix/features/lsp-completion-hover.json @@ -32,7 +32,7 @@ ] }, "owner": "Language Tooling", - "verification": "使用合成语言服务返回名称相近、签名较长的候选。在常规及窄窗口检查完整名称,窄窗口中用方向键切换并阅读换行的完整签名,按回车确认插入所选候选。检查补全弹窗与参数信息同时可见时的位置。 Viva(2026-11-16,#1995):指针停留触发 Hover 的等待从 500 ms 缩短到 300 ms,与 Zed、VS Code 的默认值一致;F1、离开即取消和同一字符内不重复请求的行为不变。在 macOS 打包应用里接真实语言服务器实测,指针落到字符上到弹窗出现的中位数为 360 ms(改动前 562 ms,各 5 次,扣除等待后都约 60 ms)。Windows 共用同一份 Viva 代码,原生窗口验收待做。 Core 6a14126(2026-11-16,Lithe-core-rust #61,#1995):LSP 引擎为每个语言服务器会话保留悬浮答案;同一文档版本的同一位置,或答案 range 在同一行内覆盖的位置,再次悬浮时直接返回保留的答案,不再发给服务器。每个会话最多 32 个答案(最久没用的先丢),单个答案超过 64 KiB 不保留,空答案、错误和缓存清空之后才到达的答案不保留;文档打开、修改、保存、关闭,工作区文件变化,配置或索引排除项变化,executeCommand,插件后端的 languageServer/request 与 languageServer/notify,宿主通知写给插件后端,服务器发来的任何请求或 $/progress end,以及会话停止、崩溃或重启时清空。Core 的单元测试和脚本化服务器会话测试覆盖命中、范围、淘汰和清空,两端共用同一个 Core;原生窗口验收待做。 Viva(2026-11-16,#1995):指针离开文本后忘掉停留过的字符:按 Esc、移出窗口、移到标签栏等其他控件、滚动编辑器后回到同一字符,会重新请求并显示 Hover;离开时还在 500 ms 等待中的 Hover 不再发出请求;没有 Hover 的字符内移动仍不重复请求。无头测试覆盖四种离开方式,原生窗口验收待做。 Viva(2026-11-14):Hover 弹窗按 Markdown 渲染(签名代码块、分隔线、正文、示例、链接),代码块用编辑器同一套语法着色,http/https/mailto 链接点击后用系统浏览器打开;宽度在 280–620 之间按内容收窄,超过 420 高度时可滚动。弹窗贴在指针所在行的下沿、左缘对齐指针;指针停在同一符号范围内不重新请求,移入弹窗时保持打开,离开符号和弹窗 400 ms 后才关闭,期间回到符号或弹窗会取消关闭。无头测试覆盖渲染、位置、保持、延迟关闭和代码着色,原生窗口验收待做。 Viva(2026-11-10):输入单个标识符字符或 . : > 后防抖(120 ms)自动请求补全,继续输入、移动光标或编辑会取消并丢弃过期结果;服务器声明的 triggerCharacters 尚未由 Core 暴露,暂用固定成员访问字符集。 Viva(2026-11-09):Hover 支持 F1 与 500 ms 指针停留,补全由 Control+Space 显式触发,上下键选择、回车/Tab 通过 Core 的 lsp.applyTextEdits 与 lsp.plainSnippet 接受;无头 Viva 窗口使用真实 clangd 隔离插件验证了 Hover 内容、补全列表和接受后的文本与光标位置。连续输入自动重新请求、isIncomplete 刷新、触发字符和多来源合并尚未接入,因此保持部分实现。 用测试内联的合成语言服务器插件验证补全、Hover、排序、超时和空结果。验证 initialize、didChangeConfiguration 与 workspace/configuration 三个配置入口一致,父级和叶级 section 均保留插件声明的其他配置。 连续输入时保留 isIncomplete 并通过 LSP triggerKind=3 重新请求候选;验证空未完整列表、完整/空响应、触发字符、快速修改及文档关闭/会话替换后的过期结果。 #1662 LSP-09:多个补全来源的合并由 Core 的 lsp.mergeCompletions 决定:同一来源的候选全部保留(同名不同签名的重载不再被丢弃),低优先级来源中与高优先级来源同 label 的候选被遮蔽。Windows 只有一个补全来源,原样显示服务器列表,不调用该命令。macOS 删除私有的 Go/Swift/Rust/Python/JS/TS 关键词表,关键词改由语言服务器或插件提供,与 Windows 一致。在 macOS 打开有重载方法的文件,确认同名重载全部出现;未运行语言服务器的 Go 文件不再出现内置关键词候选,当前文件标识符仍会出现。", + "verification": "使用合成语言服务返回名称相近、签名较长的候选。在常规及窄窗口检查完整名称,窄窗口中用方向键切换并阅读换行的完整签名,按回车确认插入所选候选。验证候选恰好填满行宽时仍可读到完整签名,滚动超长详情可读到末尾,候选和详情较高时弹窗避开输入行。 Viva(2026-11-16,#1995):指针停留触发 Hover 的等待从 500 ms 缩短到 300 ms,与 Zed、VS Code 的默认值一致;F1、离开即取消和同一字符内不重复请求的行为不变。在 macOS 打包应用里接真实语言服务器实测,指针落到字符上到弹窗出现的中位数为 360 ms(改动前 562 ms,各 5 次,扣除等待后都约 60 ms)。Windows 共用同一份 Viva 代码,原生窗口验收待做。 Core 6a14126(2026-11-16,Lithe-core-rust #61,#1995):LSP 引擎为每个语言服务器会话保留悬浮答案;同一文档版本的同一位置,或答案 range 在同一行内覆盖的位置,再次悬浮时直接返回保留的答案,不再发给服务器。每个会话最多 32 个答案(最久没用的先丢),单个答案超过 64 KiB 不保留,空答案、错误和缓存清空之后才到达的答案不保留;文档打开、修改、保存、关闭,工作区文件变化,配置或索引排除项变化,executeCommand,插件后端的 languageServer/request 与 languageServer/notify,宿主通知写给插件后端,服务器发来的任何请求或 $/progress end,以及会话停止、崩溃或重启时清空。Core 的单元测试和脚本化服务器会话测试覆盖命中、范围、淘汰和清空,两端共用同一个 Core;原生窗口验收待做。 Viva(2026-11-16,#1995):指针离开文本后忘掉停留过的字符:按 Esc、移出窗口、移到标签栏等其他控件、滚动编辑器后回到同一字符,会重新请求并显示 Hover;离开时还在 500 ms 等待中的 Hover 不再发出请求;没有 Hover 的字符内移动仍不重复请求。无头测试覆盖四种离开方式,原生窗口验收待做。 Viva(2026-11-14):Hover 弹窗按 Markdown 渲染(签名代码块、分隔线、正文、示例、链接),代码块用编辑器同一套语法着色,http/https/mailto 链接点击后用系统浏览器打开;宽度在 280–620 之间按内容收窄,超过 420 高度时可滚动。弹窗贴在指针所在行的下沿、左缘对齐指针;指针停在同一符号范围内不重新请求,移入弹窗时保持打开,离开符号和弹窗 400 ms 后才关闭,期间回到符号或弹窗会取消关闭。无头测试覆盖渲染、位置、保持、延迟关闭和代码着色,原生窗口验收待做。 Viva(2026-11-10):输入单个标识符字符或 . : > 后防抖(120 ms)自动请求补全,继续输入、移动光标或编辑会取消并丢弃过期结果;服务器声明的 triggerCharacters 尚未由 Core 暴露,暂用固定成员访问字符集。 Viva(2026-11-09):Hover 支持 F1 与 500 ms 指针停留,补全由 Control+Space 显式触发,上下键选择、回车/Tab 通过 Core 的 lsp.applyTextEdits 与 lsp.plainSnippet 接受;无头 Viva 窗口使用真实 clangd 隔离插件验证了 Hover 内容、补全列表和接受后的文本与光标位置。连续输入自动重新请求、isIncomplete 刷新、触发字符和多来源合并尚未接入,因此保持部分实现。 用测试内联的合成语言服务器插件验证补全、Hover、排序、超时和空结果。验证 initialize、didChangeConfiguration 与 workspace/configuration 三个配置入口一致,父级和叶级 section 均保留插件声明的其他配置。 连续输入时保留 isIncomplete 并通过 LSP triggerKind=3 重新请求候选;验证空未完整列表、完整/空响应、触发字符、快速修改及文档关闭/会话替换后的过期结果。 #1662 LSP-09:多个补全来源的合并由 Core 的 lsp.mergeCompletions 决定:同一来源的候选全部保留(同名不同签名的重载不再被丢弃),低优先级来源中与高优先级来源同 label 的候选被遮蔽。Windows 只有一个补全来源,原样显示服务器列表,不调用该命令。macOS 删除私有的 Go/Swift/Rust/Python/JS/TS 关键词表,关键词改由语言服务器或插件提供,与 Windows 一致。在 macOS 打开有重载方法的文件,确认同名重载全部出现;未运行语言服务器的 Go 文件不再出现内置关键词候选,当前文件标识符仍会出现。", "notes": "Viva 已接入显式补全与接受(含 Core 的 snippet 转换)以及 Hover(F1 与指针停留);isIncomplete 连续请求、触发字符和多来源合并尚未接入。无头 clangd 测试不是原生窗口验收,平台验证保持 pending。 自动触发已接入,触发字符集暂为固定值,待 Core 暴露服务器声明。", "legacy": { "revision": "5a54c41aa37a4e6e6b68918a4b05765b9dd07a36", diff --git a/shared/platform-feature-matrix/features/lsp-signature-help.json b/shared/platform-feature-matrix/features/lsp-signature-help.json index e7cb7165c6..a04bc6d3da 100644 --- a/shared/platform-feature-matrix/features/lsp-signature-help.json +++ b/shared/platform-feature-matrix/features/lsp-signature-help.json @@ -22,6 +22,6 @@ ] }, "owner": "Editor / Language Tooling", - "verification": "以声明该能力的合成语言服务器验证参数信息快捷键、触发字符、当前参数高亮、重载切换、离开调用和 Esc 关闭。 在窄窗口同时打开带长签名详情的补全弹窗,确认两块内容都可见且弹窗互不遮挡。", + "verification": "以声明该能力的合成语言服务器验证参数信息快捷键、触发字符、当前参数高亮、重载切换、离开调用和 Esc 关闭。 在窄窗口同时打开带长签名详情的补全弹窗,确认弹窗互不遮挡。将光标移到窗口底部,验证两个弹窗均避开输入行,换行说明留在卡片边框内。", "notes": "Viva 在 macOS 和 Windows 共享实现。原生平台验证待补。" }