@Test public void noTrailingNewline() { String input = "package com.example.whatever;\n\npublic class SomeClass {}"; String output = input + "\n"; assertEquals(output, Reformatter.fixup(input)); }
static String fixup(String s) { StringBuilder out = new StringBuilder(); JavaScanner scanner = new JavaScanner(s); s = scanner.string(); int len = s.length(); for (int start = 0, previous = 0, braces = 0, parens = 0, end = 0; start < len; previous = start, start = end) { end = scanner.tokenEnd(start); switch (s.charAt(start)) { case '(': parens++; break; case ')': parens--; break; case '{': braces++; break; case '}': braces--; break; case ' ': if (s.charAt(previous) != '(' && "\n.,;)".indexOf(s.charAt(end)) < 0) { out.append(' '); } continue; case '\n': if (end < len && s.charAt(end) != '\n') { if (out.length() == 0) { start++; } break; } if (parens == 0 && braces < 2 && s.charAt(previous) != '\n' && out.length() > 0) { out.append('\n'); } continue; default: break; } out.append(s, start, end); } return out.toString(); }
Reformatter { static String fixup(String s) { StringBuilder out = new StringBuilder(); JavaScanner scanner = new JavaScanner(s); s = scanner.string(); int len = s.length(); for (int start = 0, previous = 0, braces = 0, parens = 0, end = 0; start < len; previous = start, start = end) { end = scanner.tokenEnd(start); switch (s.charAt(start)) { case '(': parens++; break; case ')': parens--; break; case '{': braces++; break; case '}': braces--; break; case ' ': if (s.charAt(previous) != '(' && "\n.,;)".indexOf(s.charAt(end)) < 0) { out.append(' '); } continue; case '\n': if (end < len && s.charAt(end) != '\n') { if (out.length() == 0) { start++; } break; } if (parens == 0 && braces < 2 && s.charAt(previous) != '\n' && out.length() > 0) { out.append('\n'); } continue; default: break; } out.append(s, start, end); } return out.toString(); } }
Reformatter { static String fixup(String s) { StringBuilder out = new StringBuilder(); JavaScanner scanner = new JavaScanner(s); s = scanner.string(); int len = s.length(); for (int start = 0, previous = 0, braces = 0, parens = 0, end = 0; start < len; previous = start, start = end) { end = scanner.tokenEnd(start); switch (s.charAt(start)) { case '(': parens++; break; case ')': parens--; break; case '{': braces++; break; case '}': braces--; break; case ' ': if (s.charAt(previous) != '(' && "\n.,;)".indexOf(s.charAt(end)) < 0) { out.append(' '); } continue; case '\n': if (end < len && s.charAt(end) != '\n') { if (out.length() == 0) { start++; } break; } if (parens == 0 && braces < 2 && s.charAt(previous) != '\n' && out.length() > 0) { out.append('\n'); } continue; default: break; } out.append(s, start, end); } return out.toString(); }  }
Reformatter { static String fixup(String s) { StringBuilder out = new StringBuilder(); JavaScanner scanner = new JavaScanner(s); s = scanner.string(); int len = s.length(); for (int start = 0, previous = 0, braces = 0, parens = 0, end = 0; start < len; previous = start, start = end) { end = scanner.tokenEnd(start); switch (s.charAt(start)) { case '(': parens++; break; case ')': parens--; break; case '{': braces++; break; case '}': braces--; break; case ' ': if (s.charAt(previous) != '(' && "\n.,;)".indexOf(s.charAt(end)) < 0) { out.append(' '); } continue; case '\n': if (end < len && s.charAt(end) != '\n') { if (out.length() == 0) { start++; } break; } if (parens == 0 && braces < 2 && s.charAt(previous) != '\n' && out.length() > 0) { out.append('\n'); } continue; default: break; } out.append(s, start, end); } return out.toString(); }   }
Reformatter { static String fixup(String s) { StringBuilder out = new StringBuilder(); JavaScanner scanner = new JavaScanner(s); s = scanner.string(); int len = s.length(); for (int start = 0, previous = 0, braces = 0, parens = 0, end = 0; start < len; previous = start, start = end) { end = scanner.tokenEnd(start); switch (s.charAt(start)) { case '(': parens++; break; case ')': parens--; break; case '{': braces++; break; case '}': braces--; break; case ' ': if (s.charAt(previous) != '(' && "\n.,;)".indexOf(s.charAt(end)) < 0) { out.append(' '); } continue; case '\n': if (end < len && s.charAt(end) != '\n') { if (out.length() == 0) { start++; } break; } if (parens == 0 && braces < 2 && s.charAt(previous) != '\n' && out.length() > 0) { out.append('\n'); } continue; default: break; } out.append(s, start, end); } return out.toString(); }    }