Libraryless. Click here for Pure Java version (4989L/28K).
1 | sS tok_moveImportsUp(S src) { |
2 | ret join(tok_moveImportsUp(javaTok(src))); |
3 | } |
4 | |
5 | static LS tok_moveImportsUp(LS tok) { |
6 | new StringBuilder buf; |
7 | |
8 | //print("tok_moveImportsUp n=" + l(tok)); |
9 | //print("Source begins: " + quote(shorten(join(tok), 200))); |
10 | |
11 | // scan imports on top |
12 | bool change = false; |
13 | int i; |
14 | new Set<S> have; |
15 | for (i = 1; i < l(tok); i += 2) |
16 | if (eq(tok.get(i), 'import)) { |
17 | int j = indexOf(tok, i+2, ";"); |
18 | if (j < 0) break; |
19 | S s = joinSubList(tok, i, j+1); |
20 | have.add(s); |
21 | i = j; |
22 | } else break; |
23 | |
24 | //print("tok_moveImportsUp have " + l(have) + " after " + i); |
25 | |
26 | // scan imports in text |
27 | for (; i < l(tok); i += 2) |
28 | if (eq(tok.get(i), 'import)) { |
29 | int j = indexOf(tok, i+2, ";"); |
30 | if (j < 0) continue; // huh? |
31 | S s = joinSubList(tok, i, j+1); |
32 | //print("Found import at " + i + ": " + s); |
33 | if (!have.contains(s)) { |
34 | buf.append(s).append("\n"); |
35 | have.add(s); |
36 | } |
37 | replaceTokens/*_reTok*/(tok, i, j+1, ""); |
38 | change = true; |
39 | i -= 2; |
40 | } |
41 | |
42 | if (nempty(buf)) { |
43 | if (l(tok) == 1) addAll(tok, "", ""); |
44 | tok.set(1, str(buf)+"\n"+tok.get(1)); |
45 | change = true; |
46 | //reTok(tok, 1, 2); |
47 | } |
48 | |
49 | if (change) reTok(tok); |
50 | ret tok; |
51 | } |
Began life as a copy of #1000608
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1009579 |
Snippet name: | tok_moveImportsUp - proper version using tokenizer |
Eternal ID of this version: | #1009579/22 |
Text MD5: | 1aeb13902e9783066b41af0413e2ce95 |
Transpilation MD5: | 13fb95e8a8503f5a1baed9f5dc1ed332 |
Author: | stefan |
Category: | |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-01-20 15:28:27 |
Source code size: | 1305 bytes / 51 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 450 / 523 |
Version history: | 21 change(s) |
Referenced in: | [show references] |