Skip to content

Commit 8277773

Browse files
committed
Add types for database and catalog and use them
1 parent cea8c67 commit 8277773

File tree

3 files changed

+66
-37
lines changed

3 files changed

+66
-37
lines changed

packages/server/src/complete/CompletionItemUtils.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { CompletionItem, CompletionItemKind } from 'vscode-languageserver-types'
22
import { DbFunction } from '../database_libs/AbstractClient'
33

44
export const ICONS = {
5-
KEYWORD: CompletionItemKind.Text,
6-
COLUMN: CompletionItemKind.Interface,
7-
TABLE: CompletionItemKind.Field,
8-
FUNCTION: CompletionItemKind.Property,
9-
ALIAS: CompletionItemKind.Variable,
5+
KEYWORD: CompletionItemKind.Keyword,
6+
COLUMN: CompletionItemKind.Field,
7+
TABLE: CompletionItemKind.Constant,
8+
DATABASE: CompletionItemKind.Enum,
9+
CATALOG: CompletionItemKind.Folder,
10+
FUNCTION: CompletionItemKind.Event,
11+
ALIAS: CompletionItemKind.Constant,
1012
UTILITY: CompletionItemKind.Event,
1113
}
1214

packages/server/src/complete/Identifier.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import { CompletionItem, CompletionItemKind } from 'vscode-languageserver-types'
2-
3-
export const ICONS = {
4-
KEYWORD: CompletionItemKind.Text,
5-
COLUMN: CompletionItemKind.Interface,
6-
TABLE: CompletionItemKind.Field,
7-
FUNCTION: CompletionItemKind.Property,
8-
ALIAS: CompletionItemKind.Variable,
9-
UTILITY: CompletionItemKind.Event,
10-
}
2+
import { ICONS } from './CompletionItemUtils'
113

124
type OnClause = 'FROM' | 'ALTER TABLE' | 'OTHERS'
135
export class Identifier {
@@ -44,18 +36,37 @@ export class Identifier {
4436
toCompletionItem(): CompletionItem {
4537
const idx = this.lastToken.lastIndexOf('.')
4638
const label = this.identifier.substring(idx + 1)
47-
let kindName: string
48-
if (this.kind === ICONS.TABLE) {
39+
if (
40+
this.kind === ICONS.TABLE ||
41+
this.kind === ICONS.DATABASE ||
42+
this.kind === ICONS.CATALOG
43+
) {
4944
let tableName = label
5045
const i = tableName.lastIndexOf('.')
5146
if (i > 0) {
5247
tableName = label.substring(i + 1)
5348
}
54-
kindName = 'table'
55-
} else {
56-
kindName = 'column'
5749
}
5850

51+
const kindName = (() => {
52+
switch (this.kind) {
53+
case ICONS.TABLE:
54+
return 'table'
55+
case ICONS.DATABASE:
56+
return 'schema'
57+
case ICONS.CATALOG:
58+
return 'database'
59+
case ICONS.FUNCTION:
60+
return 'function'
61+
case ICONS.ALIAS:
62+
return 'table'
63+
case ICONS.COLUMN:
64+
return 'column'
65+
default:
66+
return 'column'
67+
}
68+
})()
69+
5970
const item: CompletionItem = {
6071
label: label,
6172
detail: `${kindName} ${this.detail}`,

packages/server/src/complete/candidates/createTableCandidates.ts

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,51 @@ export function createCatalogDatabaseAndTableCandidates(
3636
}
3737
const qualificationLevelNeeded = qualificationNeeded - qualificationLevel
3838
switch (qualificationLevelNeeded) {
39-
case 0:
40-
return [getFullyQualifiedTableName(table)]
41-
case 1:
42-
if (table.catalog && table.database) {
43-
return [table.catalog + '.' + table.database]
44-
}
45-
if (table.database) {
46-
return [table.database]
39+
case 0: {
40+
const tableIdentifier = new Identifier(
41+
lastToken,
42+
getFullyQualifiedTableName(table),
43+
'',
44+
ICONS.TABLE,
45+
onFromClause ? 'FROM' : 'OTHERS'
46+
)
47+
return [tableIdentifier]
48+
}
49+
case 1: {
50+
const qualifiedDatabaseName =
51+
table.catalog && table.database
52+
? table.catalog + '.' + table.database
53+
: table.database
54+
55+
if (qualifiedDatabaseName !== null) {
56+
const databaseIdentifier = new Identifier(
57+
lastToken,
58+
qualifiedDatabaseName,
59+
'',
60+
ICONS.DATABASE,
61+
onFromClause ? 'FROM' : 'OTHERS'
62+
)
63+
return [databaseIdentifier]
4764
}
4865
break
66+
}
4967
case 2:
5068
if (table.catalog) {
51-
return [table.catalog]
69+
const catalogIdentifier = new Identifier(
70+
lastToken,
71+
table.catalog,
72+
'',
73+
ICONS.CATALOG,
74+
onFromClause ? 'FROM' : 'OTHERS'
75+
)
76+
return [catalogIdentifier]
5277
}
5378
break
5479
}
5580
return []
5681
})
5782

5883
return qualifiedEntities
59-
.map((databaseEntity) => {
60-
return new Identifier(
61-
lastToken,
62-
databaseEntity,
63-
'',
64-
ICONS.TABLE,
65-
onFromClause ? 'FROM' : 'OTHERS'
66-
)
67-
})
6884
.filter((item) => item.matchesLastToken())
6985
.map((item) => item.toCompletionItem())
7086
}

0 commit comments

Comments
 (0)