KTextEditor::MovingRange Class

class KTextEditor::MovingRange

A range that is bound to a specific Document, and maintains its position. More...

Header: #include <KTextEditor/MovingRange>
CMake: find_package(KF6 REQUIRED COMPONENTS TextEditor)
target_link_libraries(mytarget PRIVATE KF6::TextEditor)
Since: 4.5

Public Types

enum EmptyBehavior { AllowEmpty, InvalidateIfEmpty }
enum InsertBehavior { DoNotExpand, ExpandLeft, ExpandRight }
flags InsertBehaviors

Public Functions

MovingRange(const KTextEditor::MovingRange &)
virtual ~MovingRange()
virtual const KTextEditor::Attribute::Ptr &attribute() const = 0
virtual bool attributeOnlyForViews() const = 0
bool contains(KTextEditor::Cursor cursor) const
bool contains(const KTextEditor::Range &range) const
bool containsColumn(int column) const
bool containsLine(int line) const
virtual KTextEditor::Document *document() const = 0
virtual KTextEditor::MovingRange::EmptyBehavior emptyBehavior() const = 0
virtual const KTextEditor::MovingCursor &end() const = 0
virtual KTextEditor::MovingRangeFeedback *feedback() const = 0
virtual KTextEditor::MovingRange::InsertBehaviors insertBehaviors() const = 0
bool isEmpty() const
int numberOfLines() const
bool onSingleLine() const
bool overlaps(const KTextEditor::Range &range) const
bool overlapsColumn(int column) const
bool overlapsLine(int line) const
virtual void setAttribute(KTextEditor::Attribute::Ptr attribute) = 0
virtual void setAttributeOnlyForViews(bool onlyForViews) = 0
virtual void setEmptyBehavior(KTextEditor::MovingRange::EmptyBehavior emptyBehavior) = 0
virtual void setFeedback(KTextEditor::MovingRangeFeedback *feedback) = 0
virtual void setInsertBehaviors(KTextEditor::MovingRange::InsertBehaviors insertBehaviors) = 0
virtual void setRange(KTextEditor::Range range) = 0
(since 6.0) virtual void setRange(KTextEditor::Range range, KTextEditor::Attribute::Ptr attribute) = 0
(since 6.0) virtual void setRange(KTextEditor::Range range, KTextEditor::Attribute::Ptr attribute, qreal zDepth) = 0
void setRange(KTextEditor::Cursor start, KTextEditor::Cursor end)
virtual void setView(KTextEditor::View *view) = 0
virtual void setZDepth(qreal zDepth) = 0
virtual const KTextEditor::MovingCursor &start() const = 0
KTextEditor::LineRange toLineRange() const
const KTextEditor::Range toRange() const
virtual KTextEditor::View *view() const = 0
virtual qreal zDepth() const = 0
KTextEditor::Range operator KTextEditor::Range() const
KTextEditor::MovingRange &operator=(const KTextEditor::MovingRange &)

Protected Functions

QDebug operator<<(QDebug s, const KTextEditor::MovingRange &range)
QDebug operator<<(QDebug s, const KTextEditor::MovingRange *range)

Detailed Description

Introduction

A MovingRange is an extension of the basic Range class. It maintains its position in the document. As a result of this, MovingRange%s may not be copied, as they need to maintain a connection to the associated Document.

Create a new MovingRange like this:

KTextEditor::MovingRange* range = yourDocument->newMovingRange();

The ownership of the range is passed to the user. When finished with a MovingRange, simply delete it.

Editing Behavior

The insert behavior controls how the range reacts to characters inserted at the range boundaries, i.e. at the start of the range or the end of the range. Either the range boundary moves with text insertion, or it stays. Use setInsertBehaviors() and insertBehaviors() to set and query the current insert behavior.

When the start() and end() Cursor of a range equal, isEmpty() returns true. Further, the empty-behavior can be changed such that the start() and end() Cursor%s of MovingRange%s that get empty are automatically set to (-1, -1). Use setEmptyBehavior() and emptyBehavior() to control the empty behavior.

Warning: MovingRanges may be set to (-1, -1, -1, -1) at any time, if the user reloads a document (F5)! Use a MovingRangeFeedback to get notified if you need to catch this case, and/or listen to the signal MovingInterface::aboutToInvalidateMovingInterfaceContent().

MovingRange Feedback

With setFeedback() a feedback instance can be associated with the moving range. The MovingRangeFeedback notifies about the following events:

  • the text cursor (caret) entered the range,
  • the text cursor (caret) left the range,
  • the mouse cursor entered the range,
  • the mouse cursor left the range,
  • the range became empty, i.e. start() == end(),
  • the range became invalid, i.e. start() == end() == (-1, -1).

If a feedback is not needed anymore, call setFeedback(0).

Working with Ranges

There are several convenience methods that make working with MovingRanges very simple. For instance, use isEmpty() to check if the start() Cursor equals the end() Cursor. Use contains(), containsLine() or containsColumn() to check whether the MovingRange contains a Range, a Cursor, a line or column. The same holds for overlaps(), overlapsLine() and overlapsColumn(). Besides onSingleLine() returns whether a MovingRange spans only one line.

For compatibility, a MovingRange can be explicitly converted to a simple Range by calling toRange(), or implicitly by the Range operator.

Arbitrary Highlighting

With setAttribute() highlighting Attribute%s can be assigned to a MovingRange. By default, this highlighting is used in all views of a document. Use setView(), if the highlighting should only appear in a specific view. Further, if the additional highlighting should not be printed call setAttributeOnlyForViews() with the parameter true.

MovingRange Example

In the following example, we assume the KTextEditor::Document has the contents:

void printText(const std::string & text); // this is line 3

In order to highlight the function name printText with a yellow background color, the following code is needed:

KTextEditor::View * view = ...;
KTextEditor::Document * doc = view->document();

// range is of type KTextEditor::MovingRange*
auto range = doc->newMovingRange(KTextEditor::Range(3, 5, 3, 14));

KTextEditor::Attribute::Ptr attrib = new KTextEditor::Attribute();
attrib->setBackground(Qt::yellow);

range->setAttribute(attrib);

MovingRange%s are invalidated automatically when a document is cleared or closed.

See also Cursor, MovingCursor, Range, MovingInterface, and MovingRangeFeedback.

Member Type Documentation

enum MovingRange::EmptyBehavior

Behavior of range if it becomes empty.

ConstantValueDescription
KTextEditor::MovingRange::AllowEmpty0x0Allow range to be empty
KTextEditor::MovingRange::InvalidateIfEmpty0x1Invalidate range if it becomes empty

enum MovingRange::InsertBehavior
flags MovingRange::InsertBehaviors

Determines how the range reacts to characters inserted immediately outside the range.

ConstantValueDescription
KTextEditor::MovingRange::DoNotExpand0x0Don't expand to encapsulate new characters in either direction. This is the default.
KTextEditor::MovingRange::ExpandLeft0x1Expand to encapsulate new characters to the left of the range.
KTextEditor::MovingRange::ExpandRight0x2Expand to encapsulate new characters to the right of the range.

The InsertBehaviors type is a typedef for QFlags<InsertBehavior>. It stores an OR combination of InsertBehavior values.

See also InsertBehaviors.

Member Function Documentation

[protected] MovingRange::MovingRange()

For inherited class only.

MovingRange::MovingRange(const KTextEditor::MovingRange &)

no copy constructor, don't allow this to be copied.

[virtual noexcept] MovingRange::~MovingRange()

Destruct the moving range.

[pure virtual] const KTextEditor::Attribute::Ptr &MovingRange::attribute() const

Gets the active Attribute for this range.

Returns a pointer to the active attribute

See also setAttribute().

[pure virtual] bool MovingRange::attributeOnlyForViews() const

Is this range's attribute only visible in views, not for example prints? Default is false. Returns range visible only for views

See also setAttributeOnlyForViews().

bool MovingRange::contains(KTextEditor::Cursor cursor) const

Check to see if cursor is contained within this range, ie >= start() and < end().

cursor is the position to test for containment

Returns true if the cursor is contained within this range, otherwise false.

bool MovingRange::contains(const KTextEditor::Range &range) const

Check whether the this range wholly encompasses range.

range is the range to check

Returns true, if this range contains range, otherwise false

bool MovingRange::containsColumn(int column) const

Check whether the range contains column.

column is the column to check

Returns true if the range contains column, otherwise false

bool MovingRange::containsLine(int line) const

Returns true if this range wholly encompasses line.

line is the line to check

Returns true if the line is wholly encompassed by this range, otherwise false.

[pure virtual] KTextEditor::Document *MovingRange::document() const

Gets the document to which this range is bound.

Returns a pointer to the document

[pure virtual] KTextEditor::MovingRange::EmptyBehavior MovingRange::emptyBehavior() const

Will this range invalidate itself if it becomes empty? Returns behavior on becoming empty

See also setEmptyBehavior().

[pure virtual] const KTextEditor::MovingCursor &MovingRange::end() const

Retrieve end cursor of this range, read-only. Returns end cursor

[pure virtual] KTextEditor::MovingRangeFeedback *MovingRange::feedback() const

Gets the active MovingRangeFeedback for this range.

Returns a pointer to the active MovingRangeFeedback

See also setFeedback().

[pure virtual] KTextEditor::MovingRange::InsertBehaviors MovingRange::insertBehaviors() const

Get current insert behaviors. Returns current insert behaviors

See also setInsertBehaviors().

bool MovingRange::isEmpty() const

Returns true if this range contains no characters, ie. the start() and end() positions are the same.

Returns true if the range contains no characters, otherwise false

[noexcept] int MovingRange::numberOfLines() const

Returns the number of lines separating the start() and end() positions.

Returns the number of lines separating the start() and end() positions; 0 if the start and end lines are the same.

bool MovingRange::onSingleLine() const

Check whether the start() and end() cursors of this range are on the same line.

Returns true if both the start and end positions are on the same line, otherwise false

bool MovingRange::overlaps(const KTextEditor::Range &range) const

Check whether the this range overlaps with range.

range is the range to check against

Returns true, if this range overlaps with range, otherwise false

bool MovingRange::overlapsColumn(int column) const

Check to see if this range overlaps column; that is, if column is between start().column() and end().column(). This function is most likely to be useful in relation to block text editing.

column is the column to test

Returns true if the column is between the range's starting and ending columns, otherwise false.

bool MovingRange::overlapsLine(int line) const

Check whether the range overlaps at least part of line.

line is the line to check

Returns true, if the range overlaps at least part of line, otherwise false

[pure virtual] void MovingRange::setAttribute(KTextEditor::Attribute::Ptr attribute)

Sets the currently active attribute for this range. This will trigger update of the relevant view parts, if the attribute changed.

attribute is the attribute to assign to this range. If null, simply removes the previous Attribute.

See also attribute().

[pure virtual] void MovingRange::setAttributeOnlyForViews(bool onlyForViews)

Set if this range's attribute is only visible in views, not for example prints.

onlyForViews if true, the attribute is only valid for views

See also attributeOnlyForViews().

[pure virtual] void MovingRange::setEmptyBehavior(KTextEditor::MovingRange::EmptyBehavior emptyBehavior)

Set if this range will invalidate itself if it becomes empty.

emptyBehavior is the behavior on becoming empty

See also emptyBehavior().

[pure virtual] void MovingRange::setFeedback(KTextEditor::MovingRangeFeedback *feedback)

Sets the currently active MovingRangeFeedback for this range. This will trigger evaluation if feedback must be send again (for example if mouse is already inside range).

feedback is the MovingRangeFeedback to assign to this range. If null, simply removes the previous MovingRangeFeedback.

See also feedback().

[pure virtual] void MovingRange::setInsertBehaviors(KTextEditor::MovingRange::InsertBehaviors insertBehaviors)

Set insert behaviors.

insertBehaviors are the new insert behaviors

See also insertBehaviors().

[pure virtual] void MovingRange::setRange(KTextEditor::Range range)

Set the range of this range.

A TextRange is not allowed to be empty, as soon as start == end position, it will become automatically invalid!

range new range for this clever range

[pure virtual, since 6.0] void MovingRange::setRange(KTextEditor::Range range, KTextEditor::Attribute::Ptr attribute)

Set the range of this range and the connected attribute. Avoids internal overhead of separate setting that.

A TextRange is not allowed to be empty, as soon as start == end position, it will become automatically invalid!

range is the new range for this clever range

attribute is the attribute to assign to this range. If null, simply removes the previous Attribute.

This function was introduced in 6.0.

[pure virtual, since 6.0] void MovingRange::setRange(KTextEditor::Range range, KTextEditor::Attribute::Ptr attribute, qreal zDepth)

Set the range of this range and the connected attribute and Z-depth. Avoids internal overhead of separate setting that.

A TextRange is not allowed to be empty, as soon as start == end position, it will become automatically invalid!

range is the new range for this clever range

attribute is the attribute to assign to this range. If null, simply removes the previous Attribute.

zDepth is the new Z-depth of this range

This function was introduced in 6.0.

void MovingRange::setRange(KTextEditor::Cursor start, KTextEditor::Cursor end)

This is an overloaded function.

Set the range of this range A TextRange is not allowed to be empty, as soon as start == end position, it will become automatically invalid!

start is the new start for this clever range

end is the new end for this clever range

[pure virtual] void MovingRange::setView(KTextEditor::View *view)

Sets the currently active view for this range. This will trigger update of the relevant view parts, if the view changed. Set view before the attribute, that will avoid not needed redraws.

view is the View to assign to this range. If null, simply removes the previous view.

See also view().

[pure virtual] void MovingRange::setZDepth(qreal zDepth)

Set the current Z-depth of this range. Ranges with smaller Z-depth than others will win during rendering. This will trigger update of the relevant view parts, if the depth changed. Set depth before the attribute, that will avoid not needed redraws. Default is 0.0.

zDepth is new Z-depth of this range

See also zDepth().

[pure virtual] const KTextEditor::MovingCursor &MovingRange::start() const

Retrieve start cursor of this range, read-only. Returns start cursor

[noexcept] KTextEditor::LineRange MovingRange::toLineRange() const

Convert this MovingRange to a simple LineRange. Returns LineRange from the start line to the end line of this range.

const KTextEditor::Range MovingRange::toRange() const

Convert this clever range into a dumb one. Returns normal range

[pure virtual] KTextEditor::View *MovingRange::view() const

Gets the active view for this range. Might be already invalid, internally only used for pointer comparisons.

Returns a pointer to the active view

See also setView().

[pure virtual] qreal MovingRange::zDepth() const

Gets the current Z-depth of this range. Ranges with smaller Z-depth than others will win during rendering. Default is 0.0.

Defined depths for common kind of ranges use in editor components implementing this interface, smaller depths are more more in the foreground and will win during rendering:

  • Selection == -100000.0
  • Search == -10000.0
  • Bracket Highlighting == -1000.0
  • Folding Hover == -100.0

Returns current Z-depth of this range

See also setZDepth().

KTextEditor::Range MovingRange::operator KTextEditor::Range() const

Convert this clever range into a dumb one. Equal to toRange, allowing to use implicit conversion. Returns normal range

KTextEditor::MovingRange &MovingRange::operator=(const KTextEditor::MovingRange &)

no assignment operator, no copying around clever ranges.

Related Non-Members

QDebug operator<<(QDebug s, const KTextEditor::MovingRange &range)

qDebug() stream operator. Writes this range to the debug output in a nicely formatted way.

s is the debug stream

range is the range to print

Returns debug stream

QDebug operator<<(QDebug s, const KTextEditor::MovingRange *range)

qDebug() stream operator. Writes this range to the debug output in a nicely formatted way.

s is the debug stream

range is the range to print

Returns debug stream