Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

128
LINES

< > BotCompany Repo | #1034153 // LeftArrowScriptAutoCompleter [does a few cases]

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (62701L) is out of date.

// 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<Int, Cl<S>> knownVarsByTokenIdx;
  new Map<Int, LASValueDescriptor> 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<S> 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<S> knownVars = floorValue(knownVarsByTokenIdx, cursor.iTok);
      addToSearcher(knownVars);
    }
    
    adaptingSearcher();
    
    ret searcher;
  }
  
  Cl<S> predefinedExpressions() {
    ret ll("true", "false", "null");
  }

  swappable Cl<S> globalMethodNames() {
    new L<S> names;
    for (fc : parser.functionContainers)
      addAll(names, methodNames(fc));
    addAll(names, keys(parser.functionDefs));
    ret names;
  }
  
  void addToSearcher(Iterable<S> or S... l) {
    fOr (s : l)
      if (!eq(s, typedCharacters))
        searcher.add(s);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034153
Snippet name: LeftArrowScriptAutoCompleter [does a few cases]
Eternal ID of this version: #1034153/66
Text MD5: 4b15048a8d76e76737df4c4f410177c8
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-12-26 19:29:36
Source code size: 3828 bytes / 128 lines
Pitched / IR pitched: No / No
Views / Downloads: 308 / 651
Version history: 65 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)