Skip to content

Commit

Permalink
Fix go-to-definition within same document (goplus#1224)
Browse files Browse the repository at this point in the history
* fix code-editor open / go-to-definition

* fix HTML string from language server
  • Loading branch information
nighca authored Jan 10, 2025
1 parent eeaafc5 commit 2d47003
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion tools/spxls/internal/server/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("<resource-preview resource=%q />\n", u)
return fmt.Sprintf("<resource-preview resource=%s />\n", attr(string(u)))
}

// SpxGetDefinitionsParams represents parameters to get definitions at a
Expand Down
2 changes: 1 addition & 1 deletion tools/spxls/internal/server/spx_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type SpxDefinition struct {

// HTML returns the HTML representation of the definition.
func (def SpxDefinition) HTML() string {
return fmt.Sprintf("<definition-item def-id=%q overview=%q>\n%s</definition-item>\n", def.ID, def.Overview, def.Detail)
return fmt.Sprintf("<definition-item def-id=%s overview=%s>\n%s</definition-item>\n", attr(def.ID.String()), attr(def.Overview), def.Detail)
}

// CompletionItem constructs a [CompletionItem] from the definition.
Expand Down
6 changes: 6 additions & 0 deletions tools/spxls/internal/server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"fmt"
"go/types"
"html/template"
"io/fs"
"regexp"
"strings"
Expand Down Expand Up @@ -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))
}

0 comments on commit 2d47003

Please sign in to comment.