@@ -17,14 +17,7 @@ import {
17
17
generateVariableSuggestion ,
18
18
generateWindowFunctionsSuggestion ,
19
19
} from './generateSuggestions' ;
20
- import type {
21
- AutocompleteConstant ,
22
- AutocompleteConstants ,
23
- AutocompleteConstantsInit ,
24
- CursorPosition ,
25
- FetchedColumn ,
26
- FetchedEntity ,
27
- } from './types' ;
20
+ import type { CursorPosition , FetchedColumn , FetchedEntity } from './types' ;
28
21
import { getEntitiesToFetchColumns } from './utils' ;
29
22
30
23
function getCursorPosition ( input : string , offset : number ) : CursorPosition {
@@ -61,33 +54,67 @@ function getRangeToInsertSuggestion(input: string, offset: number) {
61
54
} ;
62
55
}
63
56
64
- /**
65
- * YQLAutocomplete class provides autocompletion suggestions for YQL queries.
66
- * It uses a set of constants to generate suggestions for various YQL elements.
67
- * The class is abstract and needs to be extended to provide implementation for
68
- * fetching entities and columns.
69
- */
70
- export abstract class YQLAutocomplete {
71
- private readonly constants : AutocompleteConstants = { } ;
72
-
73
- async init ( constants : AutocompleteConstantsInit ) {
74
- const constantsArray = Object . keys ( constants ) as AutocompleteConstant [ ] ;
75
- const promises : ( string [ ] | Promise < string [ ] > ) [ ] = [ ] ;
76
- constantsArray . forEach ( ( c ) => {
77
- const value = constants [ c ] ;
78
- if ( typeof value === 'function' ) {
79
- promises . push ( value ( ) ) ;
80
- } else if ( value ) {
81
- promises . push ( value ) ;
82
- }
83
- } ) ;
84
- const result = await Promise . allSettled ( promises ) ;
85
- result . forEach ( ( item , index ) => {
86
- if ( item . status === 'fulfilled' ) {
87
- const constantName = constantsArray [ index ] ;
88
- this . constants [ constantName ] = item . value ;
89
- }
90
- } ) ;
57
+ export type YQLAutocompleteConfig = {
58
+ getQueryParser ?: YQLAutocomplete [ 'getQueryParser' ] ;
59
+ getUdfs ?: YQLAutocomplete [ 'getUdfs' ] ;
60
+ getSimpleTypes ?: YQLAutocomplete [ 'getSimpleTypes' ] ;
61
+ getPragmas ?: YQLAutocomplete [ 'getPragmas' ] ;
62
+ getWindowFunctions ?: YQLAutocomplete [ 'getWindowFunctions' ] ;
63
+ getTableFunctions ?: YQLAutocomplete [ 'getTableFunctions' ] ;
64
+ getAggregateFunctions ?: YQLAutocomplete [ 'getAggregateFunctions' ] ;
65
+ getSimpleFunctions ?: YQLAutocomplete [ 'getSimpleFunctions' ] ;
66
+ getEntitySettings ?: YQLAutocomplete [ 'getEntitySettings' ] ;
67
+ fetchEntities ?: YQLAutocomplete [ 'fetchEntities' ] ;
68
+ fetchEntityColumns ?: YQLAutocomplete [ 'fetchEntityColumns' ] ;
69
+ } ;
70
+
71
+ export class YQLAutocomplete {
72
+ constructor ( {
73
+ getQueryParser,
74
+ getUdfs,
75
+ getSimpleTypes,
76
+ getPragmas,
77
+ getWindowFunctions,
78
+ getTableFunctions,
79
+ getAggregateFunctions,
80
+ getSimpleFunctions,
81
+ getEntitySettings,
82
+ fetchEntities,
83
+ fetchEntityColumns,
84
+ } : YQLAutocompleteConfig ) {
85
+ if ( getQueryParser ) {
86
+ this . getQueryParser = getQueryParser ;
87
+ }
88
+ if ( getUdfs ) {
89
+ this . getUdfs = getUdfs ;
90
+ }
91
+ if ( getSimpleTypes ) {
92
+ this . getSimpleTypes = getSimpleTypes ;
93
+ }
94
+ if ( getPragmas ) {
95
+ this . getPragmas = getPragmas ;
96
+ }
97
+ if ( getWindowFunctions ) {
98
+ this . getWindowFunctions = getWindowFunctions ;
99
+ }
100
+ if ( getTableFunctions ) {
101
+ this . getTableFunctions = getTableFunctions ;
102
+ }
103
+ if ( getAggregateFunctions ) {
104
+ this . getAggregateFunctions = getAggregateFunctions ;
105
+ }
106
+ if ( getSimpleFunctions ) {
107
+ this . getSimpleFunctions = getSimpleFunctions ;
108
+ }
109
+ if ( getEntitySettings ) {
110
+ this . getEntitySettings = getEntitySettings ;
111
+ }
112
+ if ( fetchEntities ) {
113
+ this . fetchEntities = fetchEntities ;
114
+ }
115
+ if ( fetchEntityColumns ) {
116
+ this . fetchEntityColumns = fetchEntityColumns ;
117
+ }
91
118
}
92
119
93
120
async getSuggestions ( input : string , offset : number ) {
@@ -235,52 +262,75 @@ export abstract class YQLAutocomplete {
235
262
return parseQuery ;
236
263
}
237
264
238
- async getSimpleTypes ( _prefix ?: string ) {
239
- return this . constants . types ;
265
+ async getSimpleTypes ( _prefix ?: string ) : Promise < string [ ] > {
266
+ return [ ] ;
240
267
}
241
268
242
- async getUdfs ( _prefix ?: string ) {
243
- return this . constants . udfs ;
269
+ async getUdfs ( _prefix ?: string ) : Promise < string [ ] > {
270
+ return [ ] ;
244
271
}
245
272
246
- async getPragmas ( _prefix ?: string ) {
247
- return this . constants . pragmas ;
273
+ async getPragmas ( _prefix ?: string ) : Promise < string [ ] > {
274
+ return [ ] ;
248
275
}
249
- async getWindowFunctions ( _prefix ?: string ) {
250
- return this . constants . windowFunctions ;
276
+ async getWindowFunctions ( _prefix ?: string ) : Promise < string [ ] > {
277
+ return [ ] ;
251
278
}
252
279
253
- async getTableFunctions ( _prefix ?: string ) {
254
- return this . constants . tableFunctions ;
280
+ async getTableFunctions ( _prefix ?: string ) : Promise < string [ ] > {
281
+ return [ ] ;
255
282
}
256
- async getAggregateFunctions ( _prefix ?: string ) {
257
- return this . constants . aggregateFunctions ;
283
+ async getAggregateFunctions ( _prefix ?: string ) : Promise < string [ ] > {
284
+ return [ ] ;
258
285
}
259
286
260
- async getSimpleFunctions ( _prefix ?: string ) {
261
- return this . constants . simpleFunctions ;
287
+ async getSimpleFunctions ( _prefix ?: string ) : Promise < string [ ] > {
288
+ return [ ] ;
262
289
}
263
290
264
291
/**
265
292
* Fetches entity settings based on provided entity type.
266
293
* @param entityType
267
294
* @returns A promise that resolves to an array of entity settings.
268
295
*/
269
- abstract getEntitySettings ( entityType : YQLEntity ) : Promise < string [ ] > ;
296
+ async getEntitySettings ( _entityType : YQLEntity ) : Promise < string [ ] > {
297
+ return [ ] ;
298
+ }
270
299
/**
271
300
* Fetches entities based on the provided prefix and needed entity types.
272
301
* @param prefix - The prefix to filter entities.
273
302
* @param neededEntityTypes - An array of entity types to fetch.
274
303
* @returns A promise that resolves to an array of fetched entities.
275
304
*/
276
- abstract fetchEntities (
277
- prefix : string ,
278
- neededEntityTypes : YQLEntity [ ] ,
279
- ) : Promise < FetchedEntity [ ] > ;
305
+ async fetchEntities (
306
+ _prefix : string ,
307
+ _neededEntityTypes : YQLEntity [ ] ,
308
+ ) : Promise < FetchedEntity [ ] > {
309
+ return [ ] ;
310
+ }
280
311
/**
281
312
* Fetches columns for the specified entities.
282
313
* @param entityNames - An array of entity names to fetch columns for.
283
314
* @returns A promise that resolves to an array of fetched columns.
284
315
*/
285
- abstract fetchEntityColumns ( entityNames : string [ ] ) : Promise < FetchedColumn [ ] > ;
316
+ async fetchEntityColumns ( _entityNames : string [ ] ) : Promise < FetchedColumn [ ] > {
317
+ return [ ] ;
318
+ }
319
+ }
320
+
321
+ export function getSuggestionsGetter ( config : YQLAutocompleteConfig = { } ) {
322
+ const autocompleteInstance = new YQLAutocomplete ( config ) ;
323
+ return async (
324
+ model : Monaco . editor . ITextModel ,
325
+ monacoCursorPosition : Monaco . Position ,
326
+ _context : Monaco . languages . CompletionContext ,
327
+ _token : Monaco . CancellationToken ,
328
+ ) => {
329
+ const suggestions = await autocompleteInstance . getSuggestions (
330
+ model . getValue ( ) ,
331
+ model . getOffsetAt ( monacoCursorPosition ) ,
332
+ ) ;
333
+
334
+ return { suggestions} ;
335
+ } ;
286
336
}
0 commit comments