[frameworks/ktexteditor] src: Add option to keep spaces to the left of cursor when saving

Jan Paul Batrina null at kde.org
Tue Mar 9 20:44:30 GMT 2021


Git commit 21c0431b7a04dceb4a7591b324e801a2f3aa6f95 by Jan Paul Batrina.
Committed on 09/03/2021 at 04:08.
Pushed by cullmann into branch 'master'.

Add option to keep spaces to the left of cursor when saving

BUG: 433455
GUI:

M  +5    -0    src/dialogs/opensaveconfigwidget.ui
M  +24   -5    src/document/katedocument.cpp
M  +6    -5    src/utils/kateconfig.h

https://invent.kde.org/frameworks/ktexteditor/commit/21c0431b7a04dceb4a7591b324e801a2f3aa6f95

diff --git a/src/dialogs/opensaveconfigwidget.ui b/src/dialogs/opensaveconfigwidget.ui
index 66f98496..ccd1e775 100644
--- a/src/dialogs/opensaveconfigwidget.ui
+++ b/src/dialogs/opensaveconfigwidget.ui
@@ -186,6 +186,11 @@
             <string>In Entire Document</string>
            </property>
           </item>
+          <item>
+           <property name="text">
+            <string>Except To The Left of Cursor Position</string>
+           </property>
+          </item>
          </widget>
         </item>
         <item>
diff --git a/src/document/katedocument.cpp b/src/document/katedocument.cpp
index 56e97a2e..dcf3a8c1 100644
--- a/src/document/katedocument.cpp
+++ b/src/document/katedocument.cpp
@@ -5073,14 +5073,33 @@ void KTextEditor::DocumentPrivate::removeTrailingSpaces()
 
     editStart();
 
+    int curLine = -1, curCol = -1;
+    if (m_activeView) {
+        curLine = m_activeView->cursorPosition().line();
+        curCol = m_activeView->cursorPosition().column();
+    }
+
     for (int line = 0; line < lines(); ++line) {
         Kate::TextLine textline = plainKateTextLine(line);
+        int p = -1;
+
+        const int lastChar = textline->lastChar();
+        if (remove == 3) {
+            if (line == curLine && curCol > lastChar) {
+                // remove trailing spaces except those to the left of current cursor postion, remove = 3
+                p = curCol + 1;
+            } else {
+                // remove all other tailing spaces from other lines
+                p = lastChar + 1;
+            }
+        } else if (remove == 2 || textline->markedAsModified() || textline->markedAsSavedOnDisk()) {
+            // remove trailing spaces in entire document, remove = 2
+            // remove trailing spaces of touched lines, remove = 1
+            // remove trailing spaces of lines saved on disk, remove = 1
+            p = lastChar + 1;
+        }
 
-        // remove trailing spaces in entire document, remove = 2
-        // remove trailing spaces of touched lines, remove = 1
-        // remove trailing spaces of lines saved on disk, remove = 1
-        if (remove == 2 || textline->markedAsModified() || textline->markedAsSavedOnDisk()) {
-            const int p = textline->lastChar() + 1;
+        if (p != -1) {
             const int l = textline->length() - p;
             if (l > 0) {
                 editRemoveText(line, p, l);
diff --git a/src/utils/kateconfig.h b/src/utils/kateconfig.h
index bedfbe78..28cd8d24 100644
--- a/src/utils/kateconfig.h
+++ b/src/utils/kateconfig.h
@@ -759,13 +759,14 @@ public:
 
     /**
      * Remove trailing spaces on save.
-     * triState = 0: never remove trailing spaces
-     * triState = 1: remove trailing spaces of modified lines (line modification system)
-     * triState = 2: remove trailing spaces in entire document
+     * removeMode = 0: never remove trailing spaces
+     * removeMode = 1: remove trailing spaces of modified lines (line modification system)
+     * removeMode = 2: remove trailing spaces in entire document
+     * removeMode = 3: remove trailing spaces except those to the left of cursor
      */
-    void setRemoveSpaces(int triState)
+    void setRemoveSpaces(int removeMode)
     {
-        setValue(RemoveSpacesMode, triState);
+        setValue(RemoveSpacesMode, removeMode);
     }
 
     int removeSpaces() const



More information about the kde-doc-english mailing list