[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kwrite-devel
Subject:    [kate] ktexteditor: KF5: Range normalization methods
From:       Dominik Haumann <dhaumann () kde ! org>
Date:       2013-11-05 13:36:37
Message-ID: E1Vdgo9-0003ut-Sq () scm ! kde ! org
[Download RAW message or body]

Git commit ef9dc288f865981cbb1b2bb64d175eea44f53a19 by Dominik Haumann.
Committed on 05/11/2013 at 13:36.
Pushed by dhaumann into branch 'master'.

KF5: Range normalization methods

This patch adds code, that is commented out, for normalizing a Range:
bool isNormalized(), void normalize(), Range normalized().

Usually, start() <= end() always holds. This is also guaranteed in the
constructor. However, currently, the API allows to set the end cursor by
calling range.end().setPosition(c), same for start(). So start() could
be > end() cursor.

In this case, normalizing the cursor makes sure that start <= end by
swapping end and start.

The other way is to change the API for KF5 such that we disallow setting
the start and end cursors by reference. Then, we'd have an API like
- Range::setStartPosition(c)
- Range::setEndPosition(c)
This way, the swapping would either be automatic in these functions, or
the cursor would simply not be set to c.

Any preferences?

CCMAIL: kwrite-devel@kde.org

M  +53   -0    ktexteditor/range.h

http://commits.kde.org/kate/ef9dc288f865981cbb1b2bb64d175eea44f53a19

diff --git a/ktexteditor/range.h b/ktexteditor/range.h
index 749bd6d..75802ee 100644
--- a/ktexteditor/range.h
+++ b/ktexteditor/range.h
@@ -276,6 +276,59 @@ class KTEXTEDITOR_EXPORT Range
      */
     bool isEmpty() const;
 
+#if 0
+    // TODO: KDE5 if we have consistency cecks, the following is not needed.
+    //       otherwise, if start could possibly be after end, then the following
+    //       is handy:
+    /**
+     * Check whether the range is normalized, i.e. if start() <= end() holds.
+     *
+     * \warning If the range is invalid, the return value is undefined.
+     *
+     * \returns \e true if start() <= end(), otherwise false.
+     * \see isValid(), normalize(), normalized()
+     */
+    inline bool isNormalized() const
+    { return m_start <= m_end; }
+
+    /**
+     * Normalize if needed. After calling normalize(), this range is guaranteed
+     * to be normalized, i.e. start <= end holds then.
+     *
+     * \warning If the range is invalid, this function does nothing.
+     *
+     * \see isValid(), isNormalized(), normalized()
+     */
+    inline void normalize()
+    {
+      if (isValid() && start > end) {
+        std::swap(m_start, m_end);
+      }
+    }
+
+    /**
+     * Returns a normalized version of this range.
+     * For valid ranges, a normalized range always guarantees start() <= end().
+     * If the range is invalid, KTextEditor::Range::invalid() is returned.
+     *
+     * \warning If the range is invalid, this function does nothing.
+     *
+     * \see isValid(), isNormalized(), normalize()
+     */
+    KTextEditor::Range normalize() const
+    {
+      if (isValid()) {
+        if (m_start <= m_end) {
+          return *this;
+        } else {
+          return Range(m_end, m_start);
+        }
+      } else {
+        return invalid();
+      }
+    }
+#endif
+
     //BEGIN comparison functions
     /**
      * \}
_______________________________________________
KWrite-Devel mailing list
KWrite-Devel@kde.org
https://mail.kde.org/mailman/listinfo/kwrite-devel
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic