Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(LSP): display column definition and comments (#9690)

rebelice0f24b14d1ac3
diff --git a/backend/api/lsp/completion.go b/backend/api/lsp/completion.go
index ea58fd70d5..bdfb390abf 100644
--- a/backend/api/lsp/completion.go
+++ b/backend/api/lsp/completion.go
@@ -55,10 +55,11 @@ func (h *Handler) handleTextDocumentCompletion(ctx context.Context, _ *jsonrpc2.
var items []lsp.CompletionItem
for _, candidate := range candidates {
items = append(items, lsp.CompletionItem{
- Label: candidate.Text,
- Detail: fmt.Sprintf("<%s>", string(candidate.Type)),
- Kind: convertLSPCompletionItemKind(candidate.Type),
- SortText: generateSortText(params, candidate),
+ Label: candidate.Text,
+ Detail: fmt.Sprintf("<%s> %s", string(candidate.Type), candidate.Definition),
+ Kind: convertLSPCompletionItemKind(candidate.Type),
+ Documentation: candidate.Comment,
+ SortText: generateSortText(params, candidate),
})
}
diff --git a/backend/plugin/parser/base/complete.go b/backend/plugin/parser/base/complete.go
index 1d6cdfc4ec..06f5858632 100644
--- a/backend/plugin/parser/base/complete.go
+++ b/backend/plugin/parser/base/complete.go
@@ -31,8 +31,10 @@ const (
// Candidate is the candidate for auto-completion.
type Candidate struct {
- Text string
- Type CandidateType
+ Text string
+ Type CandidateType
+ Definition string
+ Comment string
}
func (c Candidate) String() string {
diff --git a/backend/plugin/parser/mysql/completion.go b/backend/plugin/parser/mysql/completion.go
index 310e6cf2cb..74d9946468 100644
--- a/backend/plugin/parser/mysql/completion.go
+++ b/backend/plugin/parser/mysql/completion.go
@@ -1091,12 +1091,38 @@ func (m CompletionMap) insertViews(c *Completer, schemas map[string]bool) {
}
}
-func (m CompletionMap) insertColumns(c *Completer, schemas, tables map[string]bool) {
- for _, columnName := range c.listColumns(schemas, tables) {
- m.Insert(base.Candidate{
- Type: base.CandidateTypeColumn,
- Text: columnName,
- })
+func (m CompletionMap) insertColumns(c *Completer, databases, tables map[string]bool) {
+ for database := range databases {
+ if _, exists := c.metadataCache[database]; !exists {
+ metadata, err := c.getMetadata(c.ctx, database)
+ if err != nil || metadata == nil {
+ continue
+ }
+ c.metadataCache[database] = metadata
+ }
+
+ for table := range tables {
+ tableMeta := c.metadataCache[database].GetSchema("").GetTable(table)
+ if tableMeta == nil {
+ continue
+ }
+ for _, column := range tableMeta.GetColumns() {
+ definition := fmt.Sprintf("%s | %s", table, column.Type)
+ if !column.Nullable {
+ definition += ", NOT NULL"
+ }
+ comment := column.UserComment
+ if len(column.Classification) != 0 {
+ comment = column.Classification + "\n" + column.UserComment
+ }
+ m.Insert(base.Candidate{
+ Type: base.CandidateTypeColumn,
+ Text: column.Name,
+ Definition: definition,
+ Comment: comment,
+ })
+ }
+ }
}
}
@@ -1138,28 +1164,3 @@ func (c *Completer) listViews(database string) []string {
return c.metadataCache[database].GetSchema("").ListViewNames()
}
-
-func (c *Completer) listColumns(databases, tables map[string]bool) []string {
- var result []string
- for database := range databases {
- if _, exists := c.metadataCache[database]; !exists {
- metadata, err := c.getMetadata(c.ctx, database)
- if err != nil || metadata == nil {
- continue
- }
- c.metadataCache[database] = metadata
- }
-
- for table := range tables {
- tableMeta := c.metadataCache[database].GetSchema("").GetTable(table)
- if tableMeta == nil {
- continue
- }
- for _, column := range tableMeta.GetColumns() {
- result = append(result, column.Name)
- }
- }
- }
-
- return result
-}
diff --git a/backend/plugin/parser/mysql/test-data/test_completion.yaml b/backend/plugin/parser/mysql/test-data/test_completion.yaml
index dbced1ae1d..90bb700887 100644
--- a/backend/plugin/parser/mysql/test-data/test_completion.yaml
+++ b/backend/plugin/parser/mysql/test-data/test_completion.yaml
@@ -2,117 +2,215 @@
want:
- text: db
type: DATABASE
+ definition: ""
+ comment: ""
- text: t1
type: TABLE
+ definition: ""
+ comment: ""
- text: t2
type: TABLE
+ definition: ""
+ comment: ""
- text: c1
type: COLUMN
+ definition: t2 | , NOT NULL
+ comment: ""
- text: c2
type: COLUMN
+ definition: t2 | , NOT NULL
+ comment: ""
- text: eid
type: COLUMN
+ definition: ""
+ comment: ""
- text: xid
type: COLUMN
+ definition: ""
+ comment: ""
- text: v1
type: VIEW
+ definition: ""
+ comment: ""
- input: SELECT c1 as eid FROM t1 ORDER BY |
want:
- text: db
type: DATABASE
+ definition: ""
+ comment: ""
- text: t1
type: TABLE
+ definition: ""
+ comment: ""
- text: t2
type: TABLE
+ definition: ""
+ comment: ""
- text: c1
type: COLUMN
+ definition: t1 | , NOT NULL
+ comment: ""
- text: eid
type: COLUMN
+ definition: ""
+ comment: ""
- text: v1
type: VIEW
+ definition: ""
+ comment: ""
- input: SELECT FROM basdkfjasldf;SELECT | FROM t1
want:
- text: db
type: DATABASE
+ definition: ""
+ comment: ""
- text: t1
type: TABLE
+ definition: ""
+ comment: ""
- text: t2
type: TABLE
+ definition: ""
+ comment: ""
- text: c1
type: COLUMN
+ definition: t1 | , NOT NULL
+ comment: ""
- text: v1
type: VIEW
+ definition: ""
+ comment: ""
- input: SELECT | FROM (SELECT c1 FROM t1) cc1(cc1c1)
want:
- text: db
type: DATABASE
+ definition: ""
+ comment: ""
- text: cc1
type: TABLE
+ definition: ""
+ comment: ""
- text: t1
type: TABLE
+ definition: ""
+ comment: ""
- text: t2
type: TABLE
+ definition: ""
+ comment: ""
- text: cc1c1
type: COLUMN
+ definition: ""
+ comment: ""
- text: v1
type: VIEW
+ definition: ""
+ comment: ""
- input: SELECT | FROM (SELECT c1 FROM t1) cc1
want:
- text: db
type: DATABASE
+ definition: ""
+ comment: ""
- text: cc1
type: TABLE
+ definition: ""
+ comment: ""
- text: t1
type: TABLE
+ definition: ""
+ comment: ""
- text: t2
type: TABLE
+ definition: ""
+ comment: ""
- text: v1
type: VIEW
+ definition: ""
+ comment: ""
- input: SELECT * from t1 cc1 JOIN t2 on cc1.|
want:
- text: c1
type: COLUMN
+ definition: t1 | , NOT NULL
+ comment: ""
- input: SELECT MAX(cc1.|) FROM t2 cc1
want:
- text: c1
type: COLUMN
+ definition: t2 | , NOT NULL
+ comment: ""
- text: c2
type: COLUMN
+ definition: t2 | , NOT NULL
+ comment: ""
- input: SELECT * FROM |
want:
- text: db
type: DATABASE
+ definition: ""
+ comment: ""
- text: t1
type: TABLE
+ definition: ""
+ comment: ""
- text: t2
type: TABLE
+ definition: ""
+ comment: ""
- text: v1
type: VIEW
+ definition: ""
+ comment: ""
- input: SELECT | FROM t1
want:
- text: db
type: DATABASE
+ definition: ""
+ comment: ""
- text: t1
type: TABLE
+ definition: ""
+ comment: ""
- text: t2
type: TABLE
+ definition: ""
+ comment: ""
- text: c1
type: COLUMN
+ definition: t1 | , NOT NULL
+ comment: ""
- text: v1
type: VIEW
+ definition: ""
+ comment: ""
- input: SELECT | FROM t2 x
want:
- text: db
type: DATABASE
+ definition: ""
+ comment: ""
- text: t1
type: TABLE
+ definition: ""
+ comment: ""
- text: t2
type: TABLE
+ definition: ""
+ comment: ""
- text: x
type: TABLE
+ definition: ""
+ comment: ""
- text: c1
type: COLUMN
+ definition: t2 | , NOT NULL
+ comment: ""
- text: c2
type: COLUMN
+ definition: t2 | , NOT NULL
+ comment: ""
- text: v1
type: VIEW
+ definition: ""
+ comment: ""