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

48
LINES

< > BotCompany Repo | #1012025 - tok_moveImportsUp using reTok_multi (LIVE)

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

Libraryless. Click here for Pure Java version (2657L/17K).

static LS tok_moveImportsUp(L<S> tok) {
  new StringBuilder buf;
  
  //print("tok_moveImportsUp n=" + l(tok));
  //print("Source begins: " + quote(shorten(join(tok), 200)));
  
  // scan imports on top
  int i;
  new Set<S> have;
  for (i = 1; i < l(tok); i += 2)
    if (eq(tok.get(i), 'import)) {
      int j = indexOf(tok, i+2, ";");
      if (j < 0) break;
      S s = joinSubList(tok, i, j+1);
      have.add(s);
      i = j;
    } else break;
  
  //print("tok_moveImportsUp have " + l(have) + " after " + i);
  
  new L<IntRange> reToks;
  
  // scan imports in text
  for (; i < l(tok); i += 2)
    if (eq(tok.get(i), 'import)) {
      int j = indexOf(tok, i+2, ";");
      if (j < 0) continue; // huh?
      S s = joinSubList(tok, i, j+1);
      //print("Found import at " + i + ": " + s);
      if (!have.contains(s)) {
        buf.append(s).append("\n");
        have.add(s);
      }
      replaceTokens(tok, i, j+1, "");
      ifndef tok_moveImportsUp_skipMostReToks
        reToks.add(intRange(i, j+1));
      endifndef
      i -= 2;
    }
    
  if (nempty(buf)) {
    if (l(tok) == 1) addAll(tok, "", "");
    tok.set(1, str(buf)+"\n"+tok.get(1));
    reToks.add(intRange(1, 2));
  }
    
  ret reTok_multi(tok, reToks);
}

Author comment

Began life as a copy of #1009579

download  show line numbers  debug dex   

Travelled to 10 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #1012025
Snippet name: tok_moveImportsUp using reTok_multi (LIVE)
Eternal ID of this version: #1012025/7
Text MD5: a4e8b39d4db816b2e54237233175af1f
Transpilation MD5: 72e9df4d10cd40365ecea26b345fc6f7
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-11-08 01:25:05
Source code size: 1285 bytes / 48 lines
Pitched / IR pitched: No / No
Views / Downloads: 218 / 241
Version history: 6 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1012025 & http://1012025.tinybrain.de