aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorZufu Liu <unknown>2024-03-15 07:42:51 +1100
committerZufu Liu <unknown>2024-03-15 07:42:51 +1100
commit16f53f7b3f2f16f5caf62ae01d3e892d70f7d9ca (patch)
tree9f45136c47c4b799ced0133669b15fa2e89a583f
parent7b318c5156ae8b4c9a014717416ceaab7cf2aade (diff)
downloadscintilla-mirror-16f53f7b3f2f16f5caf62ae01d3e892d70f7d9ca.tar.gz
Feature [feature-requests:#1512]. Encapsulate access to position and length.
-rw-r--r--src/UndoHistory.cxx35
-rw-r--r--src/UndoHistory.h6
2 files changed, 25 insertions, 16 deletions
diff --git a/src/UndoHistory.cxx b/src/UndoHistory.cxx
index 70a50b870..929c8743e 100644
--- a/src/UndoHistory.cxx
+++ b/src/UndoHistory.cxx
@@ -177,6 +177,13 @@ size_t UndoActions::LengthTo(size_t index) const noexcept {
return sum;
}
+Sci::Position UndoActions::Position(int action) const noexcept {
+ return positions.SignedValueAt(action);
+}
+Sci::Position UndoActions::Length(int action) const noexcept {
+ return lengths.SignedValueAt(action);
+}
+
void ScrapStack::Clear() noexcept {
stack.clear();
current = 0;
@@ -279,14 +286,14 @@ const char *UndoHistory::AppendAction(ActionType at, Sci::Position position, con
} else if ((at != actions.types[targetAct].at)) { // } && (!actions.AtStart(targetAct))) {
coalesce = false;
} else if ((at == ActionType::insert) &&
- (position != (actions.positions.SignedValueAt(targetAct) + actions.lengths.SignedValueAt(targetAct)))) {
+ (position != (actions.Position(targetAct) + actions.Length(targetAct)))) {
// Insertions must be immediately after to coalesce
coalesce = false;
} else if (at == ActionType::remove) {
if ((lengthData == 1) || (lengthData == 2)) {
- if ((position + lengthData) == actions.positions.SignedValueAt(targetAct)) {
+ if ((position + lengthData) == actions.Position(targetAct)) {
; // Backspace -> OK
- } else if (position == actions.positions.SignedValueAt(targetAct)) {
+ } else if (position == actions.Position(targetAct)) {
; // Delete -> OK
} else {
// Removals must be at same position to coalesce
@@ -413,16 +420,16 @@ bool UndoHistory::AfterOrAtDetachPoint() const noexcept {
return detach && (*detach <= currentAction);
}
-intptr_t UndoHistory::Delta(int action) noexcept {
+intptr_t UndoHistory::Delta(int action) const noexcept {
intptr_t sizeChange = 0;
for (int act = 0; act < action; act++) {
- const intptr_t lengthChange = actions.lengths.SignedValueAt(act);
+ const intptr_t lengthChange = actions.Length(act);
sizeChange += (actions.types[act].at == ActionType::insert) ? lengthChange : -lengthChange;
}
return sizeChange;
}
-bool UndoHistory::Validate(intptr_t lengthDocument) noexcept {
+bool UndoHistory::Validate(intptr_t lengthDocument) const noexcept {
// Check history for validity
const intptr_t sizeChange = Delta(currentAction);
if (sizeChange > lengthDocument) {
@@ -432,8 +439,8 @@ bool UndoHistory::Validate(intptr_t lengthDocument) noexcept {
const intptr_t lengthOriginal = lengthDocument - sizeChange;
intptr_t lengthCurrent = lengthOriginal;
for (int act = 0; act < actions.SSize(); act++) {
- const intptr_t lengthChange = actions.lengths.SignedValueAt(act);
- if (actions.positions.SignedValueAt(act) > lengthCurrent) {
+ const intptr_t lengthChange = actions.Length(act);
+ if (actions.Position(act) > lengthCurrent) {
// Change outside document.
return false;
}
@@ -469,11 +476,11 @@ int UndoHistory::Type(int action) const noexcept {
}
Sci::Position UndoHistory::Position(int action) const noexcept {
- return actions.positions.SignedValueAt(action);
+ return actions.Position(action);
}
Sci::Position UndoHistory::Length(int action) const noexcept {
- return actions.lengths.SignedValueAt(action);
+ return actions.Length(action);
}
std::string_view UndoHistory::Text(int action) noexcept {
@@ -563,9 +570,9 @@ Action UndoHistory::GetUndoStep() const noexcept {
Action acta {
actions.types[previousAction].at,
actions.types[previousAction].mayCoalesce,
- actions.positions.SignedValueAt(previousAction),
+ actions.Position(previousAction),
nullptr,
- actions.lengths.SignedValueAt(previousAction)
+ actions.Length(previousAction)
};
if (acta.lenData) {
acta.data = scraps->CurrentText() - acta.lenData;
@@ -606,9 +613,9 @@ Action UndoHistory::GetRedoStep() const noexcept {
Action acta{
actions.types[currentAction].at,
actions.types[currentAction].mayCoalesce,
- actions.positions.SignedValueAt(currentAction),
+ actions.Position(currentAction),
nullptr,
- actions.lengths.SignedValueAt(currentAction)
+ actions.Length(currentAction)
};
if (acta.lenData) {
acta.data = scraps->CurrentText();
diff --git a/src/UndoHistory.h b/src/UndoHistory.h
index 8d39a908d..8cde395ae 100644
--- a/src/UndoHistory.h
+++ b/src/UndoHistory.h
@@ -58,6 +58,8 @@ struct UndoActions {
void Create(size_t index, ActionType at_, Sci::Position position_, Sci::Position lenData_, bool mayCoalesce_);
[[nodiscard]] bool AtStart(size_t index) const noexcept;
[[nodiscard]] size_t LengthTo(size_t index) const noexcept;
+ [[nodiscard]] Sci::Position Position(int action) const noexcept;
+ [[nodiscard]] Sci::Position Length(int action) const noexcept;
};
class ScrapStack {
@@ -121,8 +123,8 @@ public:
bool AfterDetachPoint() const noexcept;
bool AfterOrAtDetachPoint() const noexcept;
- [[nodiscard]] intptr_t Delta(int action) noexcept;
- [[nodiscard]] bool Validate(intptr_t lengthDocument) noexcept;
+ [[nodiscard]] intptr_t Delta(int action) const noexcept;
+ [[nodiscard]] bool Validate(intptr_t lengthDocument) const noexcept;
void SetCurrent(int action, intptr_t lengthDocument);
[[nodiscard]] int Current() const noexcept;
[[nodiscard]] int Type(int action) const noexcept;