preserve new lines when quoting. fixes #3876

Millesimus created

Change summary

src/main/java/eu/siacs/conversations/ui/util/QuoteHelper.java   | 8 +-
src/main/java/eu/siacs/conversations/ui/widget/EditMessage.java | 8 ++
src/main/java/eu/siacs/conversations/utils/MessageUtils.java    | 6 -
3 files changed, 12 insertions(+), 10 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/ui/util/QuoteHelper.java 🔗

@@ -84,13 +84,13 @@ public class QuoteHelper {
         if (isPositionQuoteStart(line, 0)) {
             int nestingDepth = 1;
             for (int i = 1; i < line.length(); i++) {
-                if (isPositionQuoteStart(line, i)) {
+                if (isPositionQuoteCharacter(line, i)) {
                     nestingDepth++;
-                }
-                if (nestingDepth > (Config.QUOTING_MAX_DEPTH - 1)) {
-                    return true;
+                } else if (line.charAt(i) != ' ') {
+                    break;
                 }
             }
+            return nestingDepth >= (Config.QUOTING_MAX_DEPTH);
         }
         return false;
     }

src/main/java/eu/siacs/conversations/ui/widget/EditMessage.java 🔗

@@ -145,7 +145,13 @@ public class EditMessage extends AppCompatEditText {
 
     public void insertAsQuote(String text) {
         text = QuoteHelper.replaceAltQuoteCharsInText(text);
-        text = text.replaceAll("(\n *){2,}", "\n").replaceAll("(^|\n)(" + QuoteHelper.QUOTE_CHAR + ")", "$1$2$2").replaceAll("(^|\n)([^" + QuoteHelper.QUOTE_CHAR + "])", "$1> $2").replaceAll("\n$", "");
+        text = text
+                // first replace all '>' at the beginning of the line with nice and tidy '>>'
+                // for nested quoting
+                .replaceAll("(^|\n)(" + QuoteHelper.QUOTE_CHAR + ")", "$1$2$2")
+                // then find all other lines and have them start with a '> '
+                .replaceAll("(^|\n)(?!" + QuoteHelper.QUOTE_CHAR + ")(.*)", "$1> $2")
+        ;
         Editable editable = getEditableText();
         int position = getSelectionEnd();
         if (position == -1) position = editable.length();

src/main/java/eu/siacs/conversations/utils/MessageUtils.java 🔗

@@ -66,11 +66,7 @@ public class MessageUtils {
             body = message.getMergedBody().toString();
         }
         for (String line : body.split("\n")) {
-            if (line.length() <= 0) {
-                continue;
-            }
-            final char c = line.charAt(0);
-            if (QuoteHelper.isNestedTooDeeply(line)) {
+            if (!(line.length() <= 0) && QuoteHelper.isNestedTooDeeply(line)) {
                 continue;
             }
             if (builder.length() != 0) {