1
1
import type {
2
2
ColumnAliasSuggestion ,
3
3
KeywordSuggestion ,
4
+ VariableSuggestion ,
4
5
} from '@gravity-ui/websql-autocomplete/shared' ;
5
6
import type { YQLEntity , YqlAutocompleteResult } from '@gravity-ui/websql-autocomplete/yql' ;
6
7
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api' ;
@@ -107,6 +108,10 @@ function removeBackticks(value: string) {
107
108
return value . slice ( sliceStart , sliceEnd ) ;
108
109
}
109
110
111
+ function isVariable ( value : string ) {
112
+ return value . startsWith ( '$' ) ;
113
+ }
114
+
110
115
function removeStartSlash ( value : string ) {
111
116
if ( value . startsWith ( '/' ) ) {
112
117
return value . slice ( 1 ) ;
@@ -199,7 +204,8 @@ function getColumnDetails(col: AutocompleteColumn) {
199
204
200
205
export async function generateColumnsSuggestion (
201
206
rangeToInsertSuggestion : monaco . IRange ,
202
- suggestColumns : YqlAutocompleteResult [ 'suggestColumns' ] | undefined ,
207
+ suggestColumns : YqlAutocompleteResult [ 'suggestColumns' ] ,
208
+ suggestVariables : YqlAutocompleteResult [ 'suggestVariables' ] ,
203
209
database : string ,
204
210
) : Promise < monaco . languages . CompletionItem [ ] > {
205
211
if ( ! suggestColumns ?. tables ) {
@@ -212,7 +218,7 @@ export async function generateColumnsSuggestion(
212
218
const normalizedTableNames =
213
219
suggestColumns . tables ?. map ( ( entity ) => {
214
220
let normalizedEntityName = removeBackticks ( entity . name ) ;
215
- if ( ! normalizedEntityName . endsWith ( '/' ) ) {
221
+ if ( ! normalizedEntityName . endsWith ( '/' ) && ! isVariable ( normalizedEntityName ) ) {
216
222
normalizedEntityName = `${ normalizedEntityName } /` ;
217
223
}
218
224
return normalizeEntityPrefix ( normalizedEntityName , database ) ;
@@ -221,13 +227,31 @@ export async function generateColumnsSuggestion(
221
227
// remove duplicates if any
222
228
const filteredTableNames = Array . from ( new Set ( normalizedTableNames ) ) ;
223
229
224
- const autocompleteResponse = await window . api . viewer . autocomplete ( {
225
- database,
226
- table : filteredTableNames ,
227
- limit : 1000 ,
228
- } ) ;
229
- if ( ! autocompleteResponse . Success ) {
230
- return [ ] ;
230
+ const tableSources = filteredTableNames . filter ( ( name ) => ! isVariable ( name ) ) ;
231
+
232
+ let autocompleteEntities : TAutocompleteEntity [ ] = [ ] ;
233
+ if ( tableSources . length ) {
234
+ const autocompleteResponse = await window . api . viewer . autocomplete ( {
235
+ database,
236
+ table : tableSources ,
237
+ limit : 1000 ,
238
+ } ) ;
239
+ if ( autocompleteResponse . Success ) {
240
+ autocompleteEntities = autocompleteResponse . Result . Entities ?? [ ] ;
241
+ }
242
+ }
243
+
244
+ const variableSources = filteredTableNames . filter ( isVariable ) ;
245
+ let additionalColumns : TAutocompleteEntity [ ] = [ ] ;
246
+ if ( variableSources . length ) {
247
+ variableSources . forEach ( ( source ) => {
248
+ additionalColumns =
249
+ suggestVariables
250
+ // Variable name from suggestions doesn't include $ sign
251
+ ?. find ( ( variable ) => source . slice ( 1 ) === variable . name )
252
+ ?. value ?. columns ?. map ( ( col ) => ( { Name : col , Type : 'column' , Parent : source } ) ) ??
253
+ [ ] ;
254
+ } ) ;
231
255
}
232
256
233
257
const tableNameToAliasMap = suggestColumns . tables ?. reduce (
@@ -246,7 +270,7 @@ export async function generateColumnsSuggestion(
246
270
{ } as Record < string , string [ ] > ,
247
271
) ;
248
272
249
- autocompleteResponse . Result . Entities ? .forEach ( ( col ) => {
273
+ autocompleteEntities . concat ( additionalColumns ) . forEach ( ( col ) => {
250
274
if ( ! isAutocompleteColumn ( col ) ) {
251
275
return ;
252
276
}
@@ -293,7 +317,7 @@ export async function generateColumnsSuggestion(
293
317
normalizedColumns ?. push ( columnNameSuggestion ) ;
294
318
}
295
319
} ) ;
296
- if ( normalizedColumns && normalizedColumns . length > 0 ) {
320
+ if ( normalizedColumns && normalizedColumns . length > 1 ) {
297
321
const allColumns = normalizedColumns . join ( ', ' ) ;
298
322
suggestions . push ( {
299
323
label : allColumns ,
@@ -341,13 +365,13 @@ export function generateKeywordsSuggestion(
341
365
342
366
export function generateVariableSuggestion (
343
367
rangeToInsertSuggestion : monaco . IRange ,
344
- suggestVariables ?: string [ ] ,
368
+ suggestVariables ?: VariableSuggestion [ ] ,
345
369
) {
346
370
if ( ! suggestVariables ) {
347
371
return [ ] ;
348
372
}
349
- return suggestVariables . map ( ( rawVariable ) => {
350
- const variable = '$' + rawVariable ;
373
+ return suggestVariables . map ( ( { name } ) => {
374
+ const variable = '$' + name ;
351
375
return {
352
376
label : variable ,
353
377
insertText : variable ,
0 commit comments