Skip to content

Commit 9454a36

Browse files
committed
refactor: improve coding
Signed-off-by: seven <[email protected]>
1 parent acd036a commit 9454a36

File tree

2 files changed

+128
-111
lines changed

2 files changed

+128
-111
lines changed

src/common/monaco/index.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,34 @@ monaco.languages.registerCompletionItemProvider(search.id, {
2626
endLineNumber: position.lineNumber,
2727
endColumn: position.column,
2828
});
29-
const matches = new Map<RegExp, string>([
29+
30+
const methods = new Map<RegExp, string>([
3031
[/^ge?t?$/gi, 'GET '],
3132
[/^put?$/gi, 'PUT '],
3233
[/^pos?t?$/gi, 'POST '],
3334
[/^de?l?e?t?e?$/gi, 'DELETE '],
3435
]);
35-
const matchKey = Array.from(matches.keys()).find(regex => regex.test(textUntilPosition));
36-
if (!matchKey) {
36+
const matchedMethodKey = Array.from(methods.keys()).find(regex =>
37+
regex.test(textUntilPosition),
38+
);
39+
if (!matchedMethodKey) {
3740
return;
3841
}
39-
const word = matches.get(matchKey);
42+
43+
const method = methods.get(matchedMethodKey);
4044
const range = {
4145
startLineNumber: position.lineNumber,
4246
endLineNumber: position.lineNumber,
43-
startColumn: position.column - word!.length,
47+
startColumn: position.column - method!.length,
4448
endColumn: position.column,
4549
};
4650

4751
return {
4852
suggestions: [
4953
{
50-
label: word,
51-
kind: monaco.languages.CompletionItemKind.Keyword,
52-
insertText: word,
54+
label: method,
55+
kind: monaco.languages.CompletionItemKind.Constant,
56+
insertText: method,
5357
range: range,
5458
},
5559
],

src/common/monaco/lexerRules.ts

+116-103
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,113 @@ export const executeActions = {
100100
regexp: /^(GET|DELETE|POST|PUT)\s\w+/,
101101
decorationClassName: 'action-execute-decoration',
102102
};
103-
103+
const keywords = [
104+
'GET',
105+
'POST',
106+
'PUT',
107+
'DELETE',
108+
'HEAD',
109+
'OPTIONS',
110+
'PATCH',
111+
'TRACE',
112+
'index',
113+
'indices',
114+
'type',
115+
'types',
116+
'from',
117+
'size',
118+
'explain',
119+
'analyze',
120+
'default_operator',
121+
'df',
122+
'analyzer',
123+
'lenient',
124+
'lowercase_expanded_terms',
125+
'analyze_wildcard',
126+
'all_shards',
127+
'allow_no_indices',
128+
'expand_wildcards',
129+
'preference',
130+
'routing',
131+
'ignore_unavailable',
132+
'allow_no_indices',
133+
'ignore_throttled',
134+
'search_type',
135+
'batched_reduce_size',
136+
'ccs_minimize_roundtrips',
137+
'max_concurrent_shard_requests',
138+
'pre_filter_shard_size',
139+
'rest_total_hits_as_int',
140+
'scroll',
141+
'search_type',
142+
'typed_keys',
143+
'wait_for_active_shards',
144+
'wait_for_completion',
145+
'requests_per_second',
146+
'slices',
147+
'timeout',
148+
'terminate_after',
149+
'stats',
150+
'version',
151+
'version_type',
152+
'if_seq_no',
153+
'if_primary_term',
154+
'refresh',
155+
'routing',
156+
'parent',
157+
'preference',
158+
'realtime',
159+
'refresh',
160+
'retry_on_conflict',
161+
'timeout',
162+
'version',
163+
'version_type',
164+
'if_seq_no',
165+
'if_primary_term',
166+
'pipeline',
167+
'wait_for_active_shards',
168+
'wait_for_completion',
169+
'requests_per_second',
170+
'slices',
171+
'timeout',
172+
'terminate_after',
173+
'stats',
174+
'version',
175+
'version_type',
176+
'if_seq_no',
177+
'if_primary_term',
178+
'refresh',
179+
'routing',
180+
'parent',
181+
'preference',
182+
'realtime',
183+
'refresh',
184+
'retry_on_conflict',
185+
'timeout',
186+
'version',
187+
'version_type',
188+
'if_seq_no',
189+
'if_primary_term',
190+
'pipeline',
191+
'wait_for_active_shards',
192+
'wait_for_completion',
193+
'requests_per_second',
194+
'slices',
195+
'timeout',
196+
'terminate_after',
197+
'stats',
198+
'version',
199+
'version_type',
200+
'_search',
201+
'_count',
202+
'_mapping',
203+
'_cluster',
204+
'_nodes',
205+
'_aliases',
206+
'_doc',
207+
'_update',
208+
'_bulk',
209+
];
104210
export const search = {
105211
id: 'search',
106212
rules: {
@@ -109,105 +215,7 @@ export const search = {
109215
tokenPostfix: '.search',
110216

111217
// keywords of elasticsearch
112-
keywords: [
113-
'GET',
114-
'POST',
115-
'PUT',
116-
'DELETE',
117-
'HEAD',
118-
'OPTIONS',
119-
'PATCH',
120-
'TRACE',
121-
'index',
122-
'indices',
123-
'type',
124-
'types',
125-
'from',
126-
'size',
127-
'explain',
128-
'analyze',
129-
'default_operator',
130-
'df',
131-
'analyzer',
132-
'lenient',
133-
'lowercase_expanded_terms',
134-
'analyze_wildcard',
135-
'all_shards',
136-
'allow_no_indices',
137-
'expand_wildcards',
138-
'preference',
139-
'routing',
140-
'ignore_unavailable',
141-
'allow_no_indices',
142-
'ignore_throttled',
143-
'search_type',
144-
'batched_reduce_size',
145-
'ccs_minimize_roundtrips',
146-
'max_concurrent_shard_requests',
147-
'pre_filter_shard_size',
148-
'rest_total_hits_as_int',
149-
'scroll',
150-
'search_type',
151-
'typed_keys',
152-
'wait_for_active_shards',
153-
'wait_for_completion',
154-
'requests_per_second',
155-
'slices',
156-
'timeout',
157-
'terminate_after',
158-
'stats',
159-
'version',
160-
'version_type',
161-
'if_seq_no',
162-
'if_primary_term',
163-
'refresh',
164-
'routing',
165-
'parent',
166-
'preference',
167-
'realtime',
168-
'refresh',
169-
'retry_on_conflict',
170-
'timeout',
171-
'version',
172-
'version_type',
173-
'if_seq_no',
174-
'if_primary_term',
175-
'pipeline',
176-
'wait_for_active_shards',
177-
'wait_for_completion',
178-
'requests_per_second',
179-
'slices',
180-
'timeout',
181-
'terminate_after',
182-
'stats',
183-
'version',
184-
'version_type',
185-
'if_seq_no',
186-
'if_primary_term',
187-
'refresh',
188-
'routing',
189-
'parent',
190-
'preference',
191-
'realtime',
192-
'refresh',
193-
'retry_on_conflict',
194-
'timeout',
195-
'version',
196-
'version_type',
197-
'if_seq_no',
198-
'if_primary_term',
199-
'pipeline',
200-
'wait_for_active_shards',
201-
'wait_for_completion',
202-
'requests_per_second',
203-
'slices',
204-
'timeout',
205-
'terminate_after',
206-
'stats',
207-
'version',
208-
'version_type',
209-
'_search',
210-
],
218+
keywords,
211219

212220
typeKeywords: ['any', 'boolean', 'number', 'object', 'string', 'undefined'],
213221

@@ -222,11 +230,16 @@ export const search = {
222230
// The main tokenizer for our languages
223231
tokenizer: {
224232
root: [
225-
[executeActions.regexp, executeActions.decorationClassName],
226-
[/[{}]/, 'delimiter.bracket'],
233+
{
234+
regex: '{',
235+
action: {
236+
token: 'paren.lparen',
237+
next: 'json',
238+
},
239+
},
227240
{ include: 'common' },
228241
],
229-
242+
constant: [[executeActions.regexp, executeActions.decorationClassName]],
230243
common: [
231244
// identifiers and keywords
232245
[

0 commit comments

Comments
 (0)