Skip to content

Commit

Permalink
Don't complete builtin function in table path expression
Browse files Browse the repository at this point in the history
  • Loading branch information
kitagry committed Aug 27, 2023
1 parent bf8f35c commit 5c5e320
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
9 changes: 9 additions & 0 deletions langserver/internal/source/completion/builtin_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ import (
"context"
"strings"

"github.com/goccy/go-zetasql/ast"
"github.com/kitagry/bqls/langserver/internal/function"
"github.com/kitagry/bqls/langserver/internal/lsp"
"github.com/kitagry/bqls/langserver/internal/source/file"
)

func (c *completor) completeBuiltinFunction(ctx context.Context, parsedFile file.ParsedFile, position lsp.Position) []CompletionItem {
termOffset := parsedFile.TermOffset(position)

// When the cursor is in the middle of the table path, do not suggest the built-in functions.
tablePathNode, ok := file.SearchAstNode[*ast.TablePathExpressionNode](parsedFile.Node, parsedFile.TermOffset(position))
if ok && tablePathNode.ParseLocationRange().End().ByteOffset() != termOffset {
return []CompletionItem{}
}

incompleteColumnName := parsedFile.FindIncompleteColumnName(position)

result := make([]CompletionItem, 0)
Expand Down
61 changes: 61 additions & 0 deletions langserver/internal/source/completion/builtin_function_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package completion

import (
"context"
"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"
"github.com/kitagry/bqls/langserver/internal/bigquery/mock_bigquery"
"github.com/kitagry/bqls/langserver/internal/source/file"
"github.com/kitagry/bqls/langserver/internal/source/helper"
"github.com/sirupsen/logrus"
)

func TestCompletor_CompleteBuiltinFunction(t *testing.T) {
tests := map[string]struct {
files map[string]string
bigqueryClientMockFunc func(t *testing.T) bigquery.Client

expectCompletionItems []CompletionItem
}{
"Don't complete in table completion": {
files: map[string]string{
"file1.sql": "SELECT FROM `bigquery-public-data.samples.|`\n",
},
bigqueryClientMockFunc: func(t *testing.T) bigquery.Client {
ctrl := gomock.NewController(t)
bqClient := mock_bigquery.NewMockClient(ctrl)

bqClient.EXPECT().GetTableMetadata(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(&bq.TableMetadata{}, nil).MinTimes(0)
return bqClient
},
expectCompletionItems: []CompletionItem{}, // empty
},
}

for n, tt := range tests {
t.Run(n, func(t *testing.T) {
bqClient := tt.bigqueryClientMockFunc(t)
logger := logrus.New()
logger.SetLevel(logrus.DebugLevel)

analyzer := file.NewAnalyzer(bqClient)
completor := New(logger, analyzer, bqClient)

files, path, position, err := helper.GetLspPosition(tt.files)
if err != nil {
t.Fatal(err)
}

parsedFile := analyzer.ParseFile(path, files[path])

got := completor.completeBuiltinFunction(context.Background(), parsedFile, position)
if diff := cmp.Diff(got, tt.expectCompletionItems); diff != "" {
t.Errorf("(-got, +want)\n%s", diff)
}
})
}
}
3 changes: 1 addition & 2 deletions langserver/internal/source/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ func (p *ParsedFile) FindTargetAnalyzeOutput(termOffset int) (*zetasql.AnalyzerO
}

func (p *ParsedFile) FindIncompleteColumnName(pos lsp.Position) string {
targetTerm := positionToByteOffset(p.Src, pos)
targetTerm = p.fixTermOffsetForNode(targetTerm)
targetTerm := p.TermOffset(pos)

for _, err := range p.Errors {
startOffset := positionToByteOffset(p.Src, err.Position)
Expand Down

0 comments on commit 5c5e320

Please sign in to comment.