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

62
LINES

< > BotCompany Repo | #1035335 // StructureStringIndenter - successor of indentStructureString

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

Libraryless. Click here for Pure Java version (9509L/53K).

1  
sclass StructureStringIndenter {
2  
  // how many levels to indent, max
3  
  settable int levels = 100;
4  
  
5  
  // inline lowest levels of char count is not higher than this
6  
  settable int inlineChars = 40;
7  
  
8  
  settable bool verbose;
9  
  
10  
  // internal
11  
  LS tok;
12  
  Map<Int> bracketMap;
13  
  
14  
  S get(S s) {
15  
    if (s == null) null;
16  
    tok = javaTokForStructure(s);
17  
    int n = l(tok);
18  
    bracketMap = getBracketMap(tok, l1 isOpeningBracket, l1 isClosingBracket);
19  
    
20  
    // being careful because this.levels might be Int.MAX_VALUE
21  
    int levels = clampToInt(this.levels*2L);
22  
    
23  
    new StringBuilder buf;
24  
    int indent = 0;
25  
    for i to n: {
26  
      S t = tok.get(i);
27  
      if (isOpeningBracket(t)) {
28  
        S prev = _get(tok, i-2);
29  
        
30  
        Int j = or(bracketMap.get(i), n);
31  
        if (j != null &&
32  
          (eq(prev, "intarray")
33  
          || !tokenRangeLongerThanNChars(tok, i+1, j+1, inlineChars))) {
34  
          // ONE LINE
35  
          
36  
          buf.append(joinSubList(tok, i, j+1));
37  
          i = j;
38  
        } else {
39  
          // MULTIPLE LINES
40  
          
41  
          if (verbose)
42  
            print("Bracket part longer than " + inlineChars + " chars: " + quote(shortenJoinSubList(inlineChars, tok, i, j+1)));
43  
          indent += 2;
44  
          buf.append(t);
45  
          if (indent <= levels)
46  
            buf.append("\n").append(spaces(indent));
47  
        }
48  
      } else if (isClosingBracket(t)) {
49  
        indent -= 2;
50  
        if (indent < levels)
51  
          buf.append("\n").append(spaces(indent));
52  
        buf.append(t);
53  
      } else if (indent <= levels && eq(t, ",")) {
54  
        buf.append(t).append("\n").append(spaces(indent));
55  
        i++;
56  
      } else
57  
        buf.append(t);
58  
    }
59  
    
60  
    ret str(buf);
61  
  }
62  
}

Author comment

Began life as a copy of #1021424

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035335
Snippet name: StructureStringIndenter - successor of indentStructureString
Eternal ID of this version: #1035335/10
Text MD5: 93c32cd3bf157922bda25a75c09d5829
Transpilation MD5: 0511bebd62f83201ca7a67ce68cb2722
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-21 06:12:02
Source code size: 1774 bytes / 62 lines
Pitched / IR pitched: No / No
Views / Downloads: 149 / 265
Version history: 9 change(s)
Referenced in: [show references]