-
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.
- Loading branch information
Showing
9 changed files
with
228 additions
and
5 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,81 @@ | ||
package source | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/goccy/go-zetasql/ast" | ||
"github.com/goccy/go-zetasql/types" | ||
"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 | ||
} | ||
|
||
return []lsp.Location{{ | ||
URI: lsp.NewDocumentURI(uri), | ||
Range: bqlsLocationToLspLocation(locRange, sql.RawText), | ||
}}, nil | ||
} | ||
|
||
return nil, fmt.Errorf("not found") | ||
} | ||
|
||
func bqlsLocationToLspLocation(loc *types.ParseLocationRange, file string) lsp.Range { | ||
return lsp.Range{ | ||
Start: bqlsPointToLspPoint(loc.Start(), file), | ||
End: bqlsPointToLspPoint(loc.End(), file), | ||
} | ||
} | ||
|
||
func bqlsPointToLspPoint(point *types.ParseLocationPoint, file string) lsp.Position { | ||
return offsetToLspPoint(point.ByteOffset(), file) | ||
} | ||
|
||
func offsetToLspPoint(offset int, file string) lsp.Position { | ||
toEndText := file[:offset] | ||
line := strings.Count(toEndText, "\n") | ||
newLineInd := strings.LastIndex(toEndText, "\n") | ||
var char int | ||
if newLineInd == -1 { | ||
char = len(toEndText) | ||
} else { | ||
char = len(toEndText[newLineInd:]) - 1 | ||
} | ||
return lsp.Position{ | ||
Line: line, | ||
Character: char, | ||
} | ||
} |
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