Make space-indented blocks extend across newlines and empty lines.

Currently, if you have space indented blocks and they are separated by
an empty or whitespace only line, it becomes two separate pre-blocks.
Ideally these are one pre-block.

Before: https://imgur.com/a/G6Hdzah
After: https://imgur.com/a/KBIcQ6V

Release-Notes: skip
Google-Bug-Id: b/229774311
Change-Id: I5b4dd8699d13fb0eb30b2b271d4a964468fb229c
diff --git a/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts b/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts
index ae6f001..52b2e1b 100644
--- a/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts
+++ b/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts
@@ -224,13 +224,7 @@
         });
         i = endOfQuote - 1;
       } else if (this.isPreFormat(lines[i])) {
-        // include pre or all regular lines but stop at next new line
-        const predicate = (line: string) =>
-          this.isPreFormat(line) ||
-          (this.isRegularLine(line) &&
-            !this.isWhitespaceLine(line) &&
-            line.length > 0);
-        const endOfPre = this.getEndOfSection(lines, i + 1, predicate);
+        const endOfPre = this.findEndOfPreBlock(lines, i);
         result.push({
           type: 'pre',
           text: lines.slice(i, endOfPre).join('\n'),
@@ -294,6 +288,19 @@
     return index === -1 ? lines.length : index + startIndex;
   }
 
+  private findEndOfPreBlock(lines: string[], startIndex: number) {
+    let lastPreFormat = startIndex;
+    for (let i = startIndex + 1; i < lines.length; ++i) {
+      const line = lines[i];
+      if (this.isPreFormat(line)) {
+        lastPreFormat = i;
+      } else if (!this.isWhitespaceLine(line) && line.length !== 0) {
+        break;
+      }
+    }
+    return lastPreFormat + 1;
+  }
+
   /**
    * Take a block of comment text that contains a list, generate appropriate
    * block objects and append them to the output list.
diff --git a/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text_test.ts b/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text_test.ts
index 80c36ee..30aba38 100644
--- a/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text_test.ts
+++ b/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text_test.ts
@@ -412,6 +412,14 @@
     assertSimpleTextBlock(result[1], ' \nB');
   });
 
+  test('pre format 6', () => {
+    const comment = '  Q\n    <R>\n\n  S\n \nB';
+    const result = element._computeBlocks(comment);
+    assert.lengthOf(result, 2);
+    assertPreBlock(result[0], '  Q\n    <R>\n\n  S');
+    assertSimpleTextBlock(result[1], ' \nB');
+  });
+
   test('quote 1', () => {
     const comment = "> I'm happy with quotes!!";
     const result = element._computeBlocks(comment);