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).

sclass StructureStringIndenter {
  // how many levels to indent, max
  settable int levels = 100;
  
  // inline lowest levels of char count is not higher than this
  settable int inlineChars = 40;
  
  settable bool verbose;
  
  // internal
  LS tok;
  Map<Int> bracketMap;
  
  S get(S s) {
    if (s == null) null;
    tok = javaTokForStructure(s);
    int n = l(tok);
    bracketMap = getBracketMap(tok, l1 isOpeningBracket, l1 isClosingBracket);
    
    // being careful because this.levels might be Int.MAX_VALUE
    int levels = clampToInt(this.levels*2L);
    
    new StringBuilder buf;
    int indent = 0;
    for i to n: {
      S t = tok.get(i);
      if (isOpeningBracket(t)) {
        S prev = _get(tok, i-2);
        
        Int j = or(bracketMap.get(i), n);
        if (j != null &&
          (eq(prev, "intarray")
          || !tokenRangeLongerThanNChars(tok, i+1, j+1, inlineChars))) {
          // ONE LINE
          
          buf.append(joinSubList(tok, i, j+1));
          i = j;
        } else {
          // MULTIPLE LINES
          
          if (verbose)
            print("Bracket part longer than " + inlineChars + " chars: " + quote(shortenJoinSubList(inlineChars, tok, i, j+1)));
          indent += 2;
          buf.append(t);
          if (indent <= levels)
            buf.append("\n").append(spaces(indent));
        }
      } else if (isClosingBracket(t)) {
        indent -= 2;
        if (indent < levels)
          buf.append("\n").append(spaces(indent));
        buf.append(t);
      } else if (indent <= levels && eq(t, ",")) {
        buf.append(t).append("\n").append(spaces(indent));
        i++;
      } else
        buf.append(t);
    }
    
    ret str(buf);
  }
}

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: 74 / 155
Version history: 9 change(s)
Referenced in: [show references]