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.

1  
// don't reuse, create a new instance for each completion
2  
srecord noeq LeftArrowScriptAutoCompleter(GazelleV_LeftArrowScriptParser parser) > Meta {
3  
  LS tok;
4  
  CharInToken cursor;
5  
  S typedCharacters;
6  
  ScoredStringSearcher searcher;
7  
  settable G22Utils g22utils;
8  
  new TreeMap<Int, Cl<S>> knownVarsByTokenIdx;
9  
  new Map<Int, LASValueDescriptor> typeHooks;
10  
  
11  
  event adaptingSearcher;
12  
  
13  
  *(G22Utils *g22utils, GazelleV_LeftArrowScriptParser *parser) {}
14  
15  
  void seekEnd(S text) { seek(text, l(text)); }
16  
  
17  
  void seek(S text, int iChar) {
18  
    if (tok != null) fail("Don't reuse instance");
19  
    
20  
    parser.setText(text);
21  
    parser.init();
22  
    tok = parser.tok;
23  
    cursor = charIndexToCharInToken(tok, iChar);
24  
25  
    // pointing to space? move left to code token    
26  
    if (even(cursor.iTok) && cursor.iTok > 0 && cursor.iChar == 0) {
27  
      cursor.iTok--;
28  
      cursor.iChar = l(token());
29  
    }
30  
31  
    // move one token left if necessary
32  
    if (cursor.iTok > 1 && cursor.iChar == 0 && empty(space())) {
33  
      cursor.iTok -= 2;
34  
      cursor.iChar = l(token());
35  
    }
36  
    
37  
    typedCharacters = takeFirst(cursor.token(), cursor.iChar);
38  
    
39  
    // Attempt a (partial) parse of the document
40  
    // to find out which variables are available where
41  
    
42  
    parser.onKnownVarsSnapshot(knownVars ->
43  
      knownVarsByTokenIdx.put(parser.tokIdx(), cloneKeys(knownVars)));
44  
    parser.onTypeHook(type -> typeHooks.put(parser.tokIdx(), type));
45  
      
46  
    pcall-short { parser.parse(); }
47  
  }
48  
  
49  
  S typedCharacters() {
50  
    ret typedCharacters;
51  
  }
52  
  
53  
  S space() { ret get(tok, cursor.iTok-1); }
54  
  S token() { ret get(tok, cursor.iTok); }
55  
  S prevToken() { ret get(tok, cursor.iTok-2); }
56  
  
57  
  bool beginningOfCmd() {
58  
    ret eqOneOf(prevToken(), ";", "{", "}")
59  
      || containsNewLine(space());
60  
  }
61  
62  
  Cl<S> getCompletions() {
63  
    //ret searcher()!;
64  
    ret searcher().get_transformListWithinScore(l1 stringsSortedByLength);
65  
  }
66  
  
67  
  ScoredStringSearcher searcher() {
68  
    searcher = new ScoredStringSearcher(typedCharacters);
69  
    searcher.uniquify(true);
70  
    if (empty(typedCharacters)) searcher.returnAll = true;
71  
    
72  
    S prev = prevToken();
73  
    
74  
    // Get type hook (what type is left of cursor?)
75  
    
76  
    LASValueDescriptor typeHook = typeHooks.get(cursor.iTok);
77  
    
78  
    if (scaffoldingEnabled(this)) {
79  
      pnl(+typeHooks);
80  
      printVars LeftArrowScriptAutoCompleter(+beginningOfCmd(), +prev, +cursor, +typedCharacters, +typeHook, globalClassNames := l(parser.globalClassNames()));
81  
    }
82  
    
83  
    if (typeHook != null && !beginningOfCmd()) {
84  
      Class c = typeHook.javaClass();
85  
      if (c == null) c = O.class;
86  
      addToSearcher(methodNames(c));
87  
      addToSearcher(fieldNames(c));
88  
    } else {
89  
      // No type hook - add global stuff
90  
      
91  
      // add global methods
92  
      if (beginningOfCmd() || !isIdentifier(prev) || eqOneOf(prev, "if", "else"))
93  
        addToSearcher(globalMethodNames());
94  
  
95  
      // add global classes
96  
      addToSearcher(keys(parser.globalClassNames()));
97  
      
98  
      // add the predefined identifiers
99  
      addToSearcher(predefinedExpressions());
100  
      
101  
      // add locally known variables
102  
      Cl<S> knownVars = floorValue(knownVarsByTokenIdx, cursor.iTok);
103  
      addToSearcher(knownVars);
104  
    }
105  
    
106  
    adaptingSearcher();
107  
    
108  
    ret searcher;
109  
  }
110  
  
111  
  Cl<S> predefinedExpressions() {
112  
    ret ll("true", "false", "null");
113  
  }
114  
115  
  swappable Cl<S> globalMethodNames() {
116  
    new L<S> names;
117  
    for (fc : parser.functionContainers)
118  
      addAll(names, methodNames(fc));
119  
    addAll(names, keys(parser.functionDefs));
120  
    ret names;
121  
  }
122  
  
123  
  void addToSearcher(Iterable<S> or S... l) {
124  
    fOr (s : l)
125  
      if (!eq(s, typedCharacters))
126  
        searcher.add(s);
127  
  }
128  
}

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: 307 / 651
Version history: 65 change(s)
Referenced in: [show references]