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.
This commit is contained in:
Neil
2024-09-02 08:42:25 +10:00
parent 8cffa89c3b
commit 5977b00099
3 changed files with 28 additions and 39 deletions

View File

@@ -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());

View File

@@ -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 };

View File

@@ -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) {