diff --git a/langserver/internal/lsp/service.go b/langserver/internal/lsp/service.go index 593228f..2d3bb1d 100644 --- a/langserver/internal/lsp/service.go +++ b/langserver/internal/lsp/service.go @@ -435,11 +435,23 @@ var completionItemKindName = map[CompletionItemKind]string{ CIKTypeParameter: "typeParameter", } +type MarkupKind string + +const ( + MKPlainText MarkupKind = "plaintext" + MKMarkdown MarkupKind = "markdown" +) + +type MarkupContent struct { + Kind MarkupKind `json:"kind"` + Value string `json:"value"` +} + type CompletionItem struct { Label string `json:"label"` Kind CompletionItemKind `json:"kind,omitempty"` Detail string `json:"detail,omitempty"` - Documentation string `json:"documentation,omitempty"` + Documentation MarkupContent `json:"documentation,omitempty"` SortText string `json:"sortText,omitempty"` FilterText string `json:"filterText,omitempty"` InsertText string `json:"insertText,omitempty"` diff --git a/langserver/internal/source/completion/builtin_function.go b/langserver/internal/source/completion/builtin_function.go index 03321bb..4a79f6c 100644 --- a/langserver/internal/source/completion/builtin_function.go +++ b/langserver/internal/source/completion/builtin_function.go @@ -25,10 +25,13 @@ func (c *completor) completeBuiltinFunction(ctx context.Context, parsedFile file for _, f := range function.BuiltInFunctions { if strings.HasPrefix(f.Name, incompleteColumnName) { result = append(result, CompletionItem{ - Kind: lsp.CIKFunction, - NewText: f.Name, - TypedPrefix: incompleteColumnName, - Documentation: f.Description, + Kind: lsp.CIKFunction, + NewText: f.Name, + TypedPrefix: incompleteColumnName, + Documentation: lsp.MarkupContent{ + Kind: lsp.MKMarkdown, + Value: f.Description, + }, }) } } diff --git a/langserver/internal/source/completion/completion.go b/langserver/internal/source/completion/completion.go index 3fa4b69..085b393 100644 --- a/langserver/internal/source/completion/completion.go +++ b/langserver/internal/source/completion/completion.go @@ -173,10 +173,13 @@ func (c *completor) completeTableScanField(ctx context.Context, tableScanNode *r if strings.HasPrefix(tableScanNode.Alias(), incompleteColumnName) { return []CompletionItem{ { - Kind: lsp.CIKField, - NewText: tableScanNode.Alias(), - Documentation: tableScanNode.Table().FullName(), - TypedPrefix: incompleteColumnName, + Kind: lsp.CIKField, + NewText: tableScanNode.Alias(), + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: tableScanNode.Table().FullName(), + }, + TypedPrefix: incompleteColumnName, }, } } @@ -277,10 +280,13 @@ func (c *completor) completeProjectForTablePath(ctx context.Context, param table } result = append(result, CompletionItem{ - Kind: lsp.CIKModule, - NewText: p.ProjectId, - Documentation: p.Name, - TypedPrefix: param.ProjectID, + Kind: lsp.CIKModule, + NewText: p.ProjectId, + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: p.Name, + }, + TypedPrefix: param.ProjectID, }) } @@ -300,10 +306,13 @@ func (c *completor) completeDatasetForTablePath(ctx context.Context, param table } result = append(result, CompletionItem{ - Kind: lsp.CIKModule, - NewText: d.DatasetID, - Documentation: fmt.Sprintf("%s.%s", d.ProjectID, d.DatasetID), - TypedPrefix: param.DatasetID, + Kind: lsp.CIKModule, + NewText: d.DatasetID, + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: fmt.Sprintf("%s.%s", d.ProjectID, d.DatasetID), + }, + TypedPrefix: param.DatasetID, }) } @@ -323,10 +332,13 @@ func (c *completor) completeTableForTablePath(ctx context.Context, param tablePa } result = append(result, CompletionItem{ - Kind: lsp.CIKModule, - NewText: t.TableID, - Documentation: fmt.Sprintf("%s.%s.%s", t.ProjectID, t.DatasetID, t.TableID), - TypedPrefix: param.TableID, + Kind: lsp.CIKModule, + NewText: t.TableID, + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: fmt.Sprintf("%s.%s.%s", t.ProjectID, t.DatasetID, t.TableID), + }, + TypedPrefix: param.TableID, }) } @@ -426,10 +438,13 @@ type columnInterface interface { func createCompletionItemFromColumn(column columnInterface, incompleteColumnName string) CompletionItem { return CompletionItem{ - Kind: lsp.CIKField, - NewText: column.Name(), - Documentation: column.Type().TypeName(types.ProductExternal), - TypedPrefix: incompleteColumnName, + Kind: lsp.CIKField, + NewText: column.Name(), + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: column.Type().TypeName(types.ProductExternal), + }, + TypedPrefix: incompleteColumnName, } } @@ -439,9 +454,12 @@ func createCompletionItemFromSchema(schema *bq.FieldSchema, incompleteColumnName detail += "\n" + schema.Description } return CompletionItem{ - Kind: lsp.CIKField, - NewText: schema.Name, - Documentation: detail, - TypedPrefix: incompleteColumnName, + Kind: lsp.CIKField, + NewText: schema.Name, + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: detail, + }, + TypedPrefix: incompleteColumnName, } } diff --git a/langserver/internal/source/completion/completion_test.go b/langserver/internal/source/completion/completion_test.go index b3b976a..1dd7da3 100644 --- a/langserver/internal/source/completion/completion_test.go +++ b/langserver/internal/source/completion/completion_test.go @@ -48,14 +48,20 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, }, { - Kind: lsp.CIKField, - NewText: "name", - Documentation: "STRING", + Kind: lsp.CIKField, + NewText: "name", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "STRING", + }, }, }, }, @@ -76,9 +82,12 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, }, }, }, @@ -108,9 +117,12 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "name", - Documentation: "STRING", + Kind: lsp.CIKField, + NewText: "name", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "STRING", + }, }, }, }, @@ -132,9 +144,12 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INT64", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INT64", + }, }, { // TODO: Refactoring test Kind: lsp.CIKField, @@ -159,10 +174,13 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", - TypedPrefix: "i", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, + TypedPrefix: "i", }, }, }, @@ -183,10 +201,13 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", - TypedPrefix: "i", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, + TypedPrefix: "i", }, }, }, @@ -213,9 +234,12 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, }, }, }, @@ -248,9 +272,12 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, }, }, }, @@ -277,10 +304,13 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", - TypedPrefix: "i", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, + TypedPrefix: "i", }, }, }, @@ -307,9 +337,12 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, }, }, }, @@ -336,9 +369,12 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INT64", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INT64", + }, }, }, }, @@ -359,9 +395,12 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, }, }, }, @@ -382,9 +421,12 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, }, }, }, @@ -405,10 +447,13 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", - TypedPrefix: "i", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, + TypedPrefix: "i", }, }, }, @@ -429,9 +474,12 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INT64", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INT64", + }, }, }, }, @@ -451,10 +499,13 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "table", - Documentation: "project.dataset.table", - TypedPrefix: "t", + Kind: lsp.CIKField, + NewText: "table", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "project.dataset.table", + }, + TypedPrefix: "t", }, }, }, @@ -497,9 +548,12 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, }, }, }, @@ -520,9 +574,12 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, }, }, }, @@ -543,9 +600,12 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, }, }, }, @@ -566,9 +626,12 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, }, }, }, @@ -589,10 +652,13 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", - TypedPrefix: "i", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, + TypedPrefix: "i", }, }, }, @@ -613,10 +679,13 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", - TypedPrefix: "i", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, + TypedPrefix: "i", }, }, }, @@ -646,28 +715,40 @@ func TestProject_CompleteFromSQLContext(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description", - TypedPrefix: "", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description", + }, + TypedPrefix: "", }, { - Kind: lsp.CIKField, - NewText: "id", - Documentation: "INTEGER\nid description2", - TypedPrefix: "", + Kind: lsp.CIKField, + NewText: "id", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "INTEGER\nid description2", + }, + TypedPrefix: "", }, { - Kind: lsp.CIKField, - NewText: "t1", - Documentation: "project.dataset.table", - TypedPrefix: "", + Kind: lsp.CIKField, + NewText: "t1", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "project.dataset.table", + }, + TypedPrefix: "", }, { - Kind: lsp.CIKField, - NewText: "t2", - Documentation: "project.dataset.table2", - TypedPrefix: "", + Kind: lsp.CIKField, + NewText: "t2", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "project.dataset.table2", + }, + TypedPrefix: "", }, }, }, @@ -743,14 +824,20 @@ func TestProject_CompleteFromSQLContext_FromClause(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKModule, - NewText: "1table", - Documentation: "project.dataset.1table", + Kind: lsp.CIKModule, + NewText: "1table", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "project.dataset.1table", + }, }, { - Kind: lsp.CIKModule, - NewText: "2table", - Documentation: "project.dataset.2table", + Kind: lsp.CIKModule, + NewText: "2table", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "project.dataset.2table", + }, }, }, }, @@ -775,9 +862,12 @@ func TestProject_CompleteFromSQLContext_FromClause(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKModule, - NewText: "table20230622", - Documentation: "project.dataset.table20230622", + Kind: lsp.CIKModule, + NewText: "table20230622", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "project.dataset.table20230622", + }, }, }, }, @@ -805,14 +895,20 @@ func TestProject_CompleteFromSQLContext_FromClause(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKModule, - NewText: "dataset1", - Documentation: "project.dataset1", + Kind: lsp.CIKModule, + NewText: "dataset1", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "project.dataset1", + }, }, { - Kind: lsp.CIKModule, - NewText: "dataset2", - Documentation: "project.dataset2", + Kind: lsp.CIKModule, + NewText: "dataset2", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "project.dataset2", + }, }, }, }, @@ -839,16 +935,22 @@ func TestProject_CompleteFromSQLContext_FromClause(t *testing.T) { }, expectCompletionItems: []CompletionItem{ { - Kind: lsp.CIKModule, - NewText: "project1", - Documentation: "project name", - TypedPrefix: "p", + Kind: lsp.CIKModule, + NewText: "project1", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "project name", + }, + TypedPrefix: "p", }, { - Kind: lsp.CIKModule, - NewText: "project2", - Documentation: "project name", - TypedPrefix: "p", + Kind: lsp.CIKModule, + NewText: "project2", + Documentation: lsp.MarkupContent{ + Kind: lsp.MKPlainText, + Value: "project name", + }, + TypedPrefix: "p", }, }, }, diff --git a/langserver/internal/source/completion/item.go b/langserver/internal/source/completion/item.go index 5177f1e..610b185 100644 --- a/langserver/internal/source/completion/item.go +++ b/langserver/internal/source/completion/item.go @@ -5,7 +5,7 @@ import "github.com/kitagry/bqls/langserver/internal/lsp" type CompletionItem struct { Kind lsp.CompletionItemKind NewText string - Documentation string + Documentation lsp.MarkupContent TypedPrefix string }