Skip to content

Commit

Permalink
refactor(tools/spxls): centralize source location handling in `compil…
Browse files Browse the repository at this point in the history
…eResult` (#1273)

Signed-off-by: Aofei Sheng <[email protected]>
  • Loading branch information
aofei authored Jan 26, 2025
1 parent 7294c9a commit c0ed9c5
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 175 deletions.
226 changes: 125 additions & 101 deletions tools/spxls/internal/server/compile.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions tools/spxls/internal/server/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func (s *Server) textDocumentDefinition(params *DefinitionParams) (any, error) {
if !result.isInFset(objPos) {
return nil, nil
}
return s.createLocationFromPos(result.fset, objPos), nil
return result.locationForPos(objPos), nil
} else if !result.isInFset(defIdent.Pos()) {
return nil, nil
}
return s.createLocationFromIdent(result.fset, defIdent), nil
return result.locationForNode(defIdent), nil
}

// See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_typeDefinition
Expand Down Expand Up @@ -64,5 +64,5 @@ func (s *Server) textDocumentTypeDefinition(params *TypeDefinitionParams) (any,
if !result.isInFset(objPos) {
return nil, nil
}
return s.createLocationFromPos(result.fset, objPos), nil
return result.locationForPos(objPos), nil
}
16 changes: 4 additions & 12 deletions tools/spxls/internal/server/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@ func (s *Server) textDocumentDocumentLink(params *DocumentLinkParams) (links []D
// Add links for spx resource references.
links = slices.Grow(links, len(result.spxResourceRefs))
for _, spxResourceRef := range result.spxResourceRefs {
nodePos := result.fset.Position(spxResourceRef.Node.Pos())
if nodePos.Filename != spxFile {
if result.nodeFilename(spxResourceRef.Node) != spxFile {
continue
}
target := URI(spxResourceRef.ID.URI())
links = append(links, DocumentLink{
Range: Range{
Start: FromGopTokenPosition(nodePos),
End: FromGopTokenPosition(result.fset.Position(spxResourceRef.Node.End())),
},
Range: result.rangeForNode(spxResourceRef.Node),
Target: &target,
Data: SpxResourceRefDocumentLinkData{
Kind: spxResourceRef.Kind,
Expand All @@ -48,15 +44,11 @@ func (s *Server) textDocumentDocumentLink(params *DocumentLinkParams) (links []D
// Add links for spx definitions.
links = slices.Grow(links, len(result.typeInfo.Defs)+len(result.typeInfo.Uses))
addLinksForIdent := func(ident *gopast.Ident) {
identPos := result.fset.Position(ident.Pos())
if identPos.Filename != spxFile {
if result.nodeFilename(ident) != spxFile {
return
}
if spxDefs := result.spxDefinitionsForIdent(ident); spxDefs != nil {
identRange := Range{
Start: FromGopTokenPosition(identPos),
End: FromGopTokenPosition(result.fset.Position(ident.End())),
}
identRange := result.rangeForNode(ident)
for _, spxDef := range spxDefs {
target := URI(spxDef.ID.String())
links = append(links, DocumentLink{
Expand Down
7 changes: 2 additions & 5 deletions tools/spxls/internal/server/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,8 @@ func (s *Server) textDocumentDocumentHighlight(params *DocumentHighlightParams)
}

highlights = append(highlights, DocumentHighlight{
Range: Range{
Start: FromGopTokenPosition(result.fset.Position(ident.Pos())),
End: FromGopTokenPosition(result.fset.Position(ident.End())),
},
Kind: kind,
Range: result.rangeForNode(ident),
Kind: kind,
})
return true
})
Expand Down
10 changes: 2 additions & 8 deletions tools/spxls/internal/server/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ func (s *Server) textDocumentHover(params *HoverParams) (*Hover, error) {
Kind: Markdown,
Value: spxResourceRef.ID.URI().HTML(),
},
Range: Range{
Start: FromGopTokenPosition(result.fset.Position(spxResourceRef.Node.Pos())),
End: FromGopTokenPosition(result.fset.Position(spxResourceRef.Node.End())),
},
Range: result.rangeForNode(spxResourceRef.Node),
}, nil
}

Expand All @@ -44,9 +41,6 @@ func (s *Server) textDocumentHover(params *HoverParams) (*Hover, error) {
Kind: Markdown,
Value: hoverContent.String(),
},
Range: Range{
Start: FromGopTokenPosition(result.fset.Position(ident.Pos())),
End: FromGopTokenPosition(result.fset.Position(ident.End())),
},
Range: result.rangeForNode(ident),
}, nil
}
4 changes: 2 additions & 2 deletions tools/spxls/internal/server/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (s *Server) textDocumentImplementation(params *ImplementationParams) (any,
}
}

return s.createLocationFromPos(result.fset, obj.Pos()), nil
return result.locationForPos(obj.Pos()), nil
}

// findImplementingMethodDefinitions finds the definition locations of all
Expand All @@ -49,7 +49,7 @@ func (s *Server) findImplementingMethodDefinitions(result *compileResult, iface
continue
}

implementations = append(implementations, s.createLocationFromPos(result.fset, method.Pos()))
implementations = append(implementations, result.locationForPos(method.Pos()))
}
}
return implementations
Expand Down
8 changes: 8 additions & 0 deletions tools/spxls/internal/server/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func FromGopTokenPosition(p goptoken.Position) Position {
}
}

// RangeForGopTokenPosition returns a [Range] for the given [goptoken.Position].
func RangeForGopTokenPosition(pos goptoken.Position) Range {
return Range{
Start: FromGopTokenPosition(pos),
End: FromGopTokenPosition(pos),
}
}

// toURI converts a string to a [URI].
func toURI(s string) *URI {
u := URI(s)
Expand Down
6 changes: 3 additions & 3 deletions tools/spxls/internal/server/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func (s *Server) textDocumentReferences(params *ReferenceParams) ([]Location, er
if defIdent == nil {
objPos := obj.Pos()
if result.isInFset(objPos) {
locations = append(locations, s.createLocationFromPos(result.fset, objPos))
locations = append(locations, result.locationForPos(objPos))
}
} else if result.isInFset(defIdent.Pos()) {
locations = append(locations, s.createLocationFromIdent(result.fset, defIdent))
locations = append(locations, result.locationForNode(defIdent))
}
}

Expand All @@ -53,7 +53,7 @@ func (s *Server) findReferenceLocations(result *compileResult, obj types.Object)
}
locations := make([]Location, 0, len(refIdents))
for _, refIdent := range refIdents {
locations = append(locations, s.createLocationFromIdent(result.fset, refIdent))
locations = append(locations, result.locationForNode(refIdent))
}
return locations
}
Expand Down
22 changes: 7 additions & 15 deletions tools/spxls/internal/server/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go/types"
"slices"

"github.com/goplus/builder/tools/spxls/internal/util"
gopast "github.com/goplus/gop/ast"
)

Expand All @@ -31,10 +32,7 @@ func (s *Server) textDocumentPrepareRename(params *PrepareRenameParams) (*Range,
return nil, nil
}

return &Range{
Start: FromGopTokenPosition(result.fset.Position(ident.Pos())),
End: FromGopTokenPosition(result.fset.Position(ident.End())),
}, nil
return util.ToPtr(result.rangeForNode(ident)), nil
}

// See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_rename
Expand Down Expand Up @@ -65,16 +63,13 @@ func (s *Server) textDocumentRename(params *RenameParams) (*WorkspaceEdit, error
return nil, fmt.Errorf("failed to find definition of object %q", obj.Name())
}

defLoc := s.createLocationFromIdent(result.fset, defIdent)
defLoc := result.locationForNode(defIdent)

workspaceEdit := WorkspaceEdit{
Changes: map[DocumentURI][]TextEdit{
defLoc.URI: {
{
Range: Range{
Start: FromGopTokenPosition(result.fset.Position(defIdent.Pos())),
End: FromGopTokenPosition(result.fset.Position(defIdent.End())),
},
Range: result.rangeForNode(defIdent),
NewText: params.NewName,
},
},
Expand Down Expand Up @@ -123,7 +118,7 @@ func (s *Server) spxRenameResourceAtRefs(result *compileResult, id SpxResourceID
nodeEnd.Column--
}

documentURI := s.toDocumentURI(nodePos.Filename)
documentURI := result.documentURIs[nodePos.Filename]
textEdit := TextEdit{
Range: Range{
Start: FromGopTokenPosition(nodePos),
Expand Down Expand Up @@ -173,12 +168,9 @@ func (s *Server) spxRenameSpriteResource(result *compileResult, id SpxSpriteReso
continue
}
if tv.Type.String() == "main."+id.SpriteName {
documentURI := s.toDocumentURI(result.fset.Position(expr.Pos()).Filename)
documentURI := result.nodeDocumentURI(expr)
textEdit := TextEdit{
Range: Range{
Start: FromGopTokenPosition(result.fset.Position(expr.Pos())),
End: FromGopTokenPosition(result.fset.Position(expr.End())),
},
Range: result.rangeForNode(expr),
NewText: newName,
}

Expand Down
26 changes: 0 additions & 26 deletions tools/spxls/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (

"github.com/goplus/builder/tools/spxls/internal/jsonrpc2"
"github.com/goplus/builder/tools/spxls/internal/vfs"
gopast "github.com/goplus/gop/ast"
goptoken "github.com/goplus/gop/token"
)

// MessageReplier is an interface for sending messages back to the client.
Expand Down Expand Up @@ -313,27 +311,3 @@ func (s *Server) fromDocumentURI(documentURI DocumentURI) (string, error) {
func (s *Server) toDocumentURI(path string) DocumentURI {
return DocumentURI(string(s.workspaceRootURI) + path)
}

// createLocationFromPos creates a Location from a position.
func (s *Server) createLocationFromPos(fset *goptoken.FileSet, pos goptoken.Pos) Location {
position := fset.Position(pos)
return Location{
URI: s.toDocumentURI(position.Filename),
Range: Range{
Start: FromGopTokenPosition(position),
End: FromGopTokenPosition(position),
},
}
}

// createLocationFromIdent creates a Location from an ident position.
func (s *Server) createLocationFromIdent(fset *goptoken.FileSet, ident *gopast.Ident) Location {
identPos := fset.Position(ident.Pos())
return Location{
URI: s.toDocumentURI(identPos.Filename),
Range: Range{
Start: FromGopTokenPosition(identPos),
End: FromGopTokenPosition(fset.Position(ident.End())),
},
}
}

0 comments on commit c0ed9c5

Please sign in to comment.