Skip to content

Commit

Permalink
feat(tools/spxls): implement textDocument/completion
Browse files Browse the repository at this point in the history
Updates #1059

Signed-off-by: Aofei Sheng <[email protected]>
  • Loading branch information
aofei committed Dec 25, 2024
1 parent b64a5ab commit 55206a6
Show file tree
Hide file tree
Showing 11 changed files with 1,001 additions and 46 deletions.
1 change: 0 additions & 1 deletion tools/spxls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ For detailed API references, please check the [index.d.ts](index.d.ts) file.
| **Code Intelligence** |||
|| [`textDocument/hover`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_hover) | Shows types and documentation at cursor position. |
|| [`textDocument/completion`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_completion) | Generates context-aware code suggestions. |
|| [`completionItem/resolve`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#completionItem_resolve) | Provides detailed information for selected completion items. |
|| [`textDocument/signatureHelp`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_signatureHelp) | Shows function/method signature information. |
| **Symbols & Navigation** |||
|| [`textDocument/declaration`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_declaration) | Finds symbol declarations. |
Expand Down
6 changes: 1 addition & 5 deletions tools/spxls/internal/server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ func (s *Server) spxGetDefinitions(params []SpxGetDefinitionsParams) ([]SpxDefin
return nil, err
}
if astFile == nil {
diagnostics := result.diagnostics[param.TextDocument.URI]
if len(diagnostics) > 0 {
return nil, fmt.Errorf("failed to compile file: %s", diagnostics[0].Message)
}
return nil, nil
}
astFileScope := result.typeInfo.Scopes[astFile]
Expand Down Expand Up @@ -233,7 +229,7 @@ func (s *Server) spxGetDefinitions(params []SpxGetDefinitionsParams) ([]SpxDefin
})
case *types.Func:
var methodNames []string
if methodOverloads := expandGoptOverloadedMethod(member); len(methodOverloads) > 0 {
if methodOverloads := expandGopOverloadedMethod(member); len(methodOverloads) > 0 {
methodNames = make([]string, 0, len(methodOverloads))
for _, method := range methodOverloads {
_, methodName, _ := util.SplitGoptMethod(method.Name())
Expand Down
40 changes: 40 additions & 0 deletions tools/spxls/internal/server/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,46 @@ onStart => {
}))
})

t.Run("ParseError", func(t *testing.T) {
s := New(vfs.NewMapFS(func() map[string][]byte {
return map[string][]byte{
"main.spx": []byte(`
// Invalid syntax
var (
MySprite Sprite
`),
}
}), nil)

mainSpxFileScopeParams := []SpxGetDefinitionsParams{
{
TextDocumentPositionParams: TextDocumentPositionParams{
TextDocument: TextDocumentIdentifier{URI: "file:///main.spx"},
Position: Position{Line: 0, Character: 0},
},
},
}
mainSpxFileScopeDefs, err := s.spxGetDefinitions(mainSpxFileScopeParams)
require.NoError(t, err)
require.NotNil(t, mainSpxFileScopeDefs)
assert.True(t, spxDefinitionIdentifierSliceContains(mainSpxFileScopeDefs, SpxDefinitionIdentifier{
Package: util.ToPtr("builtin"),
Name: util.ToPtr("println"),
}))
assert.True(t, spxDefinitionIdentifierSliceContains(mainSpxFileScopeDefs, SpxDefinitionIdentifier{
Package: util.ToPtr("main"),
Name: util.ToPtr("MySprite"),
}))
assert.True(t, spxDefinitionIdentifierSliceContains(mainSpxFileScopeDefs, SpxDefinitionIdentifier{
Package: util.ToPtr(spxPkgPath),
Name: util.ToPtr("Game.onStart"),
}))
assert.False(t, spxDefinitionIdentifierSliceContains(mainSpxFileScopeDefs, SpxDefinitionIdentifier{
Package: util.ToPtr(spxPkgPath),
Name: util.ToPtr("Sprite.onStart"),
}))
})

t.Run("TrailingEmptyLinesOfSpriteCode", func(t *testing.T) {
s := New(vfs.NewMapFS(func() map[string][]byte {
return map[string][]byte{
Expand Down
39 changes: 18 additions & 21 deletions tools/spxls/internal/server/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,12 @@ func (s *Server) compile() (*compileResult, error) {
Mode: gopparser.AllErrors | gopparser.ParseComments,
})
if err != nil {
// Handle parse errors.
var parseErr gopscanner.ErrorList
var (
parseErr gopscanner.ErrorList
codeErr *gogen.CodeError
)
if errors.As(err, &parseErr) {
// Handle parse errors.
for _, e := range parseErr {
result.addDiagnostics(documentURI, Diagnostic{
Severity: SeverityError,
Expand All @@ -351,12 +354,8 @@ func (s *Server) compile() (*compileResult, error) {
Message: e.Msg,
})
}
continue
}

// Handle code generation errors.
var codeErr *gogen.CodeError
if errors.As(err, &codeErr) {
} else if errors.As(err, &codeErr) {
// Handle code generation errors.
position := codeErr.Fset.Position(codeErr.Pos)
result.addDiagnostics(documentURI, Diagnostic{
Severity: SeverityError,
Expand All @@ -366,21 +365,19 @@ func (s *Server) compile() (*compileResult, error) {
},
Message: codeErr.Error(),
})
continue
} else {
// Handle unknown errors.
result.addDiagnostics(documentURI, Diagnostic{
Severity: SeverityError,
Range: Range{
Start: Position{Line: 0, Character: 0},
End: Position{Line: 0, Character: 0},
},
Message: fmt.Sprintf("failed to parse spx file: %v", err),
})
}

// Handle unknown errors.
result.addDiagnostics(documentURI, Diagnostic{
Severity: SeverityError,
Range: Range{
Start: Position{Line: 0, Character: 0},
End: Position{Line: 0, Character: 0},
},
Message: fmt.Sprintf("failed to parse spx file: %v", err),
})
continue
}
if astFile.Name.Name == "main" {
if astFile != nil && astFile.Name.Name == "main" {
result.mainASTPkg.Files[spxFile] = astFile
if spxFileBaseName := path.Base(spxFile); spxFileBaseName == "main.spx" {
result.mainSpxFile = spxFile
Expand Down
Loading

0 comments on commit 55206a6

Please sign in to comment.