/* input e.g.: if (a and b) bla; This will yield ("if (a\n and b)", " bla;") Note: doesn't support multi-line strings */ static LS lines_honoringBrackets(S text) { LS lines = lines(text); new LS out; int bracketLevel = 0; // how many open brackets int start = 0; // line where current block starts for i over lines: { LS tok = javaTok(lines.get(i)); for (int j = 1; j < l(tok); j += 2) { S t = tok.get(j); if (eqOneOf(t, "(", "{", "[")) ++bracketLevel; else if (eqOneOf(t, ")", "}", "]")) --bracketLevel; } if (bracketLevel == 0) { out.add(lines_rtrim(subList(lines, start, i+1))); start = i+1; } } addIfNempty(out, lines_rtrim(subList(lines, start))); ret out; }