clarity-pattern-parser 11.3.8 → 11.3.9

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.
@@ -4062,7 +4062,7 @@
4062
4062
  const furthestError = cursor.furthestError;
4063
4063
  const furthestMatch = cursor.allMatchedNodes[cursor.allMatchedNodes.length - 1];
4064
4064
  if (furthestError && furthestMatch) {
4065
- if (furthestError.lastIndex > furthestMatch.endIndex) {
4065
+ if (furthestMatch.endIndex > furthestError.lastIndex) {
4066
4066
  return furthestMatch.endIndex;
4067
4067
  }
4068
4068
  else {
@@ -4117,7 +4117,7 @@
4117
4117
  }
4118
4118
  _createSuggestionsFromRoot() {
4119
4119
  const suggestions = [];
4120
- const tokens = this._pattern.getTokens();
4120
+ const tokens = [...this._pattern.getTokens(), ...this._getTokensForPattern(this._pattern)];
4121
4121
  for (const token of tokens) {
4122
4122
  if (suggestions.findIndex(s => s.text === token) === -1) {
4123
4123
  suggestions.push(this._createSuggestion("", token));
@@ -4129,15 +4129,29 @@
4129
4129
  if (match.pattern == null) {
4130
4130
  return this._createSuggestions(-1, this._getTokensForPattern(this._pattern));
4131
4131
  }
4132
- const leafPattern = match.pattern;
4133
- const parent = match.pattern.parent;
4134
- if (parent !== null && match.node != null) {
4135
- const patterns = leafPattern.getNextPatterns();
4136
- const tokens = patterns.reduce((acc, pattern) => {
4137
- acc.push(...this._getTokensForPattern(pattern));
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
+ }
4146
+ return acc;
4147
+ }, []);
4148
+ const leafPatterns = match.pattern.getNextPatterns();
4149
+ const leafTokens = leafPatterns.reduce((acc, leafPattern) => {
4150
+ acc.push(...this._getTokensForPattern(leafPattern));
4138
4151
  return acc;
4139
4152
  }, []);
4140
- return this._createSuggestions(match.node.lastIndex, tokens);
4153
+ const allTokens = [...trailingTokens, ...leafTokens];
4154
+ return this._createSuggestions(match.node.lastIndex, allTokens);
4141
4155
  }
4142
4156
  else {
4143
4157
  return [];
@@ -4147,44 +4161,54 @@
4147
4161
  const augmentedTokens = this._getAugmentedTokens(pattern);
4148
4162
  if (this._options.greedyPatternNames != null && this._options.greedyPatternNames.includes(pattern.name)) {
4149
4163
  const nextPatterns = pattern.getNextPatterns();
4150
- const tokens = [];
4151
4164
  const nextPatternTokens = nextPatterns.reduce((acc, pattern) => {
4152
4165
  acc.push(...this._getTokensForPattern(pattern));
4153
4166
  return acc;
4154
4167
  }, []);
4155
- for (let token of augmentedTokens) {
4156
- for (let nextPatternToken of nextPatternTokens) {
4157
- tokens.push(token + nextPatternToken);
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);
4158
4173
  }
4159
4174
  }
4160
- return tokens;
4175
+ return [...tokens];
4161
4176
  }
4162
4177
  else {
4163
4178
  return augmentedTokens;
4164
4179
  }
4165
4180
  }
4166
4181
  _getAugmentedTokens(pattern) {
4182
+ var _a, _b;
4167
4183
  const customTokensMap = this._options.customTokens || {};
4168
4184
  const leafPatterns = pattern.getPatterns();
4169
- const tokens = customTokensMap[pattern.name] || [];
4170
- leafPatterns.forEach(p => {
4171
- const augmentedTokens = customTokensMap[p.name] || [];
4172
- tokens.push(...p.getTokens(), ...augmentedTokens);
4173
- });
4174
- return tokens;
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];
4175
4198
  }
4176
4199
  _createSuggestions(lastIndex, tokens) {
4177
- let substring = lastIndex === -1 ? "" : this._cursor.getChars(0, lastIndex);
4200
+ let textToIndex = lastIndex === -1 ? "" : this._cursor.getChars(0, lastIndex);
4178
4201
  const suggestionStrings = [];
4179
4202
  const options = [];
4180
4203
  for (const token of tokens) {
4181
- const suggestion = substring + token;
4182
- const startsWith = suggestion.startsWith(substring);
4204
+ // concatenated for start index identification inside createSuggestion
4205
+ const suggestion = textToIndex + token;
4183
4206
  const alreadyExist = suggestionStrings.includes(suggestion);
4184
4207
  const isSameAsText = suggestion === this._text;
4185
- if (startsWith && !alreadyExist && !isSameAsText) {
4208
+ if (!alreadyExist && !isSameAsText) {
4186
4209
  suggestionStrings.push(suggestion);
4187
- options.push(this._createSuggestion(this._cursor.text, suggestion));
4210
+ const suggestionOption = this._createSuggestion(this._cursor.text, suggestion);
4211
+ options.push(suggestionOption);
4188
4212
  }
4189
4213
  }
4190
4214
  const reducedOptions = getFurthestOptions(options);
@@ -4194,10 +4218,11 @@
4194
4218
  _createSuggestion(fullText, suggestion) {
4195
4219
  const furthestMatch = findMatchIndex(suggestion, fullText);
4196
4220
  const text = suggestion.slice(furthestMatch);
4197
- return {
4221
+ const option = {
4198
4222
  text: text,
4199
4223
  startIndex: furthestMatch,
4200
4224
  };
4225
+ return option;
4201
4226
  }
4202
4227
  static suggestFor(text, pattern, options) {
4203
4228
  return new AutoComplete(pattern, options).suggestFor(text);