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

List:       kwrite-devel
Subject:    Re: MovingCursor + Ranges, Features
From:       Michel Ludwig <michel.ludwig () gmail ! com>
Date:       2010-04-25 10:19:29
Message-ID: 201004251119.30209.michel.ludwig () gmail ! com
[Download RAW message or body]

Hi all,

On Thu 22 Apr 2010 08:11:13 Christoph Cullmann wrote:
> On Wednesday 21 April 2010 23:39:21 Michel Ludwig wrote: 
> > I think it would also be good to add some debugging output functions as
> > it is done in the attached patch (copied from range.h/cursor.h).
> > Moreover, some range comparisons like the following would be good to
> > have as well (eliminating the need to convert moving ranges to simple
> > ranges)
> > 
> > bool 	boundaryAtCursor (const Cursor &cursor) const
> > bool 	boundaryOnColumn (int column) const
> > bool 	boundaryOnLine (int line) const
> > bool 	contains (const Cursor &cursor) const
> > bool 	contains (const Range &range) const
> > bool 	containsColumn (int column) const
> > bool 	containsLine (int line) const
> > bool 	overlaps (const Range &range) const
> > bool 	overlapsColumn (int column) const
> > bool 	overlapsLine (int line) const
> > int 	positionRelativeToCursor (const Cursor &cursor) const
> > int 	positionRelativeToLine (int line) const
> 
> I am all for it, all this can be implemented in KTE as non-virtuals,
> Dominik came up with similar list, too, for comparison operators and so
> on.

Attached is a patch which adds some of these. Now, one could also add a second 
set which operate on MovingRanges.

In the patch the equality operator on MovingCursor is implemented directly. 
However, it would also be possible to define it as purely virtual method, but 
that's up for discussion :-)

Michel


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________
["additional-methods.patch" (text/x-patch)]

diff --git a/ktexteditor/movingcursor.h b/ktexteditor/movingcursor.h
index f4eb99c..6900f0c 100644
--- a/ktexteditor/movingcursor.h
+++ b/ktexteditor/movingcursor.h
@@ -138,6 +138,28 @@ class KTEXTEDITOR_EXPORT MovingCursor
      */
     virtual ~MovingCursor ();
 
+    /**
+     * Equality operator.
+     *
+     * \note comparison between two invalid cursors is undefined.
+     *       comparison between and invalid and a valid cursor will always be \e \
false. +     *
+     * \param c1 first cursor to compare
+     * \param c2 second cursor to compare
+     * \return \e true, if c1's and c2's line and column are \e equal.
+     */
+    inline friend bool operator==(const MovingCursor& c1, const MovingCursor& c2)
+      { return c1.line() == c2.line() && c1.column() == c2.column(); }
+
+    /**
+     * Inequality operator.
+     * \param c1 first cursor to compare
+     * \param c2 second cursor to compare
+     * \return \e true, if c1's and c2's line and column are \e not equal.
+     */
+    inline friend bool operator!=(const MovingCursor& c1, const MovingCursor& c2)
+      { return !(c1 == c2); }
+    
   //
   // forbidden stuff
   //
diff --git a/ktexteditor/movingrange.cpp b/ktexteditor/movingrange.cpp
index ee0d873..ff9ff88 100644
--- a/ktexteditor/movingrange.cpp
+++ b/ktexteditor/movingrange.cpp
@@ -39,4 +39,16 @@ void MovingRange::setRange (const Cursor &start, const Cursor \
&end)  setRange (Range (start, end));
 }
 
+bool MovingRange::overlaps( const Range& range ) const
+{
+  if (range.start() <= start())
+    return range.end() > start();
+
+  else if (range.end() >= end())
+    return range.start() < end();
+
+  else
+    return contains(range);
+}
+
 // kate: space-indent on; indent-width 2; replace-tabs on;
diff --git a/ktexteditor/movingrange.h b/ktexteditor/movingrange.h
index b134903..eddef04 100644
--- a/ktexteditor/movingrange.h
+++ b/ktexteditor/movingrange.h
@@ -273,6 +273,103 @@ class KTEXTEDITOR_EXPORT MovingRange
     inline friend QDebug operator<< (QDebug s, const MovingRange &range) {
       return s << &range;
     }
+
+    /**
+     * Returns true if this range contains no characters, ie. the start() and
+     * end() positions are the same.
+     *
+     * \returns \e true if the range contains no characters, otherwise \e false
+     */
+    inline bool isEmpty() const {
+      return start() == end();
+    }
+
+    //BEGIN comparison functions
+    /**
+     * \name Comparison
+     *
+     * The following functions perform checks against this range in comparison
+     * to other lines, columns, cursors, and ranges.
+     */
+    /**
+     * Check whether the this range wholly encompasses \e range.
+     *
+     * \param range range to check
+     *
+     * \return \e true, if this range contains \e range, otherwise \e false
+     */
+    inline bool contains(const Range& range) const {
+      return range.start() >= start() && range.end() <= end();
+    }
+    
+    /**
+     * Check to see if \p cursor is contained within this range, ie >= start() and \
\< end(). +     *
+     * \param cursor the position to test for containment
+     *
+     * \return \e true if the cursor is contained within this range, otherwise \e \
false. +     */
+    inline bool contains(const Cursor& cursor) const {
+      return cursor >= start() && cursor < end();
+    }
+
+    /**
+     * Returns true if this range wholly encompasses \p line.
+     *
+     * \param line line to check
+     *
+     * \return \e true if the line is wholly encompassed by this range, otherwise \e \
false. +     */
+    inline bool containsLine(int line) const {
+      return (line > start().line() || (line == start().line() && \
!start().column())) && line < end().line(); +    }
+
+    /**
+     * Check whether the range contains \e column.
+     *
+     * \param column column to check
+     *
+     * \return \e true if the range contains \e column, otherwise \e false
+     */
+    inline bool containsColumn(int column) const {
+      return column >= start().column() && column < end().column();
+    }
+
+    /**
+     * Check whether the this range overlaps with \e range.
+     *
+     * \param range range to check against
+     *
+     * \return \e true, if this range overlaps with \e range, otherwise \e false
+     */
+    bool overlaps(const Range& range) const;
+
+    /**
+     * Check whether the range overlaps at least part of \e line.
+     *
+     * \param line line to check
+     *
+     * \return \e true, if the range overlaps at least part of \e line, otherwise \e \
false +     */
+    inline bool overlapsLine(int line) const {
+      return line >= start().line() && line <= end().line();
+    }
+
+    /**
+     * Check to see if this range overlaps \p column; that is, if \p column is
+     * between start().column() and end().column().  This function is most likely
+     * to be useful in relation to block text editing.
+     *
+     * \param column the column to test
+     *
+     * \return \e true if the column is between the range's starting and ending
+     *         columns, otherwise \e false.
+     */
+    inline bool overlapsColumn(int column) const {
+      return start().column() <= column && end().column() > column;
+    }
+    
+    //END comparison functions
 };
 
 Q_DECLARE_OPERATORS_FOR_FLAGS(MovingRange::InsertBehaviors)



_______________________________________________
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