clarity-pattern-parser 11.3.8 → 11.3.10
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.
- package/dist/index.browser.js +61 -34
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +61 -34
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +61 -34
- package/dist/index.js.map +1 -1
- package/dist/patterns/InfiniteRepeat.d.ts +1 -0
- package/package.json +1 -1
- package/src/intellisense/AutoComplete.test.ts +23 -0
- package/src/patterns/FiniteRepeat.ts +4 -6
- package/src/patterns/InfiniteRepeat.ts +7 -1
package/dist/index.js
CHANGED
|
@@ -1255,12 +1255,10 @@ class FiniteRepeat {
|
|
|
1255
1255
|
}
|
|
1256
1256
|
}
|
|
1257
1257
|
}
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
cursor.moveTo(node.firstIndex);
|
|
1263
|
-
}
|
|
1258
|
+
const endedOnDivider = this._hasDivider && nodes.length % modulo === 0;
|
|
1259
|
+
if (this._trimDivider && endedOnDivider) {
|
|
1260
|
+
const node = nodes.pop();
|
|
1261
|
+
cursor.moveTo(node.firstIndex);
|
|
1264
1262
|
}
|
|
1265
1263
|
if (matchCount < this._min) {
|
|
1266
1264
|
const lastIndex = cursor.index;
|
|
@@ -1396,6 +1394,7 @@ class InfiniteRepeat {
|
|
|
1396
1394
|
this._firstIndex = 0;
|
|
1397
1395
|
this._nodes = [];
|
|
1398
1396
|
this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
|
|
1397
|
+
this._patterns = [];
|
|
1399
1398
|
}
|
|
1400
1399
|
_assignChildrenToParent(children) {
|
|
1401
1400
|
for (const child of children) {
|
|
@@ -1411,6 +1410,7 @@ class InfiniteRepeat {
|
|
|
1411
1410
|
parse(cursor) {
|
|
1412
1411
|
this._firstIndex = cursor.index;
|
|
1413
1412
|
this._nodes = [];
|
|
1413
|
+
this._patterns = [];
|
|
1414
1414
|
const passed = this._tryToParse(cursor);
|
|
1415
1415
|
if (passed) {
|
|
1416
1416
|
cursor.resolveError();
|
|
@@ -1462,6 +1462,7 @@ class InfiniteRepeat {
|
|
|
1462
1462
|
}
|
|
1463
1463
|
if (repeatNode != null) {
|
|
1464
1464
|
this._nodes.push(repeatNode);
|
|
1465
|
+
this._patterns.push(this._pattern);
|
|
1465
1466
|
if (!cursor.hasNext()) {
|
|
1466
1467
|
passed = true;
|
|
1467
1468
|
break;
|
|
@@ -1486,6 +1487,7 @@ class InfiniteRepeat {
|
|
|
1486
1487
|
}
|
|
1487
1488
|
else {
|
|
1488
1489
|
this._nodes.push(dividerNode);
|
|
1490
|
+
this._patterns.push(this._divider);
|
|
1489
1491
|
if (!cursor.hasNext()) {
|
|
1490
1492
|
passed = true;
|
|
1491
1493
|
break;
|
|
@@ -1508,11 +1510,11 @@ class InfiniteRepeat {
|
|
|
1508
1510
|
return passed;
|
|
1509
1511
|
}
|
|
1510
1512
|
_createNode(cursor) {
|
|
1511
|
-
var _a;
|
|
1512
1513
|
const hasDivider = this._divider != null;
|
|
1514
|
+
const lastPattern = this._patterns[this._patterns.length - 1];
|
|
1513
1515
|
if (hasDivider &&
|
|
1514
1516
|
this._trimDivider &&
|
|
1515
|
-
|
|
1517
|
+
lastPattern === this._divider) {
|
|
1516
1518
|
const dividerNode = this._nodes.pop();
|
|
1517
1519
|
cursor.moveTo(dividerNode.firstIndex);
|
|
1518
1520
|
}
|
|
@@ -4060,7 +4062,7 @@ class AutoComplete {
|
|
|
4060
4062
|
const furthestError = cursor.furthestError;
|
|
4061
4063
|
const furthestMatch = cursor.allMatchedNodes[cursor.allMatchedNodes.length - 1];
|
|
4062
4064
|
if (furthestError && furthestMatch) {
|
|
4063
|
-
if (
|
|
4065
|
+
if (furthestMatch.endIndex > furthestError.lastIndex) {
|
|
4064
4066
|
return furthestMatch.endIndex;
|
|
4065
4067
|
}
|
|
4066
4068
|
else {
|
|
@@ -4115,7 +4117,7 @@ class AutoComplete {
|
|
|
4115
4117
|
}
|
|
4116
4118
|
_createSuggestionsFromRoot() {
|
|
4117
4119
|
const suggestions = [];
|
|
4118
|
-
const tokens = this._pattern.getTokens();
|
|
4120
|
+
const tokens = [...this._pattern.getTokens(), ...this._getTokensForPattern(this._pattern)];
|
|
4119
4121
|
for (const token of tokens) {
|
|
4120
4122
|
if (suggestions.findIndex(s => s.text === token) === -1) {
|
|
4121
4123
|
suggestions.push(this._createSuggestion("", token));
|
|
@@ -4127,15 +4129,29 @@ class AutoComplete {
|
|
|
4127
4129
|
if (match.pattern == null) {
|
|
4128
4130
|
return this._createSuggestions(-1, this._getTokensForPattern(this._pattern));
|
|
4129
4131
|
}
|
|
4130
|
-
|
|
4131
|
-
|
|
4132
|
-
|
|
4133
|
-
|
|
4134
|
-
|
|
4135
|
-
|
|
4132
|
+
if (match.node != null) {
|
|
4133
|
+
const textStartingMatch = this._text.slice(match.node.startIndex, match.node.endIndex);
|
|
4134
|
+
const currentPatternsTokens = this._getTokensForPattern(match.pattern);
|
|
4135
|
+
/**
|
|
4136
|
+
* Compares tokens to current text and extracts remainder tokens
|
|
4137
|
+
* - IE. **currentText:** *abc*, **baseToken:** *abcdef*, **trailingToken:** *def*
|
|
4138
|
+
*/
|
|
4139
|
+
const trailingTokens = currentPatternsTokens.reduce((acc, token) => {
|
|
4140
|
+
if (token.startsWith(textStartingMatch)) {
|
|
4141
|
+
const sliced = token.slice(textStartingMatch.length);
|
|
4142
|
+
if (sliced !== '') {
|
|
4143
|
+
acc.push(sliced);
|
|
4144
|
+
}
|
|
4145
|
+
}
|
|
4136
4146
|
return acc;
|
|
4137
4147
|
}, []);
|
|
4138
|
-
|
|
4148
|
+
const leafPatterns = match.pattern.getNextPatterns();
|
|
4149
|
+
const leafTokens = leafPatterns.reduce((acc, leafPattern) => {
|
|
4150
|
+
acc.push(...this._getTokensForPattern(leafPattern));
|
|
4151
|
+
return acc;
|
|
4152
|
+
}, []);
|
|
4153
|
+
const allTokens = [...trailingTokens, ...leafTokens];
|
|
4154
|
+
return this._createSuggestions(match.node.lastIndex, allTokens);
|
|
4139
4155
|
}
|
|
4140
4156
|
else {
|
|
4141
4157
|
return [];
|
|
@@ -4145,44 +4161,54 @@ class AutoComplete {
|
|
|
4145
4161
|
const augmentedTokens = this._getAugmentedTokens(pattern);
|
|
4146
4162
|
if (this._options.greedyPatternNames != null && this._options.greedyPatternNames.includes(pattern.name)) {
|
|
4147
4163
|
const nextPatterns = pattern.getNextPatterns();
|
|
4148
|
-
const tokens = [];
|
|
4149
4164
|
const nextPatternTokens = nextPatterns.reduce((acc, pattern) => {
|
|
4150
4165
|
acc.push(...this._getTokensForPattern(pattern));
|
|
4151
4166
|
return acc;
|
|
4152
4167
|
}, []);
|
|
4153
|
-
|
|
4154
|
-
|
|
4155
|
-
|
|
4168
|
+
// using set to prevent duplicates
|
|
4169
|
+
const tokens = new Set();
|
|
4170
|
+
for (const token of augmentedTokens) {
|
|
4171
|
+
for (const nextPatternToken of nextPatternTokens) {
|
|
4172
|
+
tokens.add(token + nextPatternToken);
|
|
4156
4173
|
}
|
|
4157
4174
|
}
|
|
4158
|
-
return tokens;
|
|
4175
|
+
return [...tokens];
|
|
4159
4176
|
}
|
|
4160
4177
|
else {
|
|
4161
4178
|
return augmentedTokens;
|
|
4162
4179
|
}
|
|
4163
4180
|
}
|
|
4164
4181
|
_getAugmentedTokens(pattern) {
|
|
4182
|
+
var _a, _b;
|
|
4165
4183
|
const customTokensMap = this._options.customTokens || {};
|
|
4166
4184
|
const leafPatterns = pattern.getPatterns();
|
|
4167
|
-
|
|
4168
|
-
|
|
4169
|
-
|
|
4170
|
-
|
|
4171
|
-
|
|
4172
|
-
|
|
4185
|
+
/** Using Set to
|
|
4186
|
+
* - prevent duplicates
|
|
4187
|
+
* - prevent mutation of original customTokensMap
|
|
4188
|
+
*/
|
|
4189
|
+
const customTokensForPattern = new Set((_a = customTokensMap[pattern.name]) !== null && _a !== void 0 ? _a : []);
|
|
4190
|
+
for (const lp of leafPatterns) {
|
|
4191
|
+
const augmentedTokens = (_b = customTokensMap[lp.name]) !== null && _b !== void 0 ? _b : [];
|
|
4192
|
+
const lpsCombinedTokens = [...lp.getTokens(), ...augmentedTokens];
|
|
4193
|
+
for (const token of lpsCombinedTokens) {
|
|
4194
|
+
customTokensForPattern.add(token);
|
|
4195
|
+
}
|
|
4196
|
+
}
|
|
4197
|
+
return [...customTokensForPattern];
|
|
4173
4198
|
}
|
|
4174
4199
|
_createSuggestions(lastIndex, tokens) {
|
|
4175
|
-
let
|
|
4200
|
+
let textToIndex = lastIndex === -1 ? "" : this._cursor.getChars(0, lastIndex);
|
|
4176
4201
|
const suggestionStrings = [];
|
|
4177
4202
|
const options = [];
|
|
4178
4203
|
for (const token of tokens) {
|
|
4179
|
-
|
|
4180
|
-
const
|
|
4204
|
+
// concatenated for start index identification inside createSuggestion
|
|
4205
|
+
const suggestion = textToIndex + token;
|
|
4181
4206
|
const alreadyExist = suggestionStrings.includes(suggestion);
|
|
4182
4207
|
const isSameAsText = suggestion === this._text;
|
|
4183
|
-
if (
|
|
4208
|
+
if (!alreadyExist && !isSameAsText) {
|
|
4184
4209
|
suggestionStrings.push(suggestion);
|
|
4185
|
-
|
|
4210
|
+
const suggestionOption = this._createSuggestion(this._cursor.text, suggestion);
|
|
4211
|
+
options.push(suggestionOption);
|
|
4186
4212
|
}
|
|
4187
4213
|
}
|
|
4188
4214
|
const reducedOptions = getFurthestOptions(options);
|
|
@@ -4192,10 +4218,11 @@ class AutoComplete {
|
|
|
4192
4218
|
_createSuggestion(fullText, suggestion) {
|
|
4193
4219
|
const furthestMatch = findMatchIndex(suggestion, fullText);
|
|
4194
4220
|
const text = suggestion.slice(furthestMatch);
|
|
4195
|
-
|
|
4221
|
+
const option = {
|
|
4196
4222
|
text: text,
|
|
4197
4223
|
startIndex: furthestMatch,
|
|
4198
4224
|
};
|
|
4225
|
+
return option;
|
|
4199
4226
|
}
|
|
4200
4227
|
static suggestFor(text, pattern, options) {
|
|
4201
4228
|
return new AutoComplete(pattern, options).suggestFor(text);
|