// don't reuse, create a new instance for each completion srecord noeq LeftArrowScriptAutoCompleter(GazelleV_LeftArrowScriptParser parser) > Meta { LS tok; CharInToken cursor; S typedCharacters; ScoredStringSearcher searcher; settable G22Utils g22utils; new TreeMap> knownVarsByTokenIdx; new Map typeHooks; event adaptingSearcher; *(G22Utils *g22utils, GazelleV_LeftArrowScriptParser *parser) {} void seekEnd(S text) { seek(text, l(text)); } void seek(S text, int iChar) { if (tok != null) fail("Don't reuse instance"); parser.setText(text); parser.init(); tok = parser.tok; cursor = charIndexToCharInToken(tok, iChar); // pointing to space? move left to code token if (even(cursor.iTok) && cursor.iTok > 0 && cursor.iChar == 0) { cursor.iTok--; cursor.iChar = l(token()); } // move one token left if necessary if (cursor.iTok > 1 && cursor.iChar == 0 && empty(space())) { cursor.iTok -= 2; cursor.iChar = l(token()); } typedCharacters = takeFirst(cursor.token(), cursor.iChar); // Attempt a (partial) parse of the document // to find out which variables are available where parser.onKnownVarsSnapshot(knownVars -> knownVarsByTokenIdx.put(parser.tokIdx(), cloneKeys(knownVars))); parser.onTypeHook(type -> typeHooks.put(parser.tokIdx(), type)); pcall-short { parser.parse(); } } S typedCharacters() { ret typedCharacters; } S space() { ret get(tok, cursor.iTok-1); } S token() { ret get(tok, cursor.iTok); } S prevToken() { ret get(tok, cursor.iTok-2); } bool beginningOfCmd() { ret eqOneOf(prevToken(), ";", "{", "}") || containsNewLine(space()); } Cl getCompletions() { //ret searcher()!; ret searcher().get_transformListWithinScore(l1 stringsSortedByLength); } ScoredStringSearcher searcher() { searcher = new ScoredStringSearcher(typedCharacters); searcher.uniquify(true); if (empty(typedCharacters)) searcher.returnAll = true; S prev = prevToken(); // Get type hook (what type is left of cursor?) LASValueDescriptor typeHook = typeHooks.get(cursor.iTok); if (scaffoldingEnabled(this)) { pnl(+typeHooks); printVars LeftArrowScriptAutoCompleter(+beginningOfCmd(), +prev, +cursor, +typedCharacters, +typeHook, globalClassNames := l(parser.globalClassNames())); } if (typeHook != null && !beginningOfCmd()) { Class c = typeHook.javaClass(); if (c == null) c = O.class; addToSearcher(methodNames(c)); addToSearcher(fieldNames(c)); } else { // No type hook - add global stuff // add global methods if (beginningOfCmd() || !isIdentifier(prev) || eqOneOf(prev, "if", "else")) addToSearcher(globalMethodNames()); // add global classes addToSearcher(keys(parser.globalClassNames())); // add the predefined identifiers addToSearcher(predefinedExpressions()); // add locally known variables Cl knownVars = floorValue(knownVarsByTokenIdx, cursor.iTok); addToSearcher(knownVars); } adaptingSearcher(); ret searcher; } Cl predefinedExpressions() { ret ll("true", "false", "null"); } swappable Cl globalMethodNames() { new L names; for (fc : parser.functionContainers) addAll(names, methodNames(fc)); addAll(names, keys(parser.functionDefs)); ret names; } void addToSearcher(Iterable or S... l) { fOr (s : l) if (!eq(s, typedCharacters)) searcher.add(s); } }