-
Notifications
You must be signed in to change notification settings - Fork 981
/
Copy pathbody_completer.js
316 lines (294 loc) · 10.2 KB
/
body_completer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import _ from 'lodash';
import { WalkingState, walkTokenPath, wrapComponentWithDefaults } from './engine';
import {
ConstantComponent,
SharedComponent,
ObjectComponent,
ConditionalProxy,
GlobalOnlyComponent,
} from './components';
function CompilingContext(endpointId, parametrizedComponentFactories) {
this.parametrizedComponentFactories = parametrizedComponentFactories;
this.endpointId = endpointId;
}
/**
* An object to resolve scope links (syntax endpoint.path1.path2)
* @param link the link either string (endpoint.path1.path2, or .path1.path2) or a function (context,editor)
* which returns a description to be compiled
* @constructor
* @param compilingContext
*
*
* For this to work we expect the context to include a method context.endpointComponentResolver(endpoint)
* which should return the top level components for the given endpoint
*/
function resolvePathToComponents(tokenPath, context, editor, components) {
const walkStates = walkTokenPath(
tokenPath,
[new WalkingState('ROOT', components, [])],
context,
editor
);
const result = [].concat.apply([], _.map(walkStates, 'components'));
return result;
}
class ScopeResolver extends SharedComponent {
constructor(link, compilingContext) {
super('__scope_link');
if (_.isString(link) && link[0] === '.') {
// relative link, inject current endpoint
if (link === '.') {
link = compilingContext.endpointId;
} else {
link = compilingContext.endpointId + link;
}
}
this.link = link;
this.compilingContext = compilingContext;
}
resolveLinkToComponents(context, editor) {
if (_.isFunction(this.link)) {
const desc = this.link(context, editor);
return compileDescription(desc, this.compilingContext);
}
if (!_.isString(this.link)) {
throw new Error('unsupported link format', this.link);
}
let path = this.link.replace(/\./g, '{').split(/(\{)/);
const endpoint = path[0];
let components;
try {
if (endpoint === 'GLOBAL') {
// global rules need an extra indirection
if (path.length < 3) {
throw new Error('missing term in global link: ' + this.link);
}
const term = path[2];
components = context.globalComponentResolver(term);
path = path.slice(3);
} else {
path = path.slice(1);
components = context.endpointComponentResolver(endpoint);
}
} catch (e) {
throw new Error('failed to resolve link [' + this.link + ']: ' + e);
}
return resolvePathToComponents(path, context, editor, components);
}
getTerms(context, editor) {
const options = [];
const components = this.resolveLinkToComponents(context, editor);
_.each(components, function (component) {
options.push.apply(options, component.getTerms(context, editor));
});
return options;
}
match(token, context, editor) {
const result = {
next: [],
};
const components = this.resolveLinkToComponents(context, editor);
_.each(components, function (component) {
const componentResult = component.match(token, context, editor);
if (componentResult && componentResult.next) {
result.next.push.apply(result.next, componentResult.next);
}
});
return result;
}
}
function getTemplate(description) {
if (description.__template) {
if (description.__raw && _.isString(description.__template)) {
return {
// This is a special secret attribute that gets passed through to indicate that
// the raw value should be passed through to the console without JSON.stringifying it
// first.
//
// Primary use case is to allow __templates to contain extended JSON special values like
// triple quotes.
__raw: true,
value: description.__template,
};
}
return description.__template;
} else if (description.__one_of) {
return getTemplate(description.__one_of[0]);
} else if (description.__any_of) {
return [];
} else if (description.__scope_link) {
// assume an object for now.
return {};
} else if (Array.isArray(description)) {
if (description.length === 1) {
if (_.isObject(description[0])) {
// shortcut to save typing
const innerTemplate = getTemplate(description[0]);
return innerTemplate != null ? [innerTemplate] : [];
}
}
return [];
} else if (_.isObject(description)) {
return {};
} else if (_.isString(description) && !/^\{.*\}$/.test(description)) {
return description;
} else {
return description;
}
}
function getOptions(description) {
const options = {};
const template = getTemplate(description);
if (!_.isUndefined(template)) {
options.template = template;
}
return options;
}
/**
* @param description a json dict describing the endpoint
* @param compilingContext
*/
function compileDescription(description, compilingContext) {
if (Array.isArray(description)) {
return [compileList(description, compilingContext)];
} else if (_.isObject(description)) {
// test for objects list as arrays are also objects
if (description.__scope_link) {
return [new ScopeResolver(description.__scope_link, compilingContext)];
}
if (description.__any_of) {
return [compileList(description.__any_of, compilingContext)];
}
if (description.__one_of) {
return _.flatten(
_.map(description.__one_of, function (d) {
return compileDescription(d, compilingContext);
})
);
}
const obj = compileObject(description, compilingContext);
if (description.__condition) {
return [compileCondition(description.__condition, obj, compilingContext)];
} else {
return [obj];
}
} else if (_.isString(description) && /^\{.*\}$/.test(description)) {
return [compileParametrizedValue(description, compilingContext)];
} else {
return [new ConstantComponent(description)];
}
}
function compileParametrizedValue(value, compilingContext, template) {
value = value.substr(1, value.length - 2).toLowerCase();
let component = compilingContext.parametrizedComponentFactories.getComponent(value, true);
if (!component) {
throw new Error("no factory found for '" + value + "'");
}
component = component(value, null, template);
if (!_.isUndefined(template)) {
component = wrapComponentWithDefaults(component, { template: template });
}
return component;
}
function compileObject(objDescription, compilingContext) {
const objectC = new ConstantComponent('{');
const constants = [];
const patterns = [];
_.each(objDescription, function (desc, key) {
if (key.indexOf('__') === 0) {
// meta key
return;
}
const options = getOptions(desc);
let component;
if (/^\{.*\}$/.test(key)) {
component = compileParametrizedValue(key, compilingContext, options.template);
patterns.push(component);
} else if (key === '*') {
component = new SharedComponent(key);
patterns.push(component);
} else {
options.name = key;
component = new ConstantComponent(key, null, [options]);
constants.push(component);
}
_.map(compileDescription(desc, compilingContext), function (subComponent) {
component.addComponent(subComponent);
});
});
objectC.addComponent(new ObjectComponent('inner', constants, patterns));
return objectC;
}
function compileList(listRule, compilingContext) {
const listC = new ConstantComponent('[');
_.each(listRule, function (desc) {
_.each(compileDescription(desc, compilingContext), function (component) {
listC.addComponent(component);
});
});
return listC;
}
/** takes a compiled object and wraps in a {@link ConditionalProxy }*/
function compileCondition(description, compiledObject) {
if (description.lines_regex) {
return new ConditionalProxy(function (context, editor) {
const lines = editor
.getLines(context.requestStartRow, editor.getCurrentPosition().lineNumber)
.join('\n');
return new RegExp(description.lines_regex, 'm').test(lines);
}, compiledObject);
} else {
throw 'unknown condition type - got: ' + JSON.stringify(description);
}
}
// a list of component that match anything but give auto complete suggestions based on global API entries.
export function globalsOnlyAutocompleteComponents() {
return [new GlobalOnlyComponent('__global__')];
}
/**
* @param endpointId id of the endpoint being compiled.
* @param description a json dict describing the endpoint
* @param endpointComponentResolver a function (endpoint,context,editor) which should resolve an endpoint
* to it's list of compiled components.
* @param parametrizedComponentFactories a dict of the following structure
* that will be used as a fall back for pattern keys (i.e.: {type} ,resolved without the $s)
* {
* TYPE: function (part, parent, endpoint) {
* return new SharedComponent(part, parent)
* }
* }
*/
export function compileBodyDescription(endpointId, description, parametrizedComponentFactories) {
return compileDescription(
description,
new CompilingContext(endpointId, parametrizedComponentFactories)
);
}