diff options
author | Neil <nyamatongwe@gmail.com> | 2024-09-02 08:42:25 +1000 |
---|---|---|
committer | Neil <nyamatongwe@gmail.com> | 2024-09-02 08:42:25 +1000 |
commit | 5977b000998149b60a80fa4bb779a663078b951a (patch) | |
tree | cc1ddb6b909d01c8eb9159116f4d02b03d150d2c | |
parent | 8cffa89c3bae58e55b61b24b9cfc76baad3584da (diff) | |
download | scintilla-mirror-5977b000998149b60a80fa4bb779a663078b951a.tar.gz |
Hoist some common code into methods.
RangeContainsProtected gains an overload that takes a SelectionRange as this is
common use and simplifies callers.
ClearSelectionRange is common code used to clear selected text and update a
SelectionRange before inserting text.
-rw-r--r-- | src/Editor.cxx | 62 | ||||
-rw-r--r-- | src/Editor.h | 2 | ||||
-rw-r--r-- | src/ScintillaBase.cxx | 3 |
3 files changed, 28 insertions, 39 deletions
diff --git a/src/Editor.cxx b/src/Editor.cxx index 356f73baa..d1bcfa02a 100644 --- a/src/Editor.cxx +++ b/src/Editor.cxx @@ -774,10 +774,13 @@ bool Editor::RangeContainsProtected(Sci::Position start, Sci::Position end) cons return false; } +bool Editor::RangeContainsProtected(const SelectionRange &range) const noexcept { + return RangeContainsProtected(range.Start().Position(), range.End().Position()); +} + bool Editor::SelectionContainsProtected() const noexcept { for (size_t r=0; r<sel.Count(); r++) { - if (RangeContainsProtected(sel.Range(r).Start().Position(), - sel.Range(r).End().Position())) { + if (RangeContainsProtected(sel.Range(r))) { return true; } } @@ -2045,17 +2048,10 @@ void Editor::InsertCharacter(std::string_view sv, CharacterSource charSource) { for (std::vector<SelectionRange *>::reverse_iterator rit = selPtrs.rbegin(); rit != selPtrs.rend(); ++rit) { SelectionRange *currentSel = *rit; - if (!RangeContainsProtected(currentSel->Start().Position(), - currentSel->End().Position())) { + if (!RangeContainsProtected(*currentSel)) { Sci::Position positionInsert = currentSel->Start().Position(); if (!currentSel->Empty()) { - if (currentSel->Length()) { - pdoc->DeleteChars(positionInsert, currentSel->Length()); - currentSel->ClearVirtualSpace(); - } else { - // Range is all virtual so collapse to start of virtual space - currentSel->MinimizeVirtualSpace(); - } + ClearSelectionRange(*currentSel); } else if (inOverstrike) { if (positionInsert < pdoc->Length()) { if (!pdoc->IsPositionInLineEnd(positionInsert)) { @@ -2123,24 +2119,26 @@ void Editor::InsertCharacter(std::string_view sv, CharacterSource charSource) { } } +void Editor::ClearSelectionRange(SelectionRange &range) { + if (!range.Empty()) { + if (range.Length()) { + pdoc->DeleteChars(range.Start().Position(), range.Length()); + range.ClearVirtualSpace(); + } else { + // Range is all virtual so collapse to start of virtual space + range.MinimizeVirtualSpace(); + } + } +} + void Editor::ClearBeforeTentativeStart() { // Make positions for the first composition string. FilterSelections(); UndoGroup ug(pdoc, (sel.Count() > 1) || !sel.Empty() || inOverstrike); for (size_t r = 0; r<sel.Count(); r++) { - if (!RangeContainsProtected(sel.Range(r).Start().Position(), - sel.Range(r).End().Position())) { - const Sci::Position positionInsert = sel.Range(r).Start().Position(); - if (!sel.Range(r).Empty()) { - if (sel.Range(r).Length()) { - pdoc->DeleteChars(positionInsert, sel.Range(r).Length()); - sel.Range(r).ClearVirtualSpace(); - } else { - // Range is all virtual so collapse to start of virtual space - sel.Range(r).MinimizeVirtualSpace(); - } - } - RealizeVirtualSpace(positionInsert, sel.Range(r).caret.VirtualSpace()); + if (!RangeContainsProtected(sel.Range(r))) { + ClearSelectionRange(sel.Range(r)); + RealizeVirtualSpace(sel.Range(r).caret.Position(), sel.Range(r).caret.VirtualSpace()); sel.Range(r).ClearVirtualSpace(); } } @@ -2157,18 +2155,9 @@ void Editor::InsertPaste(const char *text, Sci::Position len) { } else { // MultiPaste::Each for (size_t r=0; r<sel.Count(); r++) { - if (!RangeContainsProtected(sel.Range(r).Start().Position(), - sel.Range(r).End().Position())) { + if (!RangeContainsProtected(sel.Range(r))) { Sci::Position positionInsert = sel.Range(r).Start().Position(); - if (!sel.Range(r).Empty()) { - if (sel.Range(r).Length()) { - pdoc->DeleteChars(positionInsert, sel.Range(r).Length()); - sel.Range(r).ClearVirtualSpace(); - } else { - // Range is all virtual so collapse to start of virtual space - sel.Range(r).MinimizeVirtualSpace(); - } - } + ClearSelectionRange(sel.Range(r)); positionInsert = RealizeVirtualSpace(positionInsert, sel.Range(r).caret.VirtualSpace()); const Sci::Position lengthInserted = pdoc->InsertString(positionInsert, text, len); if (lengthInserted > 0) { @@ -2214,8 +2203,7 @@ void Editor::ClearSelection(bool retainMultipleSelections) { UndoGroup ug(pdoc); for (size_t r=0; r<sel.Count(); r++) { if (!sel.Range(r).Empty()) { - if (!RangeContainsProtected(sel.Range(r).Start().Position(), - sel.Range(r).End().Position())) { + if (!RangeContainsProtected(sel.Range(r))) { pdoc->DeleteChars(sel.Range(r).Start().Position(), sel.Range(r).Length()); sel.Range(r) = SelectionRange(sel.Range(r).Start()); diff --git a/src/Editor.h b/src/Editor.h index 0cfdaf64e..d95f552ad 100644 --- a/src/Editor.h +++ b/src/Editor.h @@ -355,6 +355,7 @@ protected: // ScintillaBase subclass needs access to much of Editor enum class AddNumber { one, each }; void MultipleSelectAdd(AddNumber addNumber); bool RangeContainsProtected(Sci::Position start, Sci::Position end) const noexcept; + bool RangeContainsProtected(const SelectionRange &range) const noexcept; bool SelectionContainsProtected() const noexcept; Sci::Position MovePositionOutsideChar(Sci::Position pos, Sci::Position moveDir, bool checkLineEnd=true) const; SelectionPosition MovePositionOutsideChar(SelectionPosition pos, Sci::Position moveDir, bool checkLineEnd=true) const; @@ -425,6 +426,7 @@ protected: // ScintillaBase subclass needs access to much of Editor SelectionPosition RealizeVirtualSpace(const SelectionPosition &position); void AddChar(char ch); virtual void InsertCharacter(std::string_view sv, Scintilla::CharacterSource charSource); + void ClearSelectionRange(SelectionRange &range); void ClearBeforeTentativeStart(); void InsertPaste(const char *text, Sci::Position len); enum class PasteShape { stream=0, rectangular = 1, line = 2 }; diff --git a/src/ScintillaBase.cxx b/src/ScintillaBase.cxx index 15e29b819..a95df76ae 100644 --- a/src/ScintillaBase.cxx +++ b/src/ScintillaBase.cxx @@ -222,8 +222,7 @@ void ScintillaBase::AutoCompleteInsert(Sci::Position startPos, Sci::Position rem } else { // MultiAutoComplete::Each for (size_t r=0; r<sel.Count(); r++) { - if (!RangeContainsProtected(sel.Range(r).Start().Position(), - sel.Range(r).End().Position())) { + if (!RangeContainsProtected(sel.Range(r))) { Sci::Position positionInsert = sel.Range(r).Start().Position(); positionInsert = RealizeVirtualSpace(positionInsert, sel.Range(r).caret.VirtualSpace()); if (positionInsert - removeLen >= 0) { |