Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5a689ce7 authored by Raph Levien's avatar Raph Levien
Browse files

Remove auto spaceifying behavior on paste

The paste action contained some logic to automatically insert or remove
spaces, basically to emulate the semantics of the text being a sequence
of space-separated words. This was never universally loved, and gives
particularly unexpected behavior with a hardware keyboard.

This patch simply removes the logic to automatically adjust spaces on
paste, so what gets pasted is simply the literal buffer contents.

Bug: 16900326
Bug: 3304489
Change-Id: I5cdf9c43d98a17aa708883b47245aa7929a124d2
parent a740b2bf
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -1860,9 +1860,8 @@ public class Editor {
        }

        final int originalLength = mTextView.getText().length();
        long minMax = mTextView.prepareSpacesAroundPaste(offset, offset, content);
        int min = TextUtils.unpackRangeStartFromLong(minMax);
        int max = TextUtils.unpackRangeEndFromLong(minMax);
        int min = offset;
        int max = offset;

        Selection.setSelection((Spannable) mTextView.getText(), max);
        mTextView.replaceText_internal(min, max, content);
+0 −54
Original line number Diff line number Diff line
@@ -8726,57 +8726,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        return length > 0;
    }

    /**
     * Prepare text so that there are not zero or two spaces at beginning and end of region defined
     * by [min, max] when replacing this region by paste.
     * Note that if there were two spaces (or more) at that position before, they are kept. We just
     * make sure we do not add an extra one from the paste content.
     */
    long prepareSpacesAroundPaste(int min, int max, CharSequence paste) {
        if (paste.length() > 0) {
            if (min > 0) {
                final char charBefore = mTransformed.charAt(min - 1);
                final char charAfter = paste.charAt(0);

                if (Character.isSpaceChar(charBefore) && Character.isSpaceChar(charAfter)) {
                    // Two spaces at beginning of paste: remove one
                    final int originalLength = mText.length();
                    deleteText_internal(min - 1, min);
                    // Due to filters, there is no guarantee that exactly one character was
                    // removed: count instead.
                    final int delta = mText.length() - originalLength;
                    min += delta;
                    max += delta;
                } else if (!Character.isSpaceChar(charBefore) && charBefore != '\n' &&
                        !Character.isSpaceChar(charAfter) && charAfter != '\n') {
                    // No space at beginning of paste: add one
                    final int originalLength = mText.length();
                    replaceText_internal(min, min, " ");
                    // Taking possible filters into account as above.
                    final int delta = mText.length() - originalLength;
                    min += delta;
                    max += delta;
                }
            }

            if (max < mText.length()) {
                final char charBefore = paste.charAt(paste.length() - 1);
                final char charAfter = mTransformed.charAt(max);

                if (Character.isSpaceChar(charBefore) && Character.isSpaceChar(charAfter)) {
                    // Two spaces at end of paste: remove one
                    deleteText_internal(max, max + 1);
                } else if (!Character.isSpaceChar(charBefore) && charBefore != '\n' &&
                        !Character.isSpaceChar(charAfter) && charAfter != '\n') {
                    // No space at end of paste: add one
                    replaceText_internal(max, max, " ");
                }
            }
        }

        return TextUtils.packRangeInLong(min, max);
    }

    /**
     * Paste clipboard content between min and max positions.
     */
@@ -8790,9 +8739,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
                CharSequence paste = clip.getItemAt(i).coerceToStyledText(getContext());
                if (paste != null) {
                    if (!didFirst) {
                        long minMax = prepareSpacesAroundPaste(min, max, paste);
                        min = TextUtils.unpackRangeStartFromLong(minMax);
                        max = TextUtils.unpackRangeEndFromLong(minMax);
                        Selection.setSelection((Spannable) mText, max);
                        ((Editable) mText).replace(min, max, paste);
                        didFirst = true;