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