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

47
LINES

< > BotCompany Repo | #1031306 // tok_moveImportsUp using reTok_multi (optimized, but broken?)

JavaX fragment (include)

1  
static LS tok_moveImportsUp(L<S> tok) {
2  
  new StringBuilder buf;
3  
  
4  
  //print("tok_moveImportsUp n=" + l(tok));
5  
  //print("Source begins: " + quote(shorten(join(tok), 200)));
6  
  
7  
  // scan imports on top
8  
  int i;
9  
  new Set<S> have;
10  
  for (i = 1; i < l(tok); i += 2)
11  
    if (eq(tok.get(i), 'import)) {
12  
      int j = indexOf(tok, i+2, ";");
13  
      if (j < 0) break;
14  
      S s = joinSubList(tok, i, j+1);
15  
      have.add(s);
16  
      i = j;
17  
    } else break;
18  
  
19  
  //print("tok_moveImportsUp have " + l(have) + " after " + i);
20  
  
21  
  new L<IntRange> reToks;
22  
  
23  
  // scan imports in text
24  
  for (int i2 : cloneList(indicesOf(tok, "import")))
25  
    if (i2 >= i) {
26  
      int j = indexOf(tok, i2+2, ";");
27  
      if (j < 0) continue; // huh?
28  
      S s = joinSubList(tok, i2, j+1);
29  
      //print("Found import at " + i2 + ": " + s);
30  
      if (!have.contains(s)) {
31  
        buf.append(s).append("\n");
32  
        have.add(s);
33  
      }
34  
      replaceTokens(tok, i, j+1, "");
35  
      ifndef tok_moveImportsUp_skipMostReToks
36  
        reToks.add(intRange(i2, j+1));
37  
      endifndef
38  
    }
39  
    
40  
  if (nempty(buf)) {
41  
    if (l(tok) == 1) addAll(tok, "", "");
42  
    tok.set(1, str(buf)+"\n"+tok.get(1));
43  
    reToks.add(intRange(1, 2));
44  
  }
45  
    
46  
  ret reTok_multi(tok, reToks);
47  
}

Author comment

Began life as a copy of #1012025

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031306
Snippet name: tok_moveImportsUp using reTok_multi (optimized, but broken?)
Eternal ID of this version: #1031306/1
Text MD5: 8507609aee9c729389d8a21efc72909d
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-04 21:19:30
Source code size: 1282 bytes / 47 lines
Pitched / IR pitched: No / No
Views / Downloads: 161 / 184
Referenced in: [show references]