-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from kitagry/add-definition
feat: add definition
- Loading branch information
Showing
10 changed files
with
237 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package langserver | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/kitagry/bqls/langserver/internal/lsp" | ||
"github.com/sourcegraph/jsonrpc2" | ||
) | ||
|
||
func (h *Handler) handleTextDocumentDefinition(ctx context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result any, err error) { | ||
if req.Params == nil { | ||
return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeInvalidParams} | ||
} | ||
|
||
var params lsp.TextDocumentPositionParams | ||
if err := json.Unmarshal(*req.Params, ¶ms); err != nil { | ||
return nil, err | ||
} | ||
|
||
return h.project.LookupIdent(ctx, params.TextDocument.URI.ToURI(), params.Position) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package source | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/goccy/go-zetasql/ast" | ||
"github.com/kitagry/bqls/langserver/internal/lsp" | ||
"github.com/kitagry/bqls/langserver/internal/source/file" | ||
) | ||
|
||
func (p *Project) LookupIdent(ctx context.Context, uri string, position lsp.Position) ([]lsp.Location, error) { | ||
sql := p.cache.Get(uri) | ||
parsedFile := p.analyzer.ParseFile(uri, sql.RawText) | ||
|
||
termOffset := parsedFile.TermOffset(position) | ||
|
||
tablePathExpression, ok := file.SearchAstNode[*ast.TablePathExpressionNode](parsedFile.Node, termOffset) | ||
if !ok { | ||
return nil, fmt.Errorf("not found") | ||
} | ||
|
||
pathExpr := tablePathExpression.PathExpr() | ||
if pathExpr == nil { | ||
return nil, fmt.Errorf("not found") | ||
} | ||
|
||
tableNames := make([]string, len(pathExpr.Names())) | ||
for i, n := range tablePathExpression.PathExpr().Names() { | ||
tableNames[i] = n.Name() | ||
} | ||
tableName := strings.Join(tableNames, ".") | ||
|
||
withClauseEntries := file.ListAstNode[*ast.WithClauseEntryNode](parsedFile.Node) | ||
for _, entry := range withClauseEntries { | ||
if entry.Alias().Name() != tableName { | ||
continue | ||
} | ||
|
||
locRange := entry.Alias().ParseLocationRange() | ||
if locRange == nil { | ||
continue | ||
} | ||
|
||
r, ok := parsedFile.ToLspRange(locRange) | ||
if !ok { | ||
continue | ||
} | ||
|
||
return []lsp.Location{{ | ||
URI: lsp.NewDocumentURI(uri), | ||
Range: r, | ||
}}, nil | ||
} | ||
|
||
return nil, fmt.Errorf("not found") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package source_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
bq "cloud.google.com/go/bigquery" | ||
"github.com/golang/mock/gomock" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/kitagry/bqls/langserver/internal/bigquery/mock_bigquery" | ||
"github.com/kitagry/bqls/langserver/internal/lsp" | ||
"github.com/kitagry/bqls/langserver/internal/source" | ||
"github.com/kitagry/bqls/langserver/internal/source/helper" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func TestProject_LookupIdent(t *testing.T) { | ||
tests := map[string]struct { | ||
// prepare | ||
files map[string]string | ||
bqTableMetadata *bq.TableMetadata | ||
|
||
// output | ||
expectLocations []lsp.Location | ||
expectErr error | ||
}{ | ||
"definition to with clause": { | ||
files: map[string]string{ | ||
"a.sql": `WITH data AS ( SELECT 1 AS a ) | ||
SELECT a FROM data|`, | ||
}, | ||
bqTableMetadata: &bq.TableMetadata{ | ||
FullID: "project.dataset.table", | ||
Schema: bq.Schema{}, | ||
}, | ||
expectLocations: []lsp.Location{ | ||
{ | ||
URI: lsp.NewDocumentURI("a.sql"), | ||
Range: lsp.Range{ | ||
Start: lsp.Position{ | ||
Line: 0, | ||
Character: 5, | ||
}, | ||
End: lsp.Position{ | ||
Line: 0, | ||
Character: 9, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"definition to 2 with clause": { | ||
files: map[string]string{ | ||
"a.sql": `WITH data AS ( SELECT 1 AS a ), | ||
data2 AS (SELECT * FROM data ) | ||
SELECT a FROM data2|`, | ||
}, | ||
bqTableMetadata: &bq.TableMetadata{ | ||
FullID: "project.dataset.table", | ||
Schema: bq.Schema{}, | ||
}, | ||
expectLocations: []lsp.Location{ | ||
{ | ||
URI: lsp.NewDocumentURI("a.sql"), | ||
Range: lsp.Range{ | ||
Start: lsp.Position{ | ||
Line: 1, | ||
Character: 0, | ||
}, | ||
End: lsp.Position{ | ||
Line: 1, | ||
Character: 5, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for n, tt := range tests { | ||
t.Run(n, func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
bqClient := mock_bigquery.NewMockClient(ctrl) | ||
bqClient.EXPECT().GetTableMetadata(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(tt.bqTableMetadata, nil).MinTimes(0) | ||
logger := logrus.New() | ||
logger.SetLevel(logrus.DebugLevel) | ||
p := source.NewProjectWithBQClient("/", bqClient, logger) | ||
|
||
files, path, position, err := helper.GetLspPosition(tt.files) | ||
if err != nil { | ||
t.Fatalf("failed to get position: %v", err) | ||
} | ||
|
||
for uri, content := range files { | ||
p.UpdateFile(uri, content, 1) | ||
} | ||
|
||
got, err := p.LookupIdent(context.Background(), path, position) | ||
if !errors.Is(err, tt.expectErr) { | ||
t.Fatalf("got error %v, but want %v", err, tt.expectErr) | ||
} | ||
|
||
if diff := cmp.Diff(tt.expectLocations, got); diff != "" { | ||
t.Errorf("project.TermDocument result diff (-expect, +got)\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters