monaco-yql-languages 1.3.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,8 +2,15 @@ import type { CancellationToken, Position, editor, languages } from './fillers/m
2
2
  export type CompletionListProvider = {
3
3
  (content: string, offset: number, signal?: AbortSignal | null): Promise<string[]>;
4
4
  };
5
- export declare function provideCompletionItems(getCompletionList: CompletionListProvider): (model: editor.ITextModel, position: Position, context: languages.CompletionContext, token: CancellationToken) => languages.ProviderResult<languages.CompletionList>;
5
+ type YqlCompletionType = 'Keyword' | 'PragmaName' | 'TypeName' | 'FunctionName' | 'HintName' | 'FolderName' | 'TableName' | 'ClusterName' | 'UnknownName';
6
+ export type CompletionData = string | {
7
+ type: YqlCompletionType;
8
+ text: string;
9
+ shift?: number;
10
+ };
11
+ export declare function provideCompletionItems(getCompletionList: CompletionListProvider, preserveOrder?: boolean): (model: editor.ITextModel, position: Position, context: languages.CompletionContext, token: CancellationToken) => languages.ProviderResult<languages.CompletionList>;
6
12
  export type CompletionLists = Partial<Record<`${Uncapitalize<keyof typeof languages.CompletionItemKind>}List`, Readonly<string[]>>>;
7
13
  export declare function getCompletionItemsProvider(completions: CompletionLists, completionItemKind: typeof languages.CompletionItemKind): {
8
14
  provideCompletionItems(model: editor.ITextModel, position: Position, _context: languages.CompletionContext, _token: CancellationToken): languages.CompletionList | undefined;
9
15
  };
16
+ export {};
@@ -1,5 +1,17 @@
1
+ import { suggestionIndexToWeight } from './utils';
1
2
  const allUpperCaseRe = new RegExp('^[$A-Z_\\s]+$');
2
- export function provideCompletionItems(getCompletionList) {
3
+ const YqlCompletionTypeToMonacoKind = {
4
+ Keyword: 17,
5
+ PragmaName: 8,
6
+ TypeName: 24,
7
+ FunctionName: 1,
8
+ HintName: 18,
9
+ FolderName: 23,
10
+ TableName: 18,
11
+ ClusterName: 8,
12
+ UnknownName: 18,
13
+ };
14
+ export function provideCompletionItems(getCompletionList, preserveOrder) {
3
15
  return (model, position, context, token) => {
4
16
  const controller = new AbortController();
5
17
  const signal = controller.signal;
@@ -18,7 +30,23 @@ export function provideCompletionItems(getCompletionList) {
18
30
  }
19
31
  return getCompletionList(model.getValue(), model.getOffsetAt(position), signal).then(async (data) => {
20
32
  const suggestions = [];
21
- for (const label of data) {
33
+ for (const item of data) {
34
+ let labelAsSnippet = '';
35
+ //languages.CompletionItemKind.Text
36
+ let kind = 18;
37
+ let label;
38
+ if (typeof item === 'object') {
39
+ const { text, type, shift } = item;
40
+ label = text;
41
+ kind = YqlCompletionTypeToMonacoKind[type];
42
+ if (shift) {
43
+ const pos = text.length - shift;
44
+ labelAsSnippet = text.slice(0, pos) + '$0' + text.slice(pos);
45
+ }
46
+ }
47
+ else {
48
+ label = item;
49
+ }
22
50
  const suggest = label.length > 1
23
51
  ? label.slice(0, -1).replace(/[/[]/g, '\\\\$&') + label.slice(-1)
24
52
  : label;
@@ -40,8 +68,11 @@ export function provideCompletionItems(getCompletionList) {
40
68
  suggestions.push({
41
69
  label,
42
70
  filterText: allUpperCaseRe.test(label) ? label.toLowerCase() : label,
43
- kind: 18, // languages.CompletionItemKind.Text,
44
- insertText: suggest,
71
+ kind,
72
+ insertText: labelAsSnippet || suggest,
73
+ //4 - languages.CompletionItemInsertTextRule.InsertAsSnippet
74
+ //0 - languages.CompletionItemInsertTextRule.None
75
+ insertTextRules: labelAsSnippet ? 4 : 0,
45
76
  command: label.endsWith('/') || label.endsWith('.`') || label.endsWith('::')
46
77
  ? { id: 'editor.action.triggerSuggest', title: '' }
47
78
  : undefined,
@@ -49,7 +80,12 @@ export function provideCompletionItems(getCompletionList) {
49
80
  range,
50
81
  });
51
82
  }
52
- return { suggestions, incomplete: suggestions.length > 0 };
83
+ return {
84
+ suggestions: preserveOrder
85
+ ? suggestions.map((item, index) => (Object.assign(Object.assign({}, item), { sortText: suggestionIndexToWeight(index) })))
86
+ : suggestions,
87
+ incomplete: suggestions.length > 0,
88
+ };
53
89
  });
54
90
  };
55
91
  }
@@ -0,0 +1 @@
1
+ export declare function suggestionIndexToWeight(index: number): string;
package/build/utils.js ADDED
@@ -0,0 +1,11 @@
1
+ const alphabet = 'abcdefghijklmnopqrstuvwxyz';
2
+ export function suggestionIndexToWeight(index) {
3
+ const characterInsideAlphabet = alphabet[index];
4
+ if (characterInsideAlphabet) {
5
+ return characterInsideAlphabet;
6
+ }
7
+ const duplicateTimes = Math.floor(index / alphabet.length);
8
+ const remains = index % alphabet.length;
9
+ const lastCharacter = alphabet.slice(-1);
10
+ return lastCharacter.repeat(duplicateTimes) + alphabet[remains];
11
+ }
@@ -46,7 +46,7 @@ describe('generateColumnsSuggestion', () => {
46
46
  kind: 1,
47
47
  detail: 'Column',
48
48
  range: mockRange,
49
- sortText: '00',
49
+ sortText: 'aa',
50
50
  },
51
51
  {
52
52
  label: { label: 'col2', description: undefined },
@@ -54,7 +54,7 @@ describe('generateColumnsSuggestion', () => {
54
54
  kind: 1,
55
55
  detail: 'Column',
56
56
  range: mockRange,
57
- sortText: '00',
57
+ sortText: 'ab',
58
58
  },
59
59
  ]);
60
60
  });
@@ -72,7 +72,7 @@ describe('generateColumnsSuggestion', () => {
72
72
  kind: 1,
73
73
  detail: 'Column',
74
74
  range: mockRange,
75
- sortText: '00',
75
+ sortText: 'aa',
76
76
  },
77
77
  {
78
78
  label: { label: 'table2.col2', description: undefined },
@@ -80,7 +80,7 @@ describe('generateColumnsSuggestion', () => {
80
80
  kind: 1,
81
81
  detail: 'Column',
82
82
  range: mockRange,
83
- sortText: '00',
83
+ sortText: 'ab',
84
84
  },
85
85
  ]);
86
86
  });
@@ -95,7 +95,7 @@ describe('generateColumnsSuggestion', () => {
95
95
  kind: 1,
96
96
  detail: 'Column',
97
97
  range: mockRange,
98
- sortText: '00',
98
+ sortText: 'aa',
99
99
  },
100
100
  ]);
101
101
  });
@@ -111,7 +111,7 @@ describe('generateColumnsSuggestion', () => {
111
111
  kind: 1,
112
112
  detail: 'Column',
113
113
  range: mockRange,
114
- sortText: '00',
114
+ sortText: 'aa',
115
115
  },
116
116
  ]);
117
117
  });
@@ -130,7 +130,7 @@ describe('generateColumnsSuggestion', () => {
130
130
  kind: 1,
131
131
  detail: 'Column',
132
132
  range: mockRange,
133
- sortText: '00',
133
+ sortText: 'aa',
134
134
  },
135
135
  {
136
136
  label: { label: 'table2.col2', description: undefined },
@@ -138,14 +138,14 @@ describe('generateColumnsSuggestion', () => {
138
138
  kind: 1,
139
139
  detail: 'Column',
140
140
  range: mockRange,
141
- sortText: '00',
141
+ sortText: 'ab',
142
142
  },
143
143
  {
144
144
  label: 'table1.col1, table2.col2',
145
145
  insertText: 'table1.col1, table2.col2',
146
146
  kind: 1,
147
147
  range: mockRange,
148
- sortText: '0',
148
+ sortText: 'a',
149
149
  },
150
150
  ]);
151
151
  });
@@ -163,7 +163,7 @@ describe('generateColumnsSuggestion', () => {
163
163
  kind: 1,
164
164
  detail: 'Column',
165
165
  range: mockRange,
166
- sortText: '00',
166
+ sortText: 'aa',
167
167
  },
168
168
  {
169
169
  label: { label: 'col1', description: undefined },
@@ -171,7 +171,7 @@ describe('generateColumnsSuggestion', () => {
171
171
  kind: 1,
172
172
  detail: 'Column',
173
173
  range: mockRange,
174
- sortText: '00',
174
+ sortText: 'ab',
175
175
  },
176
176
  ]);
177
177
  });
@@ -207,7 +207,7 @@ describe('generateEntitiesSuggestion', () => {
207
207
  endColumn: 1,
208
208
  },
209
209
  command: undefined,
210
- sortText: '00',
210
+ sortText: 'aa',
211
211
  },
212
212
  {
213
213
  label: 'entity2/',
@@ -222,7 +222,7 @@ describe('generateEntitiesSuggestion', () => {
222
222
  endColumn: 1,
223
223
  },
224
224
  command: { id: 'editor.action.triggerSuggest', title: '' },
225
- sortText: '00',
225
+ sortText: 'ab',
226
226
  },
227
227
  ]);
228
228
  });
@@ -246,7 +246,7 @@ describe('generateEntitiesSuggestion', () => {
246
246
  endColumn: 1,
247
247
  },
248
248
  command: undefined,
249
- sortText: '00',
249
+ sortText: 'aa',
250
250
  },
251
251
  {
252
252
  label: 'entity2/',
@@ -261,7 +261,7 @@ describe('generateEntitiesSuggestion', () => {
261
261
  endColumn: 1,
262
262
  },
263
263
  command: { id: 'editor.action.triggerSuggest', title: '' },
264
- sortText: '00',
264
+ sortText: 'ab',
265
265
  },
266
266
  ]);
267
267
  });
@@ -285,7 +285,7 @@ describe('generateEntitiesSuggestion', () => {
285
285
  endColumn: 1,
286
286
  },
287
287
  command: { id: 'editor.action.triggerSuggest', title: '' },
288
- sortText: '00',
288
+ sortText: 'aa',
289
289
  },
290
290
  {
291
291
  label: 'entity2',
@@ -300,7 +300,7 @@ describe('generateEntitiesSuggestion', () => {
300
300
  endColumn: 1,
301
301
  },
302
302
  command: undefined,
303
- sortText: '00',
303
+ sortText: 'ab',
304
304
  },
305
305
  ]);
306
306
  });
@@ -1,4 +1,5 @@
1
- import { suggestionIndexToWeight, wrapStringToBackticks } from '../utils';
1
+ import { suggestionIndexToWeight } from '../../../utils';
2
+ import { wrapStringToBackticks } from '../utils';
2
3
  describe('wrapStringToBackticks', () => {
3
4
  it('should return the same string if it already starts and ends with backticks', () => {
4
5
  expect(wrapStringToBackticks('`test`')).toBe('`test`');
@@ -1,5 +1,6 @@
1
1
  import * as monaco from '../../fillers/monaco-editor-core';
2
- import { getSuggestionIndex, isVariable, removeBackticks, removeStringDuplicates, suggestionIndexToWeight, wrapStringToBackticks, } from './utils';
2
+ import { getSuggestionIndex, isVariable, removeBackticks, removeStringDuplicates, wrapStringToBackticks, } from './utils';
3
+ import { suggestionIndexToWeight } from '../../utils';
3
4
  export async function generateSimpleTypesSuggestion(rangeToInsertSuggestion, simpleTypes = []) {
4
5
  return simpleTypes.map((el) => ({
5
6
  label: el,
@@ -1,7 +1,6 @@
1
1
  import type { YqlAutocompleteResult } from '@gravity-ui/websql-autocomplete/yql';
2
2
  type SuggestionType = keyof Omit<YqlAutocompleteResult, 'errors' | 'suggestDatabases'> | 'suggestAllColumns';
3
3
  export declare function getSuggestionIndex(suggestionType: SuggestionType): number;
4
- export declare function suggestionIndexToWeight(index: number): string;
5
4
  export declare function isVariable(value: string): boolean;
6
5
  export declare function removeStringDuplicates(value?: string[]): string[];
7
6
  export declare function getEntitiesToFetchColumns(suggestColumns: YqlAutocompleteResult['suggestColumns']): string[];
@@ -20,17 +20,6 @@ const SuggestionsWeight = {
20
20
  export function getSuggestionIndex(suggestionType) {
21
21
  return SuggestionsWeight[suggestionType];
22
22
  }
23
- const alphabet = 'abcdefghijklmnopqrstuvwxyz';
24
- export function suggestionIndexToWeight(index) {
25
- const characterInsideAlphabet = alphabet[index];
26
- if (characterInsideAlphabet) {
27
- return characterInsideAlphabet;
28
- }
29
- const duplicateTimes = Math.floor(index / alphabet.length);
30
- const remains = index % alphabet.length;
31
- const lastCharacter = alphabet.slice(-1);
32
- return lastCharacter.repeat(duplicateTimes) + alphabet[remains];
33
- }
34
23
  export function isVariable(value) {
35
24
  return value.startsWith('$');
36
25
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "monaco-yql-languages",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "description": "YQL languages for the Monaco Editor, based on monaco-languages.",
5
5
  "author": "YDB",
6
6
  "license": "MIT",