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.
package/dist/index.esm.js CHANGED
@@ -4056,7 +4056,7 @@ class AutoComplete {
4056
4056
  const furthestError = cursor.furthestError;
4057
4057
  const furthestMatch = cursor.allMatchedNodes[cursor.allMatchedNodes.length - 1];
4058
4058
  if (furthestError && furthestMatch) {
4059
- if (furthestError.lastIndex > furthestMatch.endIndex) {
4059
+ if (furthestMatch.endIndex > furthestError.lastIndex) {
4060
4060
  return furthestMatch.endIndex;
4061
4061
  }
4062
4062
  else {
@@ -4111,7 +4111,7 @@ class AutoComplete {
4111
4111
  }
4112
4112
  _createSuggestionsFromRoot() {
4113
4113
  const suggestions = [];
4114
- const tokens = this._pattern.getTokens();
4114
+ const tokens = [...this._pattern.getTokens(), ...this._getTokensForPattern(this._pattern)];
4115
4115
  for (const token of tokens) {
4116
4116
  if (suggestions.findIndex(s => s.text === token) === -1) {
4117
4117
  suggestions.push(this._createSuggestion("", token));
@@ -4123,15 +4123,29 @@ class AutoComplete {
4123
4123
  if (match.pattern == null) {
4124
4124
  return this._createSuggestions(-1, this._getTokensForPattern(this._pattern));
4125
4125
  }
4126
- const leafPattern = match.pattern;
4127
- const parent = match.pattern.parent;
4128
- if (parent !== null && match.node != null) {
4129
- const patterns = leafPattern.getNextPatterns();
4130
- const tokens = patterns.reduce((acc, pattern) => {
4131
- acc.push(...this._getTokensForPattern(pattern));
4126
+ if (match.node != null) {
4127
+ const textStartingMatch = this._text.slice(match.node.startIndex, match.node.endIndex);
4128
+ const currentPatternsTokens = this._getTokensForPattern(match.pattern);
4129
+ /**
4130
+ * Compares tokens to current text and extracts remainder tokens
4131
+ * - IE. **currentText:** *abc*, **baseToken:** *abcdef*, **trailingToken:** *def*
4132
+ */
4133
+ const trailingTokens = currentPatternsTokens.reduce((acc, token) => {
4134
+ if (token.startsWith(textStartingMatch)) {
4135
+ const sliced = token.slice(textStartingMatch.length);
4136
+ if (sliced !== '') {
4137
+ acc.push(sliced);
4138
+ }
4139
+ }
4140
+ return acc;
4141
+ }, []);
4142
+ const leafPatterns = match.pattern.getNextPatterns();
4143
+ const leafTokens = leafPatterns.reduce((acc, leafPattern) => {
4144
+ acc.push(...this._getTokensForPattern(leafPattern));
4132
4145
  return acc;
4133
4146
  }, []);
4134
- return this._createSuggestions(match.node.lastIndex, tokens);
4147
+ const allTokens = [...trailingTokens, ...leafTokens];
4148
+ return this._createSuggestions(match.node.lastIndex, allTokens);
4135
4149
  }
4136
4150
  else {
4137
4151
  return [];
@@ -4141,44 +4155,54 @@ class AutoComplete {
4141
4155
  const augmentedTokens = this._getAugmentedTokens(pattern);
4142
4156
  if (this._options.greedyPatternNames != null && this._options.greedyPatternNames.includes(pattern.name)) {
4143
4157
  const nextPatterns = pattern.getNextPatterns();
4144
- const tokens = [];
4145
4158
  const nextPatternTokens = nextPatterns.reduce((acc, pattern) => {
4146
4159
  acc.push(...this._getTokensForPattern(pattern));
4147
4160
  return acc;
4148
4161
  }, []);
4149
- for (let token of augmentedTokens) {
4150
- for (let nextPatternToken of nextPatternTokens) {
4151
- tokens.push(token + nextPatternToken);
4162
+ // using set to prevent duplicates
4163
+ const tokens = new Set();
4164
+ for (const token of augmentedTokens) {
4165
+ for (const nextPatternToken of nextPatternTokens) {
4166
+ tokens.add(token + nextPatternToken);
4152
4167
  }
4153
4168
  }
4154
- return tokens;
4169
+ return [...tokens];
4155
4170
  }
4156
4171
  else {
4157
4172
  return augmentedTokens;
4158
4173
  }
4159
4174
  }
4160
4175
  _getAugmentedTokens(pattern) {
4176
+ var _a, _b;
4161
4177
  const customTokensMap = this._options.customTokens || {};
4162
4178
  const leafPatterns = pattern.getPatterns();
4163
- const tokens = customTokensMap[pattern.name] || [];
4164
- leafPatterns.forEach(p => {
4165
- const augmentedTokens = customTokensMap[p.name] || [];
4166
- tokens.push(...p.getTokens(), ...augmentedTokens);
4167
- });
4168
- return tokens;
4179
+ /** Using Set to
4180
+ * - prevent duplicates
4181
+ * - prevent mutation of original customTokensMap
4182
+ */
4183
+ const customTokensForPattern = new Set((_a = customTokensMap[pattern.name]) !== null && _a !== void 0 ? _a : []);
4184
+ for (const lp of leafPatterns) {
4185
+ const augmentedTokens = (_b = customTokensMap[lp.name]) !== null && _b !== void 0 ? _b : [];
4186
+ const lpsCombinedTokens = [...lp.getTokens(), ...augmentedTokens];
4187
+ for (const token of lpsCombinedTokens) {
4188
+ customTokensForPattern.add(token);
4189
+ }
4190
+ }
4191
+ return [...customTokensForPattern];
4169
4192
  }
4170
4193
  _createSuggestions(lastIndex, tokens) {
4171
- let substring = lastIndex === -1 ? "" : this._cursor.getChars(0, lastIndex);
4194
+ let textToIndex = lastIndex === -1 ? "" : this._cursor.getChars(0, lastIndex);
4172
4195
  const suggestionStrings = [];
4173
4196
  const options = [];
4174
4197
  for (const token of tokens) {
4175
- const suggestion = substring + token;
4176
- const startsWith = suggestion.startsWith(substring);
4198
+ // concatenated for start index identification inside createSuggestion
4199
+ const suggestion = textToIndex + token;
4177
4200
  const alreadyExist = suggestionStrings.includes(suggestion);
4178
4201
  const isSameAsText = suggestion === this._text;
4179
- if (startsWith && !alreadyExist && !isSameAsText) {
4202
+ if (!alreadyExist && !isSameAsText) {
4180
4203
  suggestionStrings.push(suggestion);
4181
- options.push(this._createSuggestion(this._cursor.text, suggestion));
4204
+ const suggestionOption = this._createSuggestion(this._cursor.text, suggestion);
4205
+ options.push(suggestionOption);
4182
4206
  }
4183
4207
  }
4184
4208
  const reducedOptions = getFurthestOptions(options);
@@ -4188,10 +4212,11 @@ class AutoComplete {
4188
4212
  _createSuggestion(fullText, suggestion) {
4189
4213
  const furthestMatch = findMatchIndex(suggestion, fullText);
4190
4214
  const text = suggestion.slice(furthestMatch);
4191
- return {
4215
+ const option = {
4192
4216
  text: text,
4193
4217
  startIndex: furthestMatch,
4194
4218
  };
4219
+ return option;
4195
4220
  }
4196
4221
  static suggestFor(text, pattern, options) {
4197
4222
  return new AutoComplete(pattern, options).suggestFor(text);