1
1
import { Table , uniq } from "../../../../utils"
2
2
import * as monaco from "monaco-editor"
3
- import { InformationSchemaColumn } from "./types"
4
- import { editor , IRange } from "monaco-editor"
5
- import { languages } from "monaco-editor"
3
+ import { CompletionItemPriority , InformationSchemaColumn } from "./types"
4
+ import { editor } from "monaco-editor"
6
5
import IStandaloneCodeEditor = editor . IStandaloneCodeEditor
7
6
import { findMatches , getQueryFromCursor } from "../utils"
8
- import { operators } from "./operators"
9
- import { dataTypes , functions , keywords } from "@questdb/sql-grammar"
10
-
11
- const getLanguageCompletions = ( range : IRange ) => [
12
- ...functions . map ( ( qdbFunction ) => {
13
- return {
14
- label : qdbFunction ,
15
- kind : languages . CompletionItemKind . Function ,
16
- insertText : qdbFunction ,
17
- range,
18
- }
19
- } ) ,
20
- ...dataTypes . map ( ( item ) => {
21
- return {
22
- label : item ,
23
- kind : languages . CompletionItemKind . Keyword ,
24
- insertText : item ,
25
- range,
26
- }
27
- } ) ,
28
- ...keywords . map ( ( item ) => {
29
- const keyword = item . toUpperCase ( )
30
- return {
31
- label : keyword ,
32
- kind : languages . CompletionItemKind . Keyword ,
33
- insertText : keyword ,
34
- range,
35
- }
36
- } ) ,
37
- ...operators . map ( ( item ) => {
38
- const operator = item . toUpperCase ( )
39
- return {
40
- label : operator ,
41
- kind : languages . CompletionItemKind . Operator ,
42
- insertText : operator . toUpperCase ( ) ,
43
- range,
44
- }
45
- } ) ,
46
- ]
47
-
48
- export const getColumnCompletions = (
49
- columns : InformationSchemaColumn [ ] ,
50
- range : IRange ,
51
- withTableName ?: boolean ,
52
- ) => {
53
- // For JOIN ON ... completions, return `table.column` text
54
- if ( withTableName ) {
55
- return columns . map ( ( item ) => ( {
56
- label : {
57
- label : `${ item . table_name } .${ item . column_name } ` ,
58
- detail : "" ,
59
- description : item . data_type ,
60
- } ,
61
- kind : languages . CompletionItemKind . Enum ,
62
- insertText : `${ item . table_name } .${ item . column_name } ` ,
63
- sortText : "1" ,
64
- range,
65
- } ) )
66
- // For everything else, return a list of unique column names.
67
- } else {
68
- return uniq ( columns . map ( ( item ) => item . column_name ) ) . map ( ( columnName ) => {
69
- const tableNames = columns
70
- . filter ( ( item ) => item . column_name === columnName )
71
- . map ( ( item ) => item . table_name )
72
- return {
73
- label : {
74
- label : columnName ,
75
- detail : ` (${ tableNames . sort ( ) . join ( ", " ) } )` ,
76
- // If the column is present in multiple tables, show their list here, otherwise return the column type.
77
- description :
78
- tableNames . length > 1
79
- ? ""
80
- : columns . find ( ( item ) => item . column_name === columnName )
81
- ?. data_type ,
82
- } ,
83
- kind : languages . CompletionItemKind . Enum ,
84
- insertText : columnName ,
85
- sortText : "1" ,
86
- range,
87
- }
88
- } )
89
- }
90
- }
7
+ import { getTableCompletions } from "./getTableCompletions"
8
+ import { getColumnCompletions } from "./getColumnCompletions"
9
+ import { getLanguageCompletions } from "./getLanguageCompletions"
91
10
92
11
export const createSchemaCompletionProvider = (
93
12
editor : IStandaloneCodeEditor ,
@@ -149,20 +68,6 @@ export const createSchemaCompletionProvider = (
149
68
const openQuote = textUntilPosition . substr ( - 1 ) === '"'
150
69
const nextCharQuote = nextChar == '"'
151
70
152
- const tableSuggestions = tables . map ( ( item ) => {
153
- return {
154
- label : item . table_name ,
155
- kind : languages . CompletionItemKind . Class ,
156
- insertText : openQuote
157
- ? item . table_name + ( nextCharQuote ? "" : '"' )
158
- : / ^ [ a - z 0 - 9 _ ] + $ / i. test ( item . table_name )
159
- ? item . table_name
160
- : `"${ item . table_name } "` ,
161
- sortText : "1" ,
162
- range,
163
- }
164
- } )
165
-
166
71
if (
167
72
/ ( F R O M | I N T O | ( A L T E R | B A C K U P | D R O P | R E I N D E X | R E N A M E | T R U N C A T E | V A C U U M ) T A B L E | J O I N | U P D A T E ) \s $ / gim. test (
168
73
textUntilPosition ,
@@ -171,7 +76,13 @@ export const createSchemaCompletionProvider = (
171
76
! textUntilPosition . endsWith ( "= '" ) )
172
77
) {
173
78
return {
174
- suggestions : tableSuggestions ,
79
+ suggestions : getTableCompletions ( {
80
+ tables,
81
+ range,
82
+ priority : CompletionItemPriority . High ,
83
+ openQuote,
84
+ nextCharQuote,
85
+ } ) ,
175
86
}
176
87
}
177
88
@@ -186,21 +97,33 @@ export const createSchemaCompletionProvider = (
186
97
textUntilPosition . match ( / \s O N \s / gim) !== null
187
98
return {
188
99
suggestions : [
189
- ...getColumnCompletions (
190
- informationSchemaColumns . filter ( ( item ) =>
100
+ ...getColumnCompletions ( {
101
+ columns : informationSchemaColumns . filter ( ( item ) =>
191
102
tableContext . includes ( item . table_name ) ,
192
103
) ,
193
104
range,
194
105
withTableName,
195
- ) ,
106
+ priority : CompletionItemPriority . High ,
107
+ } ) ,
196
108
...getLanguageCompletions ( range ) ,
197
109
] ,
198
110
}
199
111
} else {
200
112
return {
201
113
suggestions : [
202
- ...getColumnCompletions ( informationSchemaColumns , range ) ,
203
- ...tableSuggestions ,
114
+ ...getColumnCompletions ( {
115
+ columns : informationSchemaColumns ,
116
+ range,
117
+ withTableName : false ,
118
+ priority : CompletionItemPriority . High ,
119
+ } ) ,
120
+ ...getTableCompletions ( {
121
+ tables,
122
+ range,
123
+ priority : CompletionItemPriority . MediumHigh ,
124
+ openQuote,
125
+ nextCharQuote,
126
+ } ) ,
204
127
...getLanguageCompletions ( range ) ,
205
128
] ,
206
129
}
@@ -210,7 +133,13 @@ export const createSchemaCompletionProvider = (
210
133
if ( word . word ) {
211
134
return {
212
135
suggestions : [
213
- ...tableSuggestions ,
136
+ ...getTableCompletions ( {
137
+ tables,
138
+ range,
139
+ priority : CompletionItemPriority . High ,
140
+ openQuote,
141
+ nextCharQuote,
142
+ } ) ,
214
143
...getLanguageCompletions ( range ) ,
215
144
] ,
216
145
}
0 commit comments