@mojir/lits 2.1.19 → 2.1.20

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/lits.iife.js CHANGED
@@ -14862,18 +14862,25 @@ var Lits = (function (exports) {
14862
14862
  return Parser;
14863
14863
  }());
14864
14864
 
14865
- var autoCompleteTokenTypes = [
14866
- 'Operator',
14867
- 'ReservedSymbol',
14868
- 'Symbol',
14869
- ];
14870
14865
  var litsCommands = new Set(__spreadArray(__spreadArray([], __read(normalExpressionKeys), false), __read(specialExpressionKeys), false));
14871
14866
  // TODO: replace with get suggestions function
14872
14867
  var AutoCompleter = /** @class */ (function () {
14873
- function AutoCompleter(tokenStream, params) {
14874
- this.searchPrefix = '';
14868
+ function AutoCompleter(originalProgram, originalPosition, lits, params) {
14869
+ this.originalProgram = originalProgram;
14870
+ this.originalPosition = originalPosition;
14871
+ this.prefixProgram = '';
14872
+ this.suffixProgram = '';
14873
+ this.searchString = '';
14875
14874
  this.suggestions = [];
14876
14875
  this.suggestionIndex = null;
14876
+ var partialProgram = this.originalProgram.slice(0, this.originalPosition);
14877
+ var tokenStream = null;
14878
+ try {
14879
+ tokenStream = lits.tokenize(partialProgram);
14880
+ }
14881
+ catch (_a) {
14882
+ // do nothing
14883
+ }
14877
14884
  if (!tokenStream) {
14878
14885
  return;
14879
14886
  }
@@ -14881,14 +14888,28 @@ var Lits = (function (exports) {
14881
14888
  if (!lastToken) {
14882
14889
  return;
14883
14890
  }
14884
- var _a = __read(lastToken, 2), tokenType = _a[0], tokenValue = _a[1];
14885
- if (!autoCompleteTokenTypes.includes(tokenType)) {
14886
- return;
14887
- }
14888
- this.searchPrefix = tokenValue;
14889
- this.generateSuggestions(params);
14891
+ this.searchString = lastToken[1];
14892
+ this.prefixProgram = this.originalProgram.slice(0, this.originalPosition - this.searchString.length);
14893
+ this.suffixProgram = this.originalProgram.slice(this.prefixProgram.length + this.searchString.length);
14894
+ this.originalProgram.slice(this.prefixProgram.length + this.searchString.length);
14895
+ this.suggestions = this.generateSuggestions(params);
14890
14896
  }
14891
14897
  AutoCompleter.prototype.getNextSuggestion = function () {
14898
+ return this.getAutoCompleteSuggestionResult(this.getNextSuggestionSymbol());
14899
+ };
14900
+ AutoCompleter.prototype.getPreviousSuggestion = function () {
14901
+ return this.getAutoCompleteSuggestionResult(this.getPreviousSuggestionSymbol());
14902
+ };
14903
+ AutoCompleter.prototype.getAutoCompleteSuggestionResult = function (suggestion) {
14904
+ if (suggestion === null) {
14905
+ return null;
14906
+ }
14907
+ return {
14908
+ program: this.prefixProgram + suggestion + this.suffixProgram,
14909
+ position: this.prefixProgram.length + suggestion.length,
14910
+ };
14911
+ };
14912
+ AutoCompleter.prototype.getNextSuggestionSymbol = function () {
14892
14913
  if (this.suggestions.length === 0) {
14893
14914
  return null;
14894
14915
  }
@@ -14901,12 +14922,9 @@ var Lits = (function (exports) {
14901
14922
  this.suggestionIndex = 0;
14902
14923
  }
14903
14924
  }
14904
- return {
14905
- suggestion: this.suggestions[this.suggestionIndex],
14906
- searchPattern: this.searchPrefix,
14907
- };
14925
+ return this.suggestions[this.suggestionIndex];
14908
14926
  };
14909
- AutoCompleter.prototype.getPreviousSuggestion = function () {
14927
+ AutoCompleter.prototype.getPreviousSuggestionSymbol = function () {
14910
14928
  if (this.suggestions.length === 0) {
14911
14929
  return null;
14912
14930
  }
@@ -14919,41 +14937,38 @@ var Lits = (function (exports) {
14919
14937
  this.suggestionIndex = this.suggestions.length - 1;
14920
14938
  }
14921
14939
  }
14922
- return {
14923
- suggestion: this.suggestions[this.suggestionIndex],
14924
- searchPattern: this.searchPrefix,
14925
- };
14940
+ return this.suggestions[this.suggestionIndex];
14926
14941
  };
14927
14942
  AutoCompleter.prototype.getSuggestions = function () {
14928
14943
  return __spreadArray([], __read(this.suggestions), false);
14929
14944
  };
14930
- AutoCompleter.prototype.getSearchPrefix = function () {
14931
- return this.searchPrefix;
14945
+ AutoCompleter.prototype.getSearchString = function () {
14946
+ return this.searchString;
14932
14947
  };
14933
14948
  AutoCompleter.prototype.generateSuggestions = function (params) {
14934
14949
  var _this = this;
14935
14950
  var _a, _b, _c, _d;
14936
14951
  var suggestions = new Set();
14937
14952
  litsCommands.forEach(function (name) {
14938
- if (name.startsWith(_this.searchPrefix)) {
14953
+ if (name.startsWith(_this.searchString)) {
14939
14954
  suggestions.add(name);
14940
14955
  }
14941
14956
  });
14942
14957
  Object.keys((_a = params.globalContext) !== null && _a !== void 0 ? _a : {})
14943
- .filter(function (name) { return name.startsWith(_this.searchPrefix); })
14958
+ .filter(function (name) { return name.startsWith(_this.searchString); })
14944
14959
  .forEach(function (name) { return suggestions.add(name); });
14945
14960
  (_b = params.contexts) === null || _b === void 0 ? void 0 : _b.forEach(function (context) {
14946
14961
  Object.keys(context)
14947
- .filter(function (name) { return name.startsWith(_this.searchPrefix); })
14962
+ .filter(function (name) { return name.startsWith(_this.searchString); })
14948
14963
  .forEach(function (name) { return suggestions.add(name); });
14949
14964
  });
14950
14965
  Object.keys((_c = params.jsFunctions) !== null && _c !== void 0 ? _c : {})
14951
- .filter(function (name) { return name.startsWith(_this.searchPrefix); })
14966
+ .filter(function (name) { return name.startsWith(_this.searchString); })
14952
14967
  .forEach(function (name) { return suggestions.add(name); });
14953
14968
  Object.keys((_d = params.values) !== null && _d !== void 0 ? _d : {})
14954
- .filter(function (name) { return name.startsWith(_this.searchPrefix); })
14969
+ .filter(function (name) { return name.startsWith(_this.searchString); })
14955
14970
  .forEach(function (name) { return suggestions.add(name); });
14956
- this.suggestions = __spreadArray([], __read(suggestions), false).sort(function (a, b) { return a.localeCompare(b); });
14971
+ return __spreadArray([], __read(suggestions), false).sort(function (a, b) { return a.localeCompare(b); });
14957
14972
  };
14958
14973
  return AutoCompleter;
14959
14974
  }());
@@ -15133,15 +15148,9 @@ var Lits = (function (exports) {
15133
15148
  (_a = this.astCache) === null || _a === void 0 ? void 0 : _a.set(program, ast);
15134
15149
  return ast;
15135
15150
  };
15136
- Lits.prototype.getAutoCompleter = function (partialProgram, params) {
15151
+ Lits.prototype.getAutoCompleter = function (program, position, params) {
15137
15152
  if (params === void 0) { params = {}; }
15138
- try {
15139
- var tokenStream = this.tokenize(partialProgram);
15140
- return new AutoCompleter(tokenStream, params);
15141
- }
15142
- catch (_a) {
15143
- return new AutoCompleter(null, params);
15144
- }
15153
+ return new AutoCompleter(program, position, this, params);
15145
15154
  };
15146
15155
  return Lits;
15147
15156
  }());