Fix partial formatting of lines with trailing comments

Previously the replacements ended at the beginning of any trailing
whitespace/comments, which caused whitespace/comments to get pushed to the next
line.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=95548588
diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaOutput.java b/core/src/main/java/com/google/googlejavaformat/java/JavaOutput.java
index 89d9cdb..aa7d683 100644
--- a/core/src/main/java/com/google/googlejavaformat/java/JavaOutput.java
+++ b/core/src/main/java/com/google/googlejavaformat/java/JavaOutput.java
@@ -290,36 +290,20 @@
         replacement.append('\n');
       }
 
-      boolean first = true;
       for (int i = kToJ.get(startTok.getIndex()).lowerEndpoint();
           i < kToJ.get(endTok.getIndex()).upperEndpoint();
           i++) {
-        if (!first) {
-          replacement.append('\n');
-        }
-        first = false;
         // It's possible to run out of output lines (e.g. if the input ended with
         // multiple trailing newlines).
         if (i < getLineCount()) {
-          replacement.append(getLine(i));
+          replacement.append(getLine(i)).append('\n');
         }
       }
 
-      // Insert a line break if the input doesn't already contain one at the end
-      // of the reformatted region.
-      boolean needsBreakAfter =
-          endTok.getPosition() + 1 < javaInput.getText().length()
-              && javaInput.getText().charAt(endTok.getPosition() + 1) != '\n';
-      if (needsBreakAfter) {
-        replacement.append('\n');
-      }
-
-      result.add(
-          Replacement.create(
-              Range.closedOpen(
-                  replaceFrom, 
-                  Math.min(endTok.getPosition() + 1, javaInput.getText().length())),
-              replacement.toString()));
+      int endpos = Math.min(
+          endTok.getPosition() + endTok.getText().length() + 1,
+          javaInput.getText().length());
+      result.add(Replacement.create(Range.closedOpen(replaceFrom, endpos), replacement.toString()));
     }
 
     return result.build();
diff --git a/core/src/test/java/com/google/googlejavaformat/java/PartialFormattingTest.java b/core/src/test/java/com/google/googlejavaformat/java/PartialFormattingTest.java
index c24946c..9d29e6e 100644
--- a/core/src/test/java/com/google/googlejavaformat/java/PartialFormattingTest.java
+++ b/core/src/test/java/com/google/googlejavaformat/java/PartialFormattingTest.java
@@ -180,7 +180,7 @@
     String expectedOutput =
         "class Test { int xxx = 1;\n"
             + "  int yyy = 1;\n"
-            + " int zzz = 1; }";
+            + "int zzz = 1; }";
     int idx = input.indexOf("yyy");
     String output = doGetFormatReplacements(input, idx, idx + 1);
     assertEquals("bad output", expectedOutput, output);
@@ -192,7 +192,7 @@
     String expectedOutput =
         "class Test { int xxx = 1;\n\n"
             + "  int yyy = 1;\n"
-            + " int zzz = 1; }";
+            + "int zzz = 1; }";
     int idx = input.indexOf("yyy");
     String output = doGetFormatReplacements(input, idx, idx + 1);
     assertEquals("bad output", expectedOutput, output);
@@ -204,7 +204,7 @@
     String expectedOutput =
         "class Test { int xxx = 1;\n"
             + "  int yyy = 1;\n"
-            + "      int zzz = 1; }";
+            + "     int zzz = 1; }";
     int idx = input.indexOf("yyy");
     String output = doGetFormatReplacements(input, idx, idx + 1);
     assertEquals("bad output", expectedOutput, output);
@@ -216,7 +216,7 @@
     String expectedOutput =
         "class Test {\n"
             + "  void zzz() {\n"
-            + " int x; } }";
+            + "int x; } }";
     int idx = input.indexOf("zzz");
     String output = doGetFormatReplacements(input, idx, idx);
     assertEquals("bad output", expectedOutput, output);
@@ -228,7 +228,7 @@
     String expectedOutput =
         "class Test { void f() { return;\n"
             + "  }\n"
-            + " }\n";
+            + "}\n";
     int idx = input.indexOf("}");
     String output = doGetFormatReplacements(input, idx, idx);
     assertEquals("bad output", expectedOutput, output);
@@ -240,7 +240,7 @@
     String expectedOutput =
         "class Test {\n"
             + "  void f() {}\n"
-            + " }\n";
+            + "}\n";
     int idx = input.indexOf("}");
     String output = doGetFormatReplacements(input, idx, idx);
     assertEquals("bad output", expectedOutput, output);
@@ -534,8 +534,8 @@
         new Formatter().getFormatReplacements(input, ImmutableList.of(Range.closedOpen(18, 19)));
     assertThat(ranges).hasSize(1);
     Replacement replacement = ranges.get(0);
-    assertThat(replacement.getReplacementString()).isEqualTo("  void f() {}");
-    assertThat(replacement.getReplaceRange()).isEqualTo(Range.closedOpen(11, 24));
+    assertThat(replacement.getReplacementString()).isEqualTo("  void f() {}\n");
+    assertThat(replacement.getReplaceRange()).isEqualTo(Range.closedOpen(11, 25));
   }
 
   @Test
@@ -771,4 +771,28 @@
     assertThat(main.format(args)).isEqualTo(0);
     assertThat(out.toString()).isEqualTo(expectedOutput);
   }
+
+  @Test
+  public void lineWithTrailingComment() throws Exception {
+    String input =
+        "class Foo{\n"
+            + "int xxx; // asd\n"
+            + "}\n";
+    String expectedOutput =
+        "class Foo{\n"
+            + "  int xxx; // asd\n"
+            + "}\n";
+
+    Path tmpdir = testFolder.newFolder().toPath();
+    Path path = tmpdir.resolve("Foo.java");
+    Files.write(path, input.getBytes(StandardCharsets.UTF_8));
+
+    StringWriter out = new StringWriter();
+    StringWriter err = new StringWriter();
+
+    Main main = new Main(new PrintWriter(out, true), new PrintWriter(err, true));
+    String[] args = {"-lines", "2", path.toString()};
+    assertThat(main.format(args)).isEqualTo(0);
+    assertThat(out.toString()).isEqualTo(expectedOutput);
+  }
 }