/usr/include/ktexteditor/smartrange.h is in kdelibs5-dev 4:4.8.4-4+deb7u1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 | /* This file is part of the KDE project
Copyright (C) 2003-2005 Hamish Rodda <rodda@kde.org>
Copyright (C) 2008 David Nolden <david.nolden.kdevelop@art-master.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef KDELIBS_KTEXTEDITOR_SMARTRANGE_H
#define KDELIBS_KTEXTEDITOR_SMARTRANGE_H
#include <ktexteditor/ktexteditor_export.h>
#include <ktexteditor/range.h>
#include <ktexteditor/smartcursor.h>
#include <ktexteditor/attribute.h>
#include <QtCore/QList>
#ifndef DOXYGEN_SHOULD_SKIP_THIS
template <class T> class QStack;
#endif
class KAction;
namespace KTextEditor
{
class SmartRangeNotifier;
class SmartRangeWatcher;
/**
* \short A Range which is bound to a specific Document, and maintains its position.
*
* \ingroup kte_group_smart_classes
*
* A SmartRange is an extension of the basic Range class. It maintains its
* position in the document and provides extra functionality,
* including:
* \li convenience functions for accessing and manipulating the content
* of the associated document,
* \li adjusting behavior in response to text edits,
* \li forming a tree structure out of multiple SmartRange%s,
* \li providing attribute information for the arbitrary highlighting extension,
* \li allowing KAction%s to be bound to the range (note: not currently implemented), and
* \li providing notification of changes to 3rd party software.
*
* As a result of a smart range's close association with a document, and the processing
* that occurrs as a result, smart ranges may not be copied.
*
* For simplicity of code, ranges always maintain their start position to
* be before or equal to their end position. Attempting to set either the
* start or end of the range beyond the respective end or start will result in
* both values being set to the specified position.
*
* Hierarchical range-trees maintain specific relationships:
* - When a child-range is changed, all parent-ranges are resized so the child-range fits in
* - When a parent-range is changed, all child-ranges are resized so they fit in
*
* This means that parent-ranges always completely contain all their child-ranges. However
* it may lead to unexpected range-changes from the perspective of your application, so keep
* this in mind.
*
* The child-ranges of one smart-range are allowed to overlap each other. However overlaps
* should be omitted where possible, for performance-reasons, and because each range can
* be overlapped by max. 63 sibling-ranges while still rendering correctly.
*
* Create a new SmartRange like this:
* \code
* // Retrieve the SmartInterface
* KTextEditor::SmartInterface* smart =
* qobject_cast<KTextEditor::SmartInterface*>( yourDocument );
*
* if ( smart ) {
* KTextEditor::SmartRange* range = smart->newSmartRange();
* }
* \endcode
*
* When finished with a SmartRange, simply delete it.
*
* \sa Range, SmartRangeNotifier, SmartRangeWatcher, and SmartInterface
*
* \author Hamish Rodda \<rodda@kde.org\>
*/
class KTEXTEDITOR_EXPORT SmartRange : public Range
{
friend class SmartCursor;
public:
/// Determine how the range reacts to characters inserted immediately outside the range.
enum InsertBehavior {
/// Don't expand to encapsulate new characters in either direction. This is the default.
DoNotExpand = 0,
/// Expand to encapsulate new characters to the left of the range.
ExpandLeft = 0x1,
/// Expand to encapsulate new characters to the right of the range.
ExpandRight = 0x2
};
Q_DECLARE_FLAGS(InsertBehaviors, InsertBehavior)
virtual ~SmartRange();
/**
* Returns that this range is a SmartRange.
*/
virtual bool isSmartRange() const;
/**
* Returns this range as a SmartRange, if it is one.
*/
virtual SmartRange* toSmartRange() const;
/**
* \name Position
*
* The following functions provide access and manipulation of the range's position.
* \{
*/
/**
* \copydoc Range::setRange(const Range&)
*
* This function also provides any required adjustment of parent and child ranges,
* and notification of the change if required.
*/
virtual void setRange(const Range& range);
/**
* Get the start point of this range. This version returns a casted
* version of start(), as SmartRange%s always use SmartCursor%s as
* the start() and end().
*
* \returns a reference to the start of this range.
*/
inline SmartCursor& smartStart()
{ return *static_cast<SmartCursor*>(m_start); }
/**
* Get the start point of this range. This version returns a casted
* version of start(), as SmartRange%s always use SmartCursor%s as
* the start() and end().
*
* \returns a const reference to the start of this range.
*/
inline const SmartCursor& smartStart() const
{ return *static_cast<const SmartCursor*>(m_start); }
/**
* Get the end point of this range. This version returns a casted
* version of end(), as SmartRange%s always use SmartCursor%s as
* the start() and end().
*
* \returns a reference to the end of this range.
*/
inline SmartCursor& smartEnd()
{ return *static_cast<SmartCursor*>(m_end); }
/**
* Get the end point of this range. This version returns a casted
* version of end(), as SmartRange%s always use SmartCursor%s as
* the start() and end().
*
* \returns a const reference to the end of this range.
*/
inline const SmartCursor& smartEnd() const
{ return *static_cast<const SmartCursor*>(m_end); }
/**
* \overload confineToRange(const Range&)
* Overloaded version which confines child ranges as well.
*/
virtual bool confineToRange(const Range& range);
/**
* \overload expandToRange(const Range&)
* Overloaded version which expands child ranges as well.
*/
virtual bool expandToRange(const Range& range);
//BEGIN Functionality present from having this range associated with a Document
/**
* \}
*
* \name Document-related functions
*
* The following functions are provided for convenient access to the
* associated Document.
* \{
*/
/**
* Retrieve the document associated with this SmartRange.
*
* \return a pointer to the associated document
*/
Document* document() const;
/**
* Retrieve the text which is contained within this range.
*
* \param block specify whether the text should be returned from the range's visual block, rather
* than all the text within the range.
*/
virtual QStringList text(bool block = false) const;
/**
* Replace text in this range with \p text
*
* \param text text to use as a replacement
* \param block insert this text as a visual block of text rather than a linear sequence
* \return \e true on success, otherwise \e false
*/
virtual bool replaceText(const QStringList &text, bool block = false);
/**
* Remove text contained within this range.
* The range itself will not be deleted.
*
* \param block specify whether the text should be deleted from the range's visual block, rather
* than all the text within the range.
*/
virtual bool removeText(bool block = false);
//END
//BEGIN Behavior
/**
* \}
*
* \name Behavior
*
* The following functions relate to the behavior of this SmartRange.
* \{
*/
/**
* Returns how this range reacts to characters inserted immediately outside the range.
*
* \return the current insert behavior.
*/
InsertBehaviors insertBehavior() const;
/**
* Determine how the range should react to characters inserted immediately outside the range.
*
* \todo does this need a custom function to enable determining of the behavior based on the
* text that is inserted / deleted?
*
* \param behavior the insertion behavior to use for future edits
*
* \sa InsertBehavior
*/
void setInsertBehavior(InsertBehaviors behavior);
//END
//BEGIN Relationships to other ranges
/**
* \}
*
* \name Tree structure
*
* The following functions relate to the tree structure functionality.
* \{
*/
/**
* Returns this range's parent range, if one exists.
*
* At all times, this range will be contained within parentRange().
*
* \return a pointer to the current parent range
*/
inline SmartRange* parentRange() const
{ return m_parentRange; }
/**
* Set this range's parent range.
*
* At all times, this range will be contained within parentRange(). So, if it is outside of the
* new parent to begin with, it will be expanded automatically.
*
* When being inserted into the parent range, the parent range will be fit in between any other
* pre-existing child ranges, and may resize them so as not to overlap. However, once insertion
* has occurred, changing this range directly will only resize the others, it will \e not change
* the order of the ranges. To change the order, unset the parent range, change the range, and
* re-set the parent range.
*
* \param r range to become the new parent of this range
*/
virtual void setParentRange(SmartRange* r);
/**
* Determine whether \a parent is a parent of this range.
*
* \param parent range to check to see if it is a parent of this range.
*
* \return \e true if \a parent is in the parent heirachy, otherwise \e false.
*/
bool hasParent(SmartRange* parent) const;
/**
* Calculate the current depth of this range.
*
* \return the depth of this range, where 0 is no parent, 1 is one parent, etc.
*/
inline int depth() const
{ return m_parentRange ? m_parentRange->depth() + 1 : 0; }
/**
* Returns the range's top parent range, or this range if there are no parents.
*
* \return a pointer to the top parent range
*/
inline SmartRange* topParentRange() const
{ return parentRange() ? parentRange()->topParentRange() : const_cast<SmartRange*>(this); }
/**
* Get the ordered list of child ranges.
*
* To insert a child range, simply set its parent to this range using setParentRange().
*
* \returns a list of child ranges.
*/
const QList<SmartRange*>& childRanges() const;
/**
* Clears child ranges - i.e., removes the text that is covered by the ranges.
* The ranges themselves are not deleted.
*
* \sa removeText()
*/
void clearChildRanges();
/**
* Deletes child ranges - i.e., deletes the SmartRange objects only.
* The underlying text is not affected.
*/
void deleteChildRanges();
/**
* Clears child ranges - i.e., clears the text that is covered by the ranges,
* and deletes the SmartRange objects.
*/
void clearAndDeleteChildRanges();
/**
* Find the child before \p range, if any.
* The order is determined by the range end-cursors.
*
* \param range to seach backwards from
*
* \return the range before \p range if one exists, otherwise null.
*/
SmartRange* childBefore( const SmartRange * range ) const;
/**
* Find the child after \p range, if any.
* The order is determined by the range end-cursors.
*
* \param range to seach forwards from
*
* \return the range after \p range if one exists, otherwise null.
*/
SmartRange* childAfter( const SmartRange * range ) const;
/**
* Finds the most specific range in a heirachy for the given input range
* (ie. the smallest range which wholly contains the input range)
*
* In case of overlaps, the smallest containing range is chosen,
* if there are multiple of the same size, then the first one.
*
* \param input the range to use in searching
*
* \return the deepest range which contains \p input
*/
SmartRange* mostSpecificRange(const Range& input) const;
/**
* Finds the first child range which contains position \p pos.
*
* \param pos the cursor position to use in searching
*
* \return the most shallow range (from and including this range) which
* contains \p pos
*/
SmartRange* firstRangeContaining(const Cursor& pos) const;
/**
* Finds the deepest range in the heirachy which contains position \p pos.
* Allows the caller to determine which ranges were entered and exited
* by providing pointers to QStack<SmartRange*>.
*
* If child-ranges overlap in the given position,
* the first smallest one is returned.
*
* \param pos the cursor position to use in searching
* \param rangesEntered provide a QStack<SmartRange*> here to find out
* which ranges were entered during the traversal.
* The top item was the first descended.
* \param rangesExited provide a QStack<SmartRange*> here to find out
* which ranges were exited during the traversal.
* The top item was the first exited.
*
* \return the deepest range (from and including this range) which
* contains \p pos, or null if no ranges contain this position.
*/
SmartRange* deepestRangeContaining(const Cursor& pos,
QStack<SmartRange*>* rangesEntered = 0L,
QStack<SmartRange*>* rangesExited = 0L) const;
QList<SmartRange*> deepestRangesContaining(const Cursor& pos) const;
/**
* Returns the count of ranges within the parent-range
* that end behind this range, and that overlap this range.
*/
int overlapCount() const;
//END
//BEGIN Arbitrary highlighting
/**
* \}
*
* \name Arbitrary highlighting
*
* The following functions relate to arbitrary highlighting capabilities.
* \{
*/
/**
* Gets the active Attribute for this range.
*
* \return a pointer to the active attribute
*/
Attribute::Ptr attribute() const;
/**
* Sets the currently active attribute for this range.
*
* \param attribute Attribute to assign to this range. If null, simply
* removes the previous Attribute.
*
* \note \ref SmartInterface::addHighlightToDocument must be called with the top-range before
* the highlighting can work.
*/
void setAttribute(Attribute::Ptr attribute);
//END
//BEGIN Action binding
/**
* \}
*
* \name Action binding
*
* The following functions relate to action binding capabilities.
*
* \note This feature is currently not implemented (ETA KDE 4.1).
* \{
*/
/**
* Associate an action with this range. The associated action(s) will be
* enabled when the caret enters the range, and disabled them on exit.
* The action is also added to the context menu when the mouse/caret is within
* an associated range.
*
* \param action KAction to associate with this range
*/
void associateAction(KAction* action);
/**
* Remove the association with an action from this range; it will no
* longer be managed.
*
* \param action KAction to dissociate from this range
*/
void dissociateAction(KAction* action);
/**
* Access the list of currently associated KAction%s.
*
* \return the list of associated actions
*/
const QList<KAction*>& associatedActions() const
{ return m_associatedActions; }
/**
* Clears all associations between KAction%s and this range.
*/
void clearAssociatedActions();
//END
//BEGIN Notification methods
/**
* \}
*
* \name Notification
*
* The following functions allow for changes related to this range to be
* notified to 3rd party programs.
* \{
*/
/**
* Connect to a notifier to receive signals indicating change of state of this range.
* This function creates a notifier if none is already bound to this range; if one has
* already been assigned this will return the first notifier.
*
* If you have finished with notifications for a reasonable period of time you can
* save memory by calling deleteNotifier().
*/
SmartRangeNotifier* primaryNotifier();
/**
* Returns a list of notifiers which are receiving signals indicating change of state
* of this range. These notifiers may be receiving signals from other ranges as well.
*/
const QList<SmartRangeNotifier*> notifiers() const;
/**
* Register a notifier to receive signals indicating change of state of this range.
*
* NOTE: Make sure you call @c removeNotifier() when deleting the notifier before the range.
*
* \param notifier notifier to register. Ownership is not transferred.
*/
void addNotifier(SmartRangeNotifier* notifier);
/**
* Deregister a notifier and no longer deliver signals indicating change of state of this range.
*
* \param notifier notifier to deregister.
*/
void removeNotifier(SmartRangeNotifier* notifier);
/**
* When finished with the primaryNotifier(), call this method to save memory by
* having the SmartRangeNotifier deleted.
*
* \note If a notifier was first registered via addNotifier() rather than created inside
* primaryNotifier(), this method will delete that notifier. Text editor implementations
* should not use notifiers for internal purposes, instead use watchers (faster and
* has documentation to this effect)
*/
void deletePrimaryNotifier();
/**
* Returns a list of registered SmartRangeWatchers.
*
* \note this function may return watchers internal to the text editor's implementation,
* eg. in the case of arbitrary highlighting and kate part. Removing these watchers
* with removeWatcher() will result in malfunction.
*/
const QList<SmartRangeWatcher*>& watchers() const;
/**
* Register a SmartRangeWatcher to receive calls indicating change of state
* of this range. To finish receiving notifications, call removeWatcher().
*
* NOTE: Make sure you call @c removeWachter() when deleting the notifier before the range.
*
* \param watcher the instance of a class which is to receive
* notifications about changes to this range.
*/
void addWatcher(SmartRangeWatcher* watcher);
/**
* Stop delivery of notifications to a SmartRangeWatcher.
*
* \param watcher the watcher that no longer wants notifications.
*/
void removeWatcher(SmartRangeWatcher* watcher);
//!\}
//END
/**
* Assignment operator. Assigns the current position of the provided range, \p rhs, only;
* does not assign watchers, notifiers, behavior etc.
*
* \note The assignment will be performed even if the provided range belongs to
* another Document.
*
* \param rhs range to assign to this range.
*
* \return a reference to this range, after assignment has occurred.
*
* \see setRange()
*/
inline SmartRange& operator=(const SmartRange& rhs)
{ setRange(rhs); return *this; }
/**
* Assignment operator. Assigns the current position of the provided range, \p rhs.
*
* \param rhs range to assign to this range.
*
* \return a reference to this range, after assignment has occurred.
*
* \see setRange()
*/
inline SmartRange& operator=(const Range& rhs)
{ setRange(rhs); return *this; }
protected:
/**
* Constructor for subclasses to utilise. Protected to prevent direct
* instantiation.
*
* \note 3rd party developers: you do not (and should not) need to subclass
* the Smart* classes; instead, use the SmartInterface to create instances.
*
* \internal
*
* \param start the start cursor to use - ownership is taken
* \param end the end cursor to use - ownership is taken
* \param parent the parent range if this is a subrange of another range
* \param insertBehavior the behavior of this range when an insert happens
* immediately outside the range.
*/
SmartRange(SmartCursor* start, SmartCursor* end, SmartRange* parent = 0L, InsertBehaviors insertBehavior = DoNotExpand);
/**
* \internal
*
* Notify this range that one or both of the cursors' position has changed directly.
*
* \param cursor the cursor that changed. If null, both cursors have changed.
* \param from the previous position of this range
*/
virtual void rangeChanged(Cursor* cursor, const Range& from);
/**
* \internal
*
* This routine is called when the range changes how much feedback it may need, eg. if it adds an action.
*/
virtual void checkFeedback();
/**
* \internal
*
* Called to request creation of a new SmartRangeNotifier for this object.
*/
virtual SmartRangeNotifier* createNotifier() = 0;
/**
* Is called after child-ranges have changed internally without the rangeChanged() notification, for example
* after translations. It rebuilds the child-structure, so it is consistent again.
*/
void rebuildChildStructure();
private:
/**
* \internal
* Copy constructor: Disable copying of this class.
*/
SmartRange(const SmartRange&);
/**
* \internal
* Implementation of deepestRangeContaining().
*/
SmartRange* deepestRangeContainingInternal(const Cursor& pos,
QStack<SmartRange*>* rangesEntered,
QStack<SmartRange*>* rangesExited,
bool first = false) const;
/**
* \internal
*
* New child classes call this to register themselves.
*/
void insertChildRange(SmartRange* newChild);
/**
* \internal
*
* Disassociating child classes call this to de-register themselves.
*/
void removeChildRange(SmartRange* newChild);
/**
* \internal
*
* This range's current attribute.
*/
Attribute::Ptr m_attribute;
SmartRange* m_parentRange;
/**
* \internal
*
* The list of this range's child ranges, sorted by end-cursor.
*/
QList<SmartRange*> m_childRanges;
/**
* \internal
*
* The list of this range's associated KAction%s.
*/
QList<KAction*> m_associatedActions;
/**
* \internal
*
* The list of registered SmartRangeNotifiers.
*/
QList<SmartRangeNotifier*> m_notifiers;
/**
* \internal
*
* The list of registered SmartRangeWatchers.
*/
QList<SmartRangeWatcher*> m_watchers;
/**
* \internal
*
* Whether this range owns the currently assigned attribute or not.
*/
bool m_ownsAttribute :1;
/**
* \internal
*
* How many ranges that end behind this one at least partially overlap it.
* Currently max. 64 overlaps are allowed.
*/
uchar m_overlapCount:6;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(SmartRange::InsertBehaviors)
}
#endif
// kate: space-indent on; indent-width 2; replace-tabs on;
|