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

121
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();
    
    if (scaffoldingEnabled(this))
      printVars LeftArrowScriptAutoCompleter(+beginningOfCmd(), +prev, +cursor, +typedCharacters, globalClassNames := l(parser.globalClassNames()));
      
    // Get type hook (what type is left of cursor?)
    
    LASValueDescriptor typeHook = typeHooks.get(cursor.iTok);
    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() {
    ret concatMap methodNames(parser.functionContainers);
  }
  
  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/63
Text MD5: 64866e6bb2c2519bfb723e7d1a55cfbf
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-09-21 16:31:09
Source code size: 3674 bytes / 121 lines
Pitched / IR pitched: No / No
Views / Downloads: 155 / 439
Version history: 62 change(s)
Referenced in: [show references]