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

32
LINES

< > BotCompany Repo | #1030721 // indentStructureString_firstLevels [OK]

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

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

1  
sS indentStructureString_firstLevels(int levelsToIndent, S s) {
2  
  if (s == null) null;
3  
  LS tok = javaTokForStructure(s);
4  
  new StringBuilder buf;
5  
  int level = 0;
6  
  for i over tok: {
7  
    S t = tok.get(i);
8  
    if (isOpeningBracket(t)) {
9  
      int j = i+5;
10  
      if (containsClosingBracket(subList(tok, i+2, i+5))) {
11  
        buf.append(joinSubList(tok, i, j));
12  
        i = j-1;
13  
      } else {
14  
        buf.append(t);
15  
        ++level;
16  
        if (level <= levelsToIndent)
17  
          buf.append("\n").append(spaces(level*2));
18  
      }
19  
    } else if (isClosingBracket(t)) {
20  
      if (level-- <= levelsToIndent)
21  
        buf.append("\n").append(spaces(level*2));
22  
      buf.append(t);
23  
    } else if (eq(t, ",")) {
24  
      buf.append(t);
25  
      if (level <= levelsToIndent)
26  
        buf.append("\n").append(spaces(level*2));
27  
      i++;
28  
    } else
29  
      buf.append(t);
30  
  }
31  
  ret str(buf);
32  
}

Author comment

Began life as a copy of #1021424

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1030721
Snippet name: indentStructureString_firstLevels [OK]
Eternal ID of this version: #1030721/5
Text MD5: 7900f8e0c16af903d54f76c20f900407
Transpilation MD5: 75519b16f3a11a482c12c327f7c00cdd
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-03-16 18:57:46
Source code size: 905 bytes / 32 lines
Pitched / IR pitched: No / No
Views / Downloads: 185 / 271
Version history: 4 change(s)
Referenced in: [show references]