From 2d47003028748b6ec43252ac504ac63c38627a5f Mon Sep 17 00:00:00 2001 From: Hanxing Yang Date: Fri, 10 Jan 2025 09:23:06 +0800 Subject: [PATCH] Fix go-to-definition within same document (#1224) * fix code-editor open / go-to-definition * fix HTML string from language server --- .../components/editor/code-editor/ui/code-editor-ui.ts | 8 ++++++-- tools/spxls/internal/server/protocol.go | 2 +- tools/spxls/internal/server/spx_definition.go | 2 +- tools/spxls/internal/server/util.go | 6 ++++++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/spx-gui/src/components/editor/code-editor/ui/code-editor-ui.ts b/spx-gui/src/components/editor/code-editor/ui/code-editor-ui.ts index 5f70d53d9..fe5365143 100644 --- a/spx-gui/src/components/editor/code-editor/ui/code-editor-ui.ts +++ b/spx-gui/src/components/editor/code-editor/ui/code-editor-ui.ts @@ -161,9 +161,13 @@ export class CodeEditorUI extends Disposable implements ICodeEditorUI { this.setActiveTextDocument(textDocument) if (positionOrRange == null) return if ('line' in positionOrRange) { - this.editor.setPosition(toMonacoPosition(positionOrRange)) + const mPos = toMonacoPosition(positionOrRange) + this.editor.setPosition(mPos) + this.editor.revealPositionNearTop(mPos) } else { - this.editor.setSelection(toMonacoRange(positionOrRange)) + const mRange = toMonacoRange(positionOrRange) + this.editor.setSelection(mRange) + this.editor.revealRangeNearTopIfOutsideViewport(mRange) } this.editor.focus() } diff --git a/tools/spxls/internal/server/protocol.go b/tools/spxls/internal/server/protocol.go index 6bd604a66..98eaae235 100644 --- a/tools/spxls/internal/server/protocol.go +++ b/tools/spxls/internal/server/protocol.go @@ -52,7 +52,7 @@ type SpxResourceURI string // HTML returns the HTML representation of the spx resource URI. func (u SpxResourceURI) HTML() string { - return fmt.Sprintf("\n", u) + return fmt.Sprintf("\n", attr(string(u))) } // SpxGetDefinitionsParams represents parameters to get definitions at a diff --git a/tools/spxls/internal/server/spx_definition.go b/tools/spxls/internal/server/spx_definition.go index 0f53fd2d8..c4ad4ede9 100644 --- a/tools/spxls/internal/server/spx_definition.go +++ b/tools/spxls/internal/server/spx_definition.go @@ -27,7 +27,7 @@ type SpxDefinition struct { // HTML returns the HTML representation of the definition. func (def SpxDefinition) HTML() string { - return fmt.Sprintf("\n%s\n", def.ID, def.Overview, def.Detail) + return fmt.Sprintf("\n%s\n", attr(def.ID.String()), attr(def.Overview), def.Detail) } // CompletionItem constructs a [CompletionItem] from the definition. diff --git a/tools/spxls/internal/server/util.go b/tools/spxls/internal/server/util.go index ed418a075..3a3ee08fb 100644 --- a/tools/spxls/internal/server/util.go +++ b/tools/spxls/internal/server/util.go @@ -3,6 +3,7 @@ package server import ( "fmt" "go/types" + "html/template" "io/fs" "regexp" "strings" @@ -268,3 +269,8 @@ func getSimplifiedTypeString(typ types.Type) string { return p.Name() }) } + +// attr transforms given string value to an HTML attribute value (with quotes). +func attr(value string) string { + return fmt.Sprintf(`"%s"`, template.HTMLEscapeString(value)) +}