Factor out shared method

Amends babb5b007b
This commit is contained in:
Juergen Spitzmueller 2024-03-31 12:40:27 +02:00
parent 55330a7844
commit 812e306dad
4 changed files with 26 additions and 50 deletions

View File

@ -2993,6 +2993,25 @@ void BufferView::putSelectionAt(DocIterator const & cur,
}
void BufferView::setSelection(DocIterator const & from,
DocIterator const & to)
{
if (from.pit() != to.pit()) {
// there are multiple paragraphs in selection
cursor().setCursor(from);
cursor().clearSelection();
cursor().selection(true);
cursor().setCursor(to);
cursor().selection(true);
} else {
// only single paragraph
int const size = to.pos() - from.pos();
putSelectionAt(from, size, false);
}
processUpdateFlags(Update::Force | Update::FitCursor);
}
bool BufferView::selectIfEmpty(DocIterator & cur)
{
if ((cur.inTexted() && !cur.paragraph().empty())

View File

@ -302,6 +302,9 @@ public:
*/
void putSelectionAt(DocIterator const & cur,
int length, bool backwards);
/// set a selection between \p from and \p to
void setSelection(DocIterator const & from,
DocIterator const & to);
/// selects the item at cursor if its paragraph is empty.
bool selectIfEmpty(DocIterator & cur);

View File

@ -72,8 +72,6 @@ struct SpellcheckerWidget::Private
void check();
/// close the spell checker dialog
void hide() const;
/// make/restore a selection between from and to
void setSelection(DocIterator const & from, DocIterator const & to) const;
/// if no selection was checked:
/// ask the user if the check should start over
bool continueFromBeginning();
@ -339,7 +337,7 @@ void SpellcheckerWidget::Private::hide() const
if (isCurrentBuffer(bvcur)) {
if (!begin_.empty() && !end_.empty()) {
// restore previous selection
setSelection(begin_, end_);
bv->setSelection(begin_, end_);
} else {
// restore cursor position
bvcur.setCursor(start_);
@ -349,29 +347,6 @@ void SpellcheckerWidget::Private::hide() const
}
}
void SpellcheckerWidget::Private::setSelection(
DocIterator const & from, DocIterator const & to) const
{
BufferView * bv = gv_->documentBufferView();
DocIterator end = to;
if (from.pit() != end.pit()) {
// there are multiple paragraphs in selection
Cursor & bvcur = bv->cursor();
bvcur.setCursor(from);
bvcur.clearSelection();
bvcur.selection(true);
bvcur.setCursor(end);
bvcur.selection(true);
} else {
// FIXME LFUN
// If we used a LFUN, dispatch would do all of this for us
int const size = end.pos() - from.pos();
bv->putSelectionAt(from, size, false);
}
bv->processUpdateFlags(Update::Force | Update::FitCursor);
}
void SpellcheckerWidget::Private::forward()
{
DocIterator const from = cursor();
@ -632,7 +607,7 @@ void SpellcheckerWidget::Private::check()
return;
setLanguage(word_lang.lang());
// mark misspelled word
setSelection(from, to);
bv->setSelection(from, to);
// enable relevant widgets
updateView();
}

View File

@ -281,27 +281,6 @@ bool searchAllowed(docstring const & str)
return true;
}
void setSelection(BufferView * bv, DocIterator const & from, DocIterator const & to)
{
DocIterator end = to;
if (from.pit() != end.pit()) {
// there are multiple paragraphs in selection
Cursor & bvcur = bv->cursor();
bvcur.setCursor(from);
bvcur.clearSelection();
bvcur.selection(true);
bvcur.setCursor(end);
bvcur.selection(true);
} else {
// FIXME LFUN
// If we used a LFUN, dispatch would do all of this for us
int const size = end.pos() - from.pos();
bv->putSelectionAt(from, size, false);
}
bv->processUpdateFlags(Update::Force | Update::FitCursor);
}
} // namespace
@ -408,7 +387,7 @@ bool findOne(BufferView * bv, docstring const & searchstr,
// restore original selection
if (had_selection) {
bv->cursor().resetAnchor();
setSelection(bv, startcur, endcur);
bv->setSelection(startcur, endcur);
}
return false;
}
@ -485,7 +464,7 @@ int replaceAll(BufferView * bv,
if (had_selection) {
endcur.fixIfBroken();
bv->cursor().resetAnchor();
setSelection(bv, startcur, endcur);
bv->setSelection(startcur, endcur);
}
return num;