diff options
author | Neil <nyamatongwe@gmail.com> | 2025-02-01 14:43:22 +1100 |
---|---|---|
committer | Neil <nyamatongwe@gmail.com> | 2025-02-01 14:43:22 +1100 |
commit | f54fd2019dd648b29a80ec7429833ab546d3a428 (patch) | |
tree | 231bebba7db25fccf798340bdd908f28fd504c26 /src | |
parent | db6332fa9933244c45c44063afbdcccb462cfc03 (diff) | |
download | scintilla-mirror-f54fd2019dd648b29a80ec7429833ab546d3a428.tar.gz |
Serialize selection type and ranges with SCI_GETSELECTIONSERIALIZED and
SCI_SETSELECTIONSERIALIZED.
Diffstat (limited to 'src')
-rw-r--r-- | src/Editor.cxx | 18 | ||||
-rw-r--r-- | src/Editor.h | 1 | ||||
-rw-r--r-- | src/Selection.cxx | 17 | ||||
-rw-r--r-- | src/Selection.h | 2 |
4 files changed, 38 insertions, 0 deletions
diff --git a/src/Editor.cxx b/src/Editor.cxx index 10c694990..d85a4c294 100644 --- a/src/Editor.cxx +++ b/src/Editor.cxx @@ -711,6 +711,15 @@ void Editor::SetEmptySelection(Sci::Position currentPos_) { SetEmptySelection(SelectionPosition(currentPos_)); } +void Editor::SetSelectionFromSerialized(const char *serialized) { + if (serialized) { + sel = Selection(serialized); + sel.Truncate(pdoc->Length()); + SetRectangularRange(); + InvalidateStyleRedraw(); + } +} + void Editor::MultipleSelectAdd(AddNumber addNumber) { if (SelectionEmpty() || !multipleSelection) { // Select word at caret @@ -8695,6 +8704,15 @@ sptr_t Editor::WndProc(Message iMessage, uptr_t wParam, sptr_t lParam) { case Message::GetUndoSelectionHistory: return static_cast<sptr_t>(undoSelectionHistoryOption); + case Message::SetSelectionSerialized: + SetSelectionFromSerialized(ConstCharPtrFromSPtr(lParam)); + break; + + case Message::GetSelectionSerialized: { + const std::string serialized = sel.ToString(); + return BytesResult(lParam, serialized); + } + case Message::SetExtraAscent: vs.extraAscent = static_cast<int>(wParam); InvalidateStyleRedraw(); diff --git a/src/Editor.h b/src/Editor.h index a6a484ede..ff940bc49 100644 --- a/src/Editor.h +++ b/src/Editor.h @@ -352,6 +352,7 @@ protected: // ScintillaBase subclass needs access to much of Editor void SetSelection(SelectionPosition currentPos_); void SetEmptySelection(SelectionPosition currentPos_); void SetEmptySelection(Sci::Position currentPos_); + void SetSelectionFromSerialized(const char *serialized); enum class AddNumber { one, each }; void MultipleSelectAdd(AddNumber addNumber); bool RangeContainsProtected(Sci::Position start, Sci::Position end) const noexcept; diff --git a/src/Selection.cxx b/src/Selection.cxx index 5650d5c7e..7b517cc4a 100644 --- a/src/Selection.cxx +++ b/src/Selection.cxx @@ -225,6 +225,13 @@ bool SelectionRange::Trim(SelectionRange range) noexcept { } } +void SelectionRange::Truncate(Sci::Position length) noexcept { + if (anchor.Position() > length) + anchor.SetPosition(length); + if (caret.Position() > length) + caret.SetPosition(length); +} + // If range is all virtual collapse to start of virtual space void SelectionRange::MinimizeVirtualSpace() noexcept { if (caret.Position() == anchor.Position()) { @@ -568,6 +575,16 @@ void Selection::SetRanges(const Ranges &rangesToSet) { ranges = rangesToSet; } +void Selection::Truncate(Sci::Position length) noexcept { + // This may be needed when applying a persisted selection onto a document that has been shortened. + for (SelectionRange &range : ranges) { + range.Truncate(length); + } + // Above may have made some non-unique empty ranges. + RemoveDuplicates(); + rangeRectangular.Truncate(length); +} + std::string Selection::ToString() const { std::string result; switch (selType) { diff --git a/src/Selection.h b/src/Selection.h index 68e7a4e54..57de92a57 100644 --- a/src/Selection.h +++ b/src/Selection.h @@ -148,6 +148,7 @@ struct SelectionRange { } void Swap() noexcept; bool Trim(SelectionRange range) noexcept; + void Truncate(Sci::Position length) noexcept; // If range is all virtual collapse to start of virtual space void MinimizeVirtualSpace() noexcept; std::string ToString() const; @@ -216,6 +217,7 @@ public: return ranges; } void SetRanges(const Ranges &rangesToSet); + void Truncate(Sci::Position length) noexcept; std::string ToString() const; }; |