Commit MetaInfo

Revisiona9baf779083304208769e43b4c6dbf28b6ac40f6 (tree)
Time2017-10-23 21:05:05
Author <exeal@user...>

Log Message

Replaced adaption flag with new adaptation level in kernel.AbstractPoint interface.

Change Summary

Incremental Difference

diff -r 208ef80d34a1 -r a9baf7790833 ascension/ascension/kernel/abstract-point.hpp
--- a/ascension/ascension/kernel/abstract-point.hpp Sun Oct 22 23:13:02 2017 +0900
+++ b/ascension/ascension/kernel/abstract-point.hpp Mon Oct 23 21:05:05 2017 +0900
@@ -29,14 +29,23 @@
2929 /// Base class of @c Point and @c viewer#VisualPoint.
3030 class AbstractPoint {
3131 public:
32+ /**
33+ * Adaptation levels.
34+ * @see #adaptationLevel, #setAdaptationLevel
35+ */
36+ enum AdaptationLevel {
37+ /// The point is moved automatically according to the document change.
38+ ADAPT_TO_DOCUMENT,
39+ /// @c ADAPT_TO_DOCUMENT and the point is shrunk to the accessible region of the document.
40+ ADAPT_TO_DOCUMENT_ACCESSIBLE_REGION
41+ };
42+
3243 explicit AbstractPoint(Document& document);
3344 AbstractPoint(const AbstractPoint& other);
3445 virtual ~AbstractPoint() BOOST_NOEXCEPT;
3546
3647 /// @name Document
3748 /// @{
38- bool adaptsToDocument() const BOOST_NOEXCEPT;
39- AbstractPoint& adaptToDocument(bool adapt) BOOST_NOEXCEPT;
4049 Document& document();
4150 const Document& document() const;
4251 bool isDocumentDisposed() const BOOST_NOEXCEPT;
@@ -44,7 +53,9 @@
4453
4554 /// @name Behaviors
4655 /// @{
56+ const boost::optional<AdaptationLevel>& adaptationLevel() const BOOST_NOEXCEPT;
4757 Direction gravity() const BOOST_NOEXCEPT;
58+ AbstractPoint& setAdaptationLevel(const boost::optional<AdaptationLevel>& level);
4859 AbstractPoint& setGravity(Direction gravity);
4960 /// @}
5061
@@ -55,41 +66,22 @@
5566 /// @}
5667
5768 private:
58- /// Called when @c Document#resetContent of the document was called.
69+ virtual void adaptationLevelChanged() BOOST_NOEXCEPT;
5970 virtual void contentReset() = 0;
60- /**
61- * Called when the content of the document is about to be changed.
62- * @param change The change
63- */
6471 virtual void documentAboutToBeChanged(const DocumentChange& change) = 0;
65- /**
66- * Called when the content of the document was changed.
67- * @param change The change
68- */
6972 virtual void documentChanged(const DocumentChange& change) = 0;
70- /// Called when the document is disposed.
7173 void documentDisposed() BOOST_NOEXCEPT;
7274 friend class Document;
7375 private:
7476 Document* document_; // weak reference
75- bool adapting_;
77+ boost::optional<AdaptationLevel> adaptationLevel_;
7678 Direction gravity_;
7779 DestructionSignal destructionSignal_;
7880 };
7981
8082 /// Returns @c true if the point is adapting to the document change.
81- inline bool AbstractPoint::adaptsToDocument() const BOOST_NOEXCEPT {
82- return adapting_;
83- }
84-
85- /**
86- * Adapts the point to the document change.
87- * @param adapt
88- * @post #adaptsToDocument() == adapt
89- */
90- inline AbstractPoint& AbstractPoint::adaptToDocument(bool adapt) BOOST_NOEXCEPT {
91- adapting_ = adapt;
92- return *this;
83+ inline const boost::optional<AbstractPoint::AdaptationLevel>& AbstractPoint::adaptationLevel() const BOOST_NOEXCEPT {
84+ return adaptationLevel_;
9385 }
9486
9587 /// Returns the @c AbstractPoint#DestructionSignal signal connector.
@@ -118,19 +110,17 @@
118110 }
119111
120112 /**
121- * @internal The document is in destruction.
122- * @post #isDocumentDisposed() == true
113+ * Returns the gravity.
114+ * @see #setGravity
123115 */
124- inline void AbstractPoint::documentDisposed() BOOST_NOEXCEPT {
125- document_ = nullptr;
126- }
127-
128- /// Returns the gravity.
129116 inline Direction AbstractPoint::gravity() const BOOST_NOEXCEPT {
130117 return gravity_;
131118 }
132119
133- /// Returns @c true if the document is already disposed.
120+ /**
121+ * Returns @c true if the document is already disposed.
122+ * @see #DestructionSignal
123+ */
134124 inline bool AbstractPoint::isDocumentDisposed() const BOOST_NOEXCEPT {
135125 return document_ == nullptr;
136126 }
diff -r 208ef80d34a1 -r a9baf7790833 ascension/ascension/kernel/locations.hpp
--- a/ascension/ascension/kernel/locations.hpp Sun Oct 22 23:13:02 2017 +0900
+++ b/ascension/ascension/kernel/locations.hpp Mon Oct 23 21:05:05 2017 +0900
@@ -72,7 +72,7 @@
7272 /// @defgroup accessible_regions_of_document Accessible Regions of Document
7373 /// Free functions related to document's accessible region.
7474 /// @{
75-// bool isOutsideOfAccessibleRegion(const PointProxy& p) BOOST_NOEXCEPT;
75+ bool isOutsideOfAccessibleRegion(const PointProxy& p) BOOST_NOEXCEPT;
7676 Position shrinkToAccessibleRegion(const PointProxy& p) BOOST_NOEXCEPT;
7777 Region shrinkToAccessibleRegion(const Document& document, const Region& region) BOOST_NOEXCEPT;
7878 /// @}
diff -r 208ef80d34a1 -r a9baf7790833 ascension/ascension/kernel/point.hpp
--- a/ascension/ascension/kernel/point.hpp Sun Oct 22 23:13:02 2017 +0900
+++ b/ascension/ascension/kernel/point.hpp Mon Oct 23 21:05:05 2017 +0900
@@ -38,6 +38,7 @@
3838 virtual void moved(const Position& from) BOOST_NOEXCEPT;
3939 private:
4040 // AbstractPoint
41+ void adaptationLevelChanged() BOOST_NOEXCEPT override;
4142 void contentReset() override;
4243 void documentAboutToBeChanged(const DocumentChange& change) override;
4344 void documentChanged(const DocumentChange& change) override;
diff -r 208ef80d34a1 -r a9baf7790833 ascension/ascension/viewer/visual-point.hpp
--- a/ascension/ascension/viewer/visual-point.hpp Sun Oct 22 23:13:02 2017 +0900
+++ b/ascension/ascension/viewer/visual-point.hpp Mon Oct 23 21:05:05 2017 +0900
@@ -106,6 +106,7 @@
106106 virtual void aboutToMove(TextHit& to);
107107 virtual void moved(const TextHit& from) BOOST_NOEXCEPT;
108108 // kernel.AbstractPoint
109+ virtual void adaptationLevelChanged() BOOST_NOEXCEPT override;
109110 virtual void contentReset() override;
110111 virtual void documentAboutToBeChanged(const kernel::DocumentChange& change) override;
111112 virtual void documentChanged(const kernel::DocumentChange& change) override;
diff -r 208ef80d34a1 -r a9baf7790833 ascension/src/kernel/abstract-point.cpp
--- a/ascension/src/kernel/abstract-point.cpp Sun Oct 22 23:13:02 2017 +0900
+++ b/ascension/src/kernel/abstract-point.cpp Mon Oct 23 21:05:05 2017 +0900
@@ -15,10 +15,10 @@
1515 * Constructor.
1616 * @param document The document to which the point attaches
1717 * @post &amp;#document() == &amp;document
18- * @post #adaptsToDocument() == true
18+ * @post #adaptationLevel() == #ADAPT_TO_DOCUMENT
1919 * @post #gravity() == Direction#forward()
2020 */
21- AbstractPoint::AbstractPoint(Document& document) : document_(&document), adapting_(true), gravity_(Direction::forward()) {
21+ AbstractPoint::AbstractPoint(Document& document) : document_(&document), adaptationLevel_(ADAPT_TO_DOCUMENT), gravity_(Direction::forward()) {
2222 static_cast<detail::PointCollection<AbstractPoint>*>(document_)->addNewPoint(*this);
2323 }
2424
@@ -27,10 +27,10 @@
2727 * @param other The source object
2828 * @throw DocumentDisposedException The document to which @a other belongs had been disposed
2929 * @post &amp;#document() == &amp;other.document
30- * @post #adaptsToDocument() == other.adaptsToDocument()
30+ * @post #adaptationLevel() == other.adaptationLevel()
3131 * @post #gravity() == other.gravity()
3232 */
33- AbstractPoint::AbstractPoint(const AbstractPoint& other) : document_(other.document_), adapting_(other.adapting_), gravity_(other.gravity_) {
33+ AbstractPoint::AbstractPoint(const AbstractPoint& other) : document_(other.document_), adaptationLevel_(other.adaptationLevel()), gravity_(other.gravity()) {
3434 if(document_ == nullptr)
3535 throw DocumentDisposedException();
3636 static_cast<detail::PointCollection<AbstractPoint>*>(document_)->addNewPoint(*this);
@@ -44,21 +44,65 @@
4444 }
4545
4646 /**
47+ * @internal The adaptation level was changed.
48+ * @see #setAdaptationLevel
49+ */
50+ void AbstractPoint::adaptationLevelChanged() BOOST_NOEXCEPT {
51+ }
52+
53+ /**
4754 * @fn ascension::kernel::AbstractPoint::contentReset
4855 * @c Document#resetContent method was called.
4956 */
5057
5158 /**
59+ * @fn ascension::kernel::AbstractPoint::documentAboutToBeChanged
60+ * Called when the content of the document is about to be changed.
61+ * @param change The content of the document change
62+ */
63+
64+ /**
5265 * @fn ascension::kernel::AbstractPoint::documentChanged
5366 * Called when the document was changed.
5467 * @param change The content of the document change
5568 */
5669
5770 /**
71+ * @internal The document is in destruction.
72+ * @post #isDocumentDisposed() == true
73+ * @see #DestructionSignal
74+ */
75+ void AbstractPoint::documentDisposed() BOOST_NOEXCEPT {
76+ document_ = nullptr;
77+ }
78+
79+ /**
80+ * Sets the adaptation level.
81+ * @param level The new adaptation level
82+ * @return This object
83+ * @post #adaptationLevel() == level
84+ * @throw UnknownValueException @a level is unknown
85+ * @note This method may invoke @c #adaptationLevelChanged method
86+ * @see #adaptationLevel
87+ */
88+ AbstractPoint& AbstractPoint::setAdaptationLevel(const boost::optional<AdaptationLevel>& level) {
89+ if(level != adaptationLevel_) {
90+ if(level != boost::none) {
91+ if(boost::get(level) != ADAPT_TO_DOCUMENT && boost::get(level) != ADAPT_TO_DOCUMENT_ACCESSIBLE_REGION)
92+ throw UnknownValueException("level");
93+ }
94+ adaptationLevel_ = level;
95+ adaptationLevelChanged();
96+ }
97+ return *this;
98+ }
99+
100+ /**
58101 * Sets the gravity.
59102 * @param gravity The new gravity value
60103 * @return This object
61104 * @post #gravity() == gravity
105+ * @see #gravity
62106 */
63107 AbstractPoint& AbstractPoint::setGravity(Direction gravity) {
64108 if(isDocumentDisposed())
diff -r 208ef80d34a1 -r a9baf7790833 ascension/src/kernel/document.cpp
--- a/ascension/src/kernel/document.cpp Sun Oct 22 23:13:02 2017 +0900
+++ b/ascension/src/kernel/document.cpp Mon Oct 23 21:05:05 2017 +0900
@@ -328,7 +328,7 @@
328328 (partitioner.get()->*partitionerMethod)(change);
329329 if(pointMethod != nullptr) {
330330 BOOST_FOREACH(AbstractPoint* p, points) {
331- if(p->adaptsToDocument())
331+ if(p->adaptationLevel() != boost::none)
332332 (p->*pointMethod)(change);
333333 }
334334 }
@@ -528,7 +528,7 @@
528528 else {
529529 widen();
530530 BOOST_FOREACH(AbstractPoint* p, points_) {
531- if(p->adaptsToDocument())
531+ if(p->adaptationLevel() != boost::none)
532532 p->contentReset();
533533 }
534534 bookmarker_->clear();
diff -r 208ef80d34a1 -r a9baf7790833 ascension/src/kernel/locations.cpp
--- a/ascension/src/kernel/locations.cpp Sun Oct 22 23:13:02 2017 +0900
+++ b/ascension/src/kernel/locations.cpp Mon Oct 23 21:05:05 2017 +0900
@@ -157,12 +157,21 @@
157157 }
158158
159159 /**
160+ * Returns @c true if the given position is outside of the accessible region of the document.
161+ * @param p The point to check
162+ * @return true if @a p is outside of the document
163+ */
164+ bool isOutsideOfAccessibleRegion(const PointProxy& p) BOOST_NOEXCEPT {
165+ return shrinkToAccessibleRegion(p) != position(p);
166+ }
167+
168+ /**
160169 * Returns @c true if the given position is outside of the document.
161170 * @param p The point to check
162171 * @return true if @a p is outside of the document
163172 */
164173 bool isOutsideOfDocumentRegion(const PointProxy& p) BOOST_NOEXCEPT {
165- return line(p) >= document(p).numberOfLines() || offsetInLine(p) > document(p).lineLength(line(p));
174+ return shrinkToDocumentRegion(p) != position(p);
166175 }
167176
168177 /**
diff -r 208ef80d34a1 -r a9baf7790833 ascension/src/kernel/point.cpp
--- a/ascension/src/kernel/point.cpp Sun Oct 22 23:13:02 2017 +0900
+++ b/ascension/src/kernel/point.cpp Mon Oct 23 21:05:05 2017 +0900
@@ -107,17 +107,23 @@
107107 boost::ignore_unused(to);
108108 }
109109
110+ /// @see AbstractPoint#adaptationLevelChanged
111+ void Point::adaptationLevelChanged() BOOST_NOEXCEPT {
112+ if(adaptationLevel() != boost::none && boost::get(adaptationLevel()) == ADAPT_TO_DOCUMENT_ACCESSIBLE_REGION && locations::isOutsideOfAccessibleRegion(*this))
113+ moveTo(locations::shrinkToAccessibleRegion(*this));
114+ }
115+
110116 /// @see AbstractPoint#contentReset
111117 void Point::contentReset() {
112118 assert(!isDocumentDisposed());
113- assert(adaptsToDocument());
119+ assert(adaptationLevel() != boost::none);
114120 moveTo(Position::zero());
115121 }
116122
117123 /// @see AbstractPoint#documentAboutToBeChanged
118124 void Point::documentAboutToBeChanged(const DocumentChange& change) {
119125 assert(!isDocumentDisposed());
120- assert(adaptsToDocument());
126+ assert(adaptationLevel() != boost::none);
121127 assert(destination_ == boost::none);
122128 destination_ = locations::updatePosition(position(), change, gravity());
123129 }
@@ -125,7 +131,7 @@
125131 /// @see AbstractPoint#documentChanged
126132 void Point::documentChanged(const DocumentChange& change) {
127133 assert(!isDocumentDisposed());
128- assert(adaptsToDocument());
134+ assert(adaptationLevel() != boost::none);
129135 if(destination_ != boost::none) {
130136 const auto newPosition(boost::get(destination_));
131137 destination_ = boost::none;
diff -r 208ef80d34a1 -r a9baf7790833 ascension/src/viewer/caret.cpp
--- a/ascension/src/viewer/caret.cpp Sun Oct 22 23:13:02 2017 +0900
+++ b/ascension/src/viewer/caret.cpp Mon Oct 23 21:05:05 2017 +0900
@@ -452,8 +452,8 @@
452452 void destructiveInsert(Caret& caret, const StringPiece& text, bool keepNewline = true) {
453453 if(text.cbegin() == nullptr || text.cend() == nullptr)
454454 throw NullPointerException("text");
455- const bool adapts = caret.adaptsToDocument();
456- caret.adaptToDocument(false);
455+ const boost::optional<kernel::AbstractPoint::AdaptationLevel> adaptationLevel(caret.adaptationLevel());
456+ caret.setAdaptationLevel(boost::none);
457457 const auto p(insertionPosition(caret));
458458 kernel::Position e((keepNewline && kernel::locations::isEndOfLine(caret)) ?
459459 p : kernel::locations::nextCharacter(caret, Direction::forward(), kernel::locations::GRAPHEME_CLUSTER));
@@ -461,12 +461,12 @@
461461 try {
462462 e = caret.document().replace(kernel::Region(p, e), text);
463463 } catch(...) {
464- caret.adaptToDocument(adapts);
464+ caret.setAdaptationLevel(adaptationLevel);
465465 throw;
466466 }
467467 caret.moveTo(TextHit::leading(e));
468468 }
469- caret.adaptToDocument(adapts);
469+ caret.setAdaptationLevel(adaptationLevel);
470470 }
471471 } // namespace @0
472472
diff -r 208ef80d34a1 -r a9baf7790833 ascension/src/viewer/visual-point.cpp
--- a/ascension/src/viewer/visual-point.cpp Sun Oct 22 23:13:02 2017 +0900
+++ b/ascension/src/viewer/visual-point.cpp Mon Oct 23 21:05:05 2017 +0900
@@ -248,6 +248,12 @@
248248 uninstall();
249249 }
250250
251+ /// @see AbstractPoint#adaptationLevelChanged
252+ void VisualPoint::adaptationLevelChanged() BOOST_NOEXCEPT {
253+ if(adaptationLevel() != boost::none && boost::get(adaptationLevel()) == ADAPT_TO_DOCUMENT_ACCESSIBLE_REGION && kernel::locations::isOutsideOfAccessibleRegion(*this))
254+ moveTo(TextHit::leading(kernel::locations::shrinkToAccessibleRegion(*this)));
255+ }
256+
251257 /**
252258 * See @c kernel#Point#aboutToMove. @c VisualPoint#aboutToMove does nothing.
253259 * @param to The destination position
@@ -276,14 +282,14 @@
276282 /// @see AbstractPoint#contentReset
277283 void VisualPoint::contentReset() {
278284 assert(!isDocumentDisposed());
279- assert(adaptsToDocument());
285+ assert(adaptationLevel() != boost::none);
280286 moveTo(TextHit::leading(kernel::Position::zero()));
281287 }
282288
283289 /// @see AbstractPoint#documentAboutToBeChanged
284290 void VisualPoint::documentAboutToBeChanged(const kernel::DocumentChange& change) {
285291 assert(!isDocumentDisposed());
286- assert(adaptsToDocument());
292+ assert(adaptationLevel() != boost::none);
287293 assert(hitAfterChange_ == boost::none);
288294 hitAfterChange_ = locations::updateTextHit(hit(), document(), change, gravity());
289295 }
@@ -291,7 +297,7 @@
291297 /// @see AbstractPoint#documentChanged
292298 void VisualPoint::documentChanged(const kernel::DocumentChange& change) {
293299 assert(!isDocumentDisposed());
294- assert(adaptsToDocument());
300+ assert(adaptationLevel() != boost::none);
295301 if(hitAfterChange_ != boost::none) {
296302 const auto newHit(boost::get(hitAfterChange_));
297303 hitAfterChange_ = boost::none;
@@ -530,13 +536,13 @@
530536
531537 /// @see VisualLinesListener#visualLinesDeleted
532538 void VisualPoint::visualLinesDeleted(const boost::integer_range<Index>& lines, Index, bool) BOOST_NOEXCEPT {
533- if(!adaptsToDocument() && includes(lines, kernel::line(hit().characterIndex())))
539+ if(adaptationLevel() == boost::none && includes(lines, kernel::line(hit().characterIndex())))
534540 lineNumberCaches_ = boost::none;
535541 }
536542
537543 /// @see VisualLinesListener#visualLinesInserted
538544 void VisualPoint::visualLinesInserted(const boost::integer_range<Index>& lines) BOOST_NOEXCEPT {
539- if(!adaptsToDocument() && includes(lines, kernel::line(hit().characterIndex())))
545+ if(adaptationLevel() == boost::none && includes(lines, kernel::line(hit().characterIndex())))
540546 lineNumberCaches_ = boost::none;
541547 }
542548
Show on old repository browser