Newer
Older
}
void BaseTextEditorPrivate::clearVisibleCollapsedBlock()
{
if (suggestedVisibleCollapsedBlockNumber) {
suggestedVisibleCollapsedBlockNumber = -1;
collapsedBlockTimer.stop();
}
if (visibleCollapsedBlockNumber >= 0) {
visibleCollapsedBlockNumber = -1;
q->viewport()->update();
}
}
void BaseTextEditor::mouseMoveEvent(QMouseEvent *e)
{
d->m_lastEventWasBlockSelectionEvent = (e->modifiers() & Qt::AltModifier);
updateLink(e);
if (e->buttons() == Qt::NoButton) {
const QTextBlock collapsedBlock = collapsedBlockAt(e->pos());
const int blockNumber = collapsedBlock.next().blockNumber();
if (blockNumber < 0) {
d->clearVisibleCollapsedBlock();
} else if (blockNumber != d->visibleCollapsedBlockNumber) {
d->suggestedVisibleCollapsedBlockNumber = blockNumber;
d->collapsedBlockTimer.start(40, this);
}
// Update the mouse cursor
if (collapsedBlock.isValid() && !d->m_mouseOnCollapsedMarker) {
d->m_mouseOnCollapsedMarker = true;
viewport()->setCursor(Qt::PointingHandCursor);
} else if (!collapsedBlock.isValid() && d->m_mouseOnCollapsedMarker) {
d->m_mouseOnCollapsedMarker = false;
viewport()->setCursor(Qt::IBeamCursor);
}
} else {
QPlainTextEdit::mouseMoveEvent(e);
}
if (d->m_lastEventWasBlockSelectionEvent && d->m_inBlockSelectionMode) {
if (textCursor().atBlockEnd()) {
d->m_blockSelectionExtraX = qMax(0, e->pos().x() - cursorRect().center().x()) / fontMetrics().averageCharWidth();
if (viewport()->cursor().shape() == Qt::BlankCursor)
viewport()->setCursor(Qt::IBeamCursor);
}
void BaseTextEditor::mousePressEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton) {
d->clearBlockSelection(); // just in case, otherwise we might get strange drag and drop
QTextBlock collapsedBlock = collapsedBlockAt(e->pos());
if (collapsedBlock.isValid()) {
toggleBlockVisible(collapsedBlock);
viewport()->setCursor(Qt::IBeamCursor);
}
updateLink(e);
if (d->m_currentLink.isValid())
d->m_linkPressed = true;
void BaseTextEditor::mouseReleaseEvent(QMouseEvent *e)
{
if (d->m_mouseNavigationEnabled
&& d->m_linkPressed
&& e->modifiers() & Qt::ControlModifier
&& !(e->modifiers() & Qt::ShiftModifier)
&& e->button() == Qt::LeftButton
) {
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
const QTextCursor cursor = cursorForPosition(e->pos());
if (openLink(findLinkAt(cursor))) {
clearLink();
return;
}
}
QPlainTextEdit::mouseReleaseEvent(e);
}
void BaseTextEditor::leaveEvent(QEvent *e)
{
// Clear link emulation when the mouse leaves the editor
clearLink();
QPlainTextEdit::leaveEvent(e);
}
void BaseTextEditor::keyReleaseEvent(QKeyEvent *e)
{
// Clear link emulation when Ctrl is released
if (e->key() == Qt::Key_Control)
clearLink();
QPlainTextEdit::keyReleaseEvent(e);
}
// fake missing mouse move event from Qt
QMouseEvent me(QEvent::MouseMove, QPoint(-1, -1), Qt::NoButton, 0, 0);
extraAreaMouseEvent(&me);
}
void BaseTextEditor::extraAreaMouseEvent(QMouseEvent *e)
{
QTextCursor cursor = cursorForPosition(QPoint(0, e->pos().y()));
cursor.setPosition(cursor.block().position());
int markWidth;
extraAreaWidth(&markWidth);
if (d->m_codeFoldingVisible
&& e->type() == QEvent::MouseMove && e->buttons() == 0) { // mouse tracking
// Update which folder marker is highlighted
const int highlightBlockNumber = d->extraAreaHighlightCollapseBlockNumber;
const int highlightColumn = d->extraAreaHighlightCollapseColumn;
d->extraAreaHighlightCollapseColumn = -1;
if (e->pos().x() > extraArea()->width() - collapseBoxWidth(fontMetrics())) {
d->extraAreaHighlightCollapseBlockNumber = cursor.blockNumber();
if (TextBlockUserData::canCollapse(cursor.block())
|| !TextBlockUserData::hasClosingCollapse(cursor.block()))
d->extraAreaHighlightCollapseColumn = cursor.block().length()-1;
if (TextBlockUserData::hasCollapseAfter(cursor.block())) {
d->extraAreaHighlightCollapseBlockNumber++;
d->extraAreaHighlightCollapseColumn = -1;
if (TextBlockUserData::canCollapse(cursor.block().next())
|| !TextBlockUserData::hasClosingCollapse(cursor.block().next()))
d->extraAreaHighlightCollapseColumn = cursor.block().next().length()-1;
}
} else if (d->m_displaySettings.m_highlightBlocks) {
QTextCursor cursor = textCursor();
d->extraAreaHighlightCollapseBlockNumber = cursor.blockNumber();
d->extraAreaHighlightCollapseColumn = cursor.position() - cursor.block().position();
if (highlightBlockNumber != d->extraAreaHighlightCollapseBlockNumber
|| highlightColumn != d->extraAreaHighlightCollapseColumn) {
d->m_highlightBlocksTimer->start(d->m_highlightBlocksInfo.isEmpty() ? 120 : 0);
}

Thorbjørn Lindeijer
committed
// Set whether the mouse cursor is a hand or normal arrow
if (e->type() == QEvent::MouseMove) {
bool hand = (e->pos().x() <= markWidth);
if (hand != (d->m_extraArea->cursor().shape() == Qt::PointingHandCursor))
d->m_extraArea->setCursor(hand ? Qt::PointingHandCursor : Qt::ArrowCursor);
}
if (e->type() == QEvent::MouseButtonPress || e->type() == QEvent::MouseButtonDblClick) {
if (e->button() == Qt::LeftButton) {
int boxWidth = collapseBoxWidth(fontMetrics());
if (d->m_codeFoldingVisible && e->pos().x() > extraArea()->width() - boxWidth) {
if (!cursor.block().next().isVisible()) {
toggleBlockVisible(cursor.block());
d->moveCursorVisible(false);
} else if (collapseBox().contains(e->pos())) {
cursor.setPosition(
document()->findBlockByNumber(d->m_highlightBlocksInfo.open.last()).position()
);
QTextBlock c = cursor.block();
if (TextBlockUserData::hasCollapseAfter(c.previous()))
c = c.previous();
toggleBlockVisible(c);
d->moveCursorVisible(false);
}
} else if (d->m_marksVisible && e->pos().x() > markWidth) {
QTextCursor selection = cursor;
selection.setVisualNavigation(true);
d->extraAreaSelectionAnchorBlockNumber = selection.blockNumber();
selection.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
selection.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
setTextCursor(selection);
} else {
d->extraAreaToggleMarkBlockNumber = cursor.blockNumber();
}
} else if (d->m_marksVisible && e->button() == Qt::RightButton) {
QMenu * contextMenu = new QMenu(this);
emit d->m_editable->markContextMenuRequested(editableInterface(), cursor.blockNumber() + 1, contextMenu);
if (!contextMenu->isEmpty())
contextMenu->exec(e->globalPos());
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
}
} else if (d->extraAreaSelectionAnchorBlockNumber >= 0) {
QTextCursor selection = cursor;
selection.setVisualNavigation(true);
if (e->type() == QEvent::MouseMove) {
QTextBlock anchorBlock = document()->findBlockByNumber(d->extraAreaSelectionAnchorBlockNumber);
selection.setPosition(anchorBlock.position());
if (cursor.blockNumber() < d->extraAreaSelectionAnchorBlockNumber) {
selection.movePosition(QTextCursor::EndOfBlock);
selection.movePosition(QTextCursor::Right);
}
selection.setPosition(cursor.block().position(), QTextCursor::KeepAnchor);
if (cursor.blockNumber() >= d->extraAreaSelectionAnchorBlockNumber) {
selection.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
selection.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
}
if (e->pos().y() >= 0 && e->pos().y() <= d->m_extraArea->height())
d->autoScrollTimer.stop();
else if (!d->autoScrollTimer.isActive())
d->autoScrollTimer.start(100, this);
} else {
d->autoScrollTimer.stop();
d->extraAreaSelectionAnchorBlockNumber = -1;
return;
}
setTextCursor(selection);
} else if (d->extraAreaToggleMarkBlockNumber >= 0 && d->m_marksVisible && d->m_requestMarkEnabled) {
if (e->type() == QEvent::MouseButtonRelease && e->button() == Qt::LeftButton) {
int n = d->extraAreaToggleMarkBlockNumber;
d->extraAreaToggleMarkBlockNumber = -1;
if (cursor.blockNumber() == n) {
int line = n + 1;
emit d->m_editable->markRequested(editableInterface(), line);
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
}
}
}
}
QTextBlock TextBlockUserData::testCollapse(const QTextBlock& block)
{
QTextBlock info = block;
if (block.userData() && static_cast<TextBlockUserData*>(block.userData())->collapseMode() == CollapseAfter)
;
else if (block.next().userData()
&& static_cast<TextBlockUserData*>(block.next().userData())->collapseMode()
== TextBlockUserData::CollapseThis)
info = block.next();
else
return QTextBlock();
int pos = static_cast<TextBlockUserData*>(info.userData())->collapseAtPos();
if (pos < 0)
return QTextBlock();
QTextCursor cursor(info);
cursor.setPosition(cursor.position() + pos);
matchCursorForward(&cursor);
return cursor.block();
}
void TextBlockUserData::doCollapse(const QTextBlock& block, bool visible)
{
QTextBlock info = block;
if (block.userData() && static_cast<TextBlockUserData*>(block.userData())->collapseMode() == CollapseAfter)
;
else if (block.next().userData()
&& static_cast<TextBlockUserData*>(block.next().userData())->collapseMode()
== TextBlockUserData::CollapseThis)
info = block.next();
else {
if (visible && !block.next().isVisible()) {
// no match, at least unfold!
QTextBlock b = block.next();
while (b.isValid() && !b.isVisible()) {
b.setVisible(true);
b.setLineCount(visible ? qMax(1, b.layout()->lineCount()) : 0);
b = b.next();
}
}
return;
}
int pos = static_cast<TextBlockUserData*>(info.userData())->collapseAtPos();
if (pos < 0)
return;
QTextCursor cursor(info);
cursor.setPosition(cursor.position() + pos);
if (matchCursorForward(&cursor) != Match) {
if (visible) {
// no match, at least unfold!
QTextBlock b = block.next();
while (b.isValid() && !b.isVisible()) {
b.setVisible(true);
b.setLineCount(visible ? qMax(1, b.layout()->lineCount()) : 0);
b = b.next();
}
}
return;
}
QTextBlock b = block.next();
while (b < cursor.block()) {
b.setVisible(visible);
b.setLineCount(visible ? qMax(1, b.layout()->lineCount()) : 0);
if (visible) {
TextBlockUserData *data = canCollapse(b);
if (data && data->collapsed()) {
QTextBlock end = testCollapse(b);
if (data->collapseIncludesClosure())
end = end.next();
if (end.isValid()) {
b = end;
continue;
}
}
}
b = b.next();
}
bool collapseIncludesClosure = hasClosingCollapseAtEnd(b);
if (collapseIncludesClosure) {
b.setVisible(visible);
b.setLineCount(visible ? qMax(1, b.layout()->lineCount()) : 0);
}
static_cast<TextBlockUserData*>(info.userData())->setCollapseIncludesClosure(collapseIncludesClosure);
static_cast<TextBlockUserData*>(info.userData())->setCollapsed(!block.next().isVisible());
}
void BaseTextEditor::ensureCursorVisible()
{
QTextBlock block = textCursor().block();
if (!block.isVisible()) {
while (!block.isVisible() && block.previous().isValid())
block = block.previous();
toggleBlockVisible(block);
}
QPlainTextEdit::ensureCursorVisible();
}
void BaseTextEditor::toggleBlockVisible(const QTextBlock &block)
{
TextEditDocumentLayout *documentLayout = qobject_cast<TextEditDocumentLayout*>(document()->documentLayout());
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
bool visible = block.next().isVisible();
TextBlockUserData::doCollapse(block, !visible);
documentLayout->requestUpdate();
documentLayout->emitDocumentSizeChanged();
}
const TabSettings &BaseTextEditor::tabSettings() const
{
return d->m_document->tabSettings();
}
const DisplaySettings &BaseTextEditor::displaySettings() const
{
return d->m_displaySettings;
}
void BaseTextEditor::indentOrUnindent(bool doIndent)
{
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
int pos = cursor.position();
const TextEditor::TabSettings &tabSettings = d->m_document->tabSettings();
QTextDocument *doc = document();
if (!cursor.hasSelection()
|| (doc->findBlock(cursor.selectionStart()) == doc->findBlock(cursor.selectionEnd()) )) {
cursor.removeSelectedText();
QTextBlock block = cursor.block();
QString text = block.text();
int indentPosition = (cursor.position() - block.position());;
int spaces = tabSettings.spacesLeftFromPosition(text, indentPosition);
int startColumn = tabSettings.columnAt(text, indentPosition - spaces);
int targetColumn = tabSettings.indentedColumn(tabSettings.columnAt(text, indentPosition), doIndent);
cursor.setPosition(block.position() + indentPosition);
cursor.setPosition(block.position() + indentPosition - spaces, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
cursor.insertText(tabSettings.indentationString(startColumn, targetColumn));
} else {
int anchor = cursor.anchor();
int start = qMin(anchor, pos);
int end = qMax(anchor, pos);
QTextBlock startBlock = doc->findBlock(start);
QTextBlock endBlock = doc->findBlock(end-1).next();
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
QString text = block.text();
int indentPosition = tabSettings.lineIndentPosition(text);
if (!doIndent && !indentPosition)
indentPosition = tabSettings.firstNonSpace(text);
int targetColumn = tabSettings.indentedColumn(tabSettings.columnAt(text, indentPosition), doIndent);
cursor.setPosition(block.position() + indentPosition);
cursor.insertText(tabSettings.indentationString(0, targetColumn));
cursor.setPosition(block.position());
cursor.setPosition(block.position() + indentPosition, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
}
}
cursor.endEditBlock();
}
void BaseTextEditor::handleHomeKey(bool anchor)
{
QTextCursor cursor = textCursor();
QTextCursor::MoveMode mode = QTextCursor::MoveAnchor;
if (anchor)
mode = QTextCursor::KeepAnchor;
const int initpos = cursor.position();
int pos = cursor.block().position();
QChar character = characterAt(pos);
const QLatin1Char tab = QLatin1Char('\t');
while (character == tab || character.category() == QChar::Separator_Space) {
++pos;
if (pos == initpos)
break;
character = characterAt(pos);
}
// Go to the start of the block when we're already at the start of the text
if (pos == initpos)
pos = cursor.block().position();
cursor.setPosition(pos, mode);
setTextCursor(cursor);
}
void BaseTextEditor::handleBackspaceKey()
{
QTextCursor cursor = textCursor();
const TextEditor::TabSettings &tabSettings = d->m_document->tabSettings();
if (tabSettings.m_autoIndent && autoBackspace(cursor))
if (!tabSettings.m_smartBackspace) {
cursor.deletePreviousChar();
return;
}
int positionInBlock = pos - currentBlock.position();
const QString blockText = currentBlock.text();
if (cursor.atBlockStart() || tabSettings.firstNonSpace(blockText) < positionInBlock) {
cursor.deletePreviousChar();
return;
}
int previousIndent = 0;
const int indent = tabSettings.columnAt(blockText, positionInBlock);
for (QTextBlock previousNonEmptyBlock = currentBlock.previous();
previousNonEmptyBlock.isValid();
previousNonEmptyBlock = previousNonEmptyBlock.previous()) {
QString previousNonEmptyBlockText = previousNonEmptyBlock.text();
if (previousNonEmptyBlockText.trimmed().isEmpty())
continue;
previousIndent = tabSettings.columnAt(previousNonEmptyBlockText,
tabSettings.firstNonSpace(previousNonEmptyBlockText));
if (previousIndent < indent) {
cursor.beginEditBlock();
cursor.setPosition(currentBlock.position(), QTextCursor::KeepAnchor);
cursor.insertText(tabSettings.indentationString(previousNonEmptyBlockText));
cursor.endEditBlock();
}
}
void BaseTextEditor::wheelEvent(QWheelEvent *e)
{
d->clearVisibleCollapsedBlock();
if (e->modifiers() & Qt::ControlModifier) {
const int delta = e->delta();
if (delta < 0)
zoomOut();
else if (delta > 0)
zoomIn();
return;
}
QPlainTextEdit::wheelEvent(e);
}
void BaseTextEditor::zoomIn(int range)
{
d->clearVisibleCollapsedBlock();
emit requestFontZoom(range*10);
}
void BaseTextEditor::zoomOut(int range)
{
zoomIn(-range);
}
void BaseTextEditor::zoomReset()
{
emit requestZoomReset();
}
bool BaseTextEditor::isElectricCharacter(const QChar &) const
{
return false;
}
QString BaseTextEditor::autoComplete(QTextCursor &cursor, const QString &text) const
Q_UNUSED(cursor);
Q_UNUSED(text);
return QString();
}
bool BaseTextEditor::autoBackspace(QTextCursor &cursor)
{
Q_UNUSED(cursor);
return false;
}
int BaseTextEditor::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor)
Q_UNUSED(cursor);
return 0;
}
void BaseTextEditor::indentBlock(QTextDocument *, QTextBlock, QChar)
{
}
void BaseTextEditor::indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar)
{
if (cursor.hasSelection()) {
QTextBlock block = doc->findBlock(cursor.selectionStart());
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
do {
indentBlock(doc, block, typedChar);
block = block.next();
} while (block.isValid() && block != end);
} else {
indentBlock(doc, cursor.block(), typedChar);
}
}
void BaseTextEditor::reindent(QTextDocument *doc, const QTextCursor &cursor)
{
if (cursor.hasSelection()) {
QTextBlock block = doc->findBlock(cursor.selectionStart());
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
const TabSettings &ts = d->m_document->tabSettings();
// skip empty blocks
while (block.isValid() && block != end) {
QString bt = block.text();
if (ts.firstNonSpace(bt) < bt.size())
break;
indentBlock(doc, block, QChar::Null);
block = block.next();
}
int previousIndentation = ts.indentationColumn(block.text());
indentBlock(doc, block, QChar::Null);
int currentIndentation = ts.indentationColumn(block.text());
int delta = currentIndentation - previousIndentation;
block = block.next();
while (block.isValid() && block != end) {
ts.reindentLine(block, delta);
block = block.next();
}
} else {
indentBlock(doc, cursor.block(), QChar::Null);
}
}
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
BaseTextEditor::Link BaseTextEditor::findLinkAt(const QTextCursor &, bool)
{
return Link();
}
bool BaseTextEditor::openLink(const Link &link)
{
if (link.fileName.isEmpty())
return false;
if (baseTextDocument()->fileName() == link.fileName) {
Core::EditorManager *editorManager = Core::EditorManager::instance();
editorManager->addCurrentPositionToNavigationHistory();
gotoLine(link.line, link.column);
setFocus();
return true;
}
return openEditorAt(link.fileName, link.line, link.column);
}
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
void BaseTextEditor::updateLink(QMouseEvent *e)
{
bool linkFound = false;
if (d->m_mouseNavigationEnabled && e->modifiers() & Qt::ControlModifier) {
// Link emulation behaviour for 'go to definition'
const QTextCursor cursor = cursorForPosition(e->pos());
// Check that the mouse was actually on the text somewhere
bool onText = cursorRect(cursor).right() >= e->x();
if (!onText) {
QTextCursor nextPos = cursor;
nextPos.movePosition(QTextCursor::Right);
onText = cursorRect(nextPos).right() >= e->x();
}
const Link link = findLinkAt(cursor, false);
if (onText && link.isValid()) {
showLink(link);
linkFound = true;
}
}
if (!linkFound)
clearLink();
}
void BaseTextEditor::showLink(const Link &link)
{
if (d->m_currentLink == link)
return;
QTextEdit::ExtraSelection sel;
sel.cursor = textCursor();
sel.cursor.setPosition(link.pos);
sel.cursor.setPosition(link.pos + link.length, QTextCursor::KeepAnchor);
sel.format = d->m_linkFormat;
sel.format.setFontUnderline(true);
setExtraSelections(OtherSelection, QList<QTextEdit::ExtraSelection>() << sel);
viewport()->setCursor(Qt::PointingHandCursor);
d->m_currentLink = link;
d->m_linkPressed = false;
}
void BaseTextEditor::clearLink()
{
if (!d->m_currentLink.isValid())
return;
setExtraSelections(OtherSelection, QList<QTextEdit::ExtraSelection>());
viewport()->setCursor(Qt::IBeamCursor);
d->m_currentLink = Link();
d->m_linkPressed = false;
}
void BaseTextEditorPrivate::updateMarksBlock(const QTextBlock &block)
{
if (const TextBlockUserData *userData = TextEditDocumentLayout::testUserData(block))
foreach (ITextMark *mrk, userData->marks())
mrk->updateBlock(block);
}
void BaseTextEditorPrivate::updateMarksLineNumber()
{
QTextDocument *doc = q->document();
QTextBlock block = doc->begin();
int blockNumber = 0;
while (block.isValid()) {
if (const TextBlockUserData *userData = TextEditDocumentLayout::testUserData(block))
foreach (ITextMark *mrk, userData->marks()) {
mrk->updateLineNumber(blockNumber + 1);
}
block = block.next();
++blockNumber;
}
}
void BaseTextEditor::markBlocksAsChanged(QList<int> blockNumbers)
{
QTextBlock block = document()->begin();
while (block.isValid()) {
if (block.revision() < 0)
block.setRevision(-block.revision() - 1);
block = block.next();
}
foreach (const int blockNumber, blockNumbers) {
QTextBlock block = document()->findBlockByNumber(blockNumber);
if (block.isValid())
block.setRevision(-block.revision() - 1);
}
}
TextBlockUserData::MatchType TextBlockUserData::checkOpenParenthesis(QTextCursor *cursor, QChar c)
{
QTextBlock block = cursor->block();
if (!TextEditDocumentLayout::hasParentheses(block) || TextEditDocumentLayout::ifdefedOut(block))
Parentheses parenList = TextEditDocumentLayout::parentheses(block);
QTextBlock closedParenParag = block;
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
const int cursorPos = cursor->position() - closedParenParag.position();
int i = 0;
int ignore = 0;
bool foundOpen = false;
for (;;) {
if (!foundOpen) {
if (i >= parenList.count())
return NoMatch;
openParen = parenList.at(i);
if (openParen.pos != cursorPos) {
++i;
continue;
} else {
foundOpen = true;
++i;
}
}
if (i >= parenList.count()) {
for (;;) {
closedParenParag = closedParenParag.next();
if (!closedParenParag.isValid())
return NoMatch;
if (TextEditDocumentLayout::hasParentheses(closedParenParag)
&& !TextEditDocumentLayout::ifdefedOut(closedParenParag)) {
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
parenList = TextEditDocumentLayout::parentheses(closedParenParag);
break;
}
}
i = 0;
}
closedParen = parenList.at(i);
if (closedParen.type == Parenthesis::Opened) {
ignore++;
++i;
continue;
} else {
if (ignore > 0) {
ignore--;
++i;
continue;
}
cursor->clearSelection();
cursor->setPosition(closedParenParag.position() + closedParen.pos + 1, QTextCursor::KeepAnchor);
if ((c == QLatin1Char('{') && closedParen.chr != QLatin1Char('}'))
|| (c == QLatin1Char('(') && closedParen.chr != QLatin1Char(')'))
|| (c == QLatin1Char('[') && closedParen.chr != QLatin1Char(']'))
|| (c == QLatin1Char('+') && closedParen.chr != QLatin1Char('-'))
)
return Mismatch;
return Match;
}
}
}
TextBlockUserData::MatchType TextBlockUserData::checkClosedParenthesis(QTextCursor *cursor, QChar c)
{
QTextBlock block = cursor->block();
if (!TextEditDocumentLayout::hasParentheses(block) || TextEditDocumentLayout::ifdefedOut(block))
Parentheses parenList = TextEditDocumentLayout::parentheses(block);
QTextBlock openParenParag = block;
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
const int cursorPos = cursor->position() - openParenParag.position();
int i = parenList.count() - 1;
int ignore = 0;
bool foundClosed = false;
for (;;) {
if (!foundClosed) {
if (i < 0)
return NoMatch;
closedParen = parenList.at(i);
if (closedParen.pos != cursorPos - 1) {
--i;
continue;
} else {
foundClosed = true;
--i;
}
}
if (i < 0) {
for (;;) {
openParenParag = openParenParag.previous();
if (!openParenParag.isValid())
return NoMatch;
if (TextEditDocumentLayout::hasParentheses(openParenParag)
&& !TextEditDocumentLayout::ifdefedOut(openParenParag)) {
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
parenList = TextEditDocumentLayout::parentheses(openParenParag);
break;
}
}
i = parenList.count() - 1;
}
openParen = parenList.at(i);
if (openParen.type == Parenthesis::Closed) {
ignore++;
--i;
continue;
} else {
if (ignore > 0) {
ignore--;
--i;
continue;
}
cursor->clearSelection();
cursor->setPosition(openParenParag.position() + openParen.pos, QTextCursor::KeepAnchor);
if ((c == '}' && openParen.chr != '{') ||
(c == ')' && openParen.chr != '(') ||
(c == ']' && openParen.chr != '[') ||
(c == '-' && openParen.chr != '+'))
return Mismatch;
return Match;
}
}
}
bool TextBlockUserData::findPreviousOpenParenthesis(QTextCursor *cursor, bool select)
{
QTextBlock block = cursor->block();
int position = cursor->position();
int ignore = 0;
while (block.isValid()) {
Parentheses parenList = TextEditDocumentLayout::parentheses(block);
if (!parenList.isEmpty() && !TextEditDocumentLayout::ifdefedOut(block)) {
for (int i = parenList.count()-1; i >= 0; --i) {
Parenthesis paren = parenList.at(i);
if (block == cursor->block() &&
(position - block.position() <= paren.pos + (paren.type == Parenthesis::Closed ? 1 : 0)))
continue;
if (paren.type == Parenthesis::Closed) {
++ignore;
} else if (ignore > 0) {
--ignore;
} else {
cursor->setPosition(block.position() + paren.pos, select ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor);
return true;
}
}
}
block = block.previous();
}
return false;
}
bool TextBlockUserData::findPreviousBlockOpenParenthesis(QTextCursor *cursor, bool checkStartPosition)
{
QTextBlock block = cursor->block();
int position = cursor->position();
int ignore = 0;
while (block.isValid()) {
Parentheses parenList = TextEditDocumentLayout::parentheses(block);
if (!parenList.isEmpty() && !TextEditDocumentLayout::ifdefedOut(block)) {
for (int i = parenList.count()-1; i >= 0; --i) {
Parenthesis paren = parenList.at(i);
if (paren.chr != QLatin1Char('{') && paren.chr != QLatin1Char('}')
&& paren.chr != QLatin1Char('+') && paren.chr != QLatin1Char('-')
&& paren.chr != QLatin1Char('[') && paren.chr != QLatin1Char(']'))
if (block == cursor->block()) {
if (position - block.position() <= paren.pos + (paren.type == Parenthesis::Closed ? 1 : 0))
if (checkStartPosition && paren.type == Parenthesis::Opened && paren.pos== cursor->position()) {
if (paren.type == Parenthesis::Closed) {
++ignore;
} else if (ignore > 0) {
--ignore;
} else {
cursor->setPosition(block.position() + paren.pos);
return true;
}
}
}
}
return false;
}
bool TextBlockUserData::findNextClosingParenthesis(QTextCursor *cursor, bool select)
{
QTextBlock block = cursor->block();
int position = cursor->position();
int ignore = 0;
while (block.isValid()) {
Parentheses parenList = TextEditDocumentLayout::parentheses(block);
if (!parenList.isEmpty() && !TextEditDocumentLayout::ifdefedOut(block)) {
for (int i = 0; i < parenList.count(); ++i) {
Parenthesis paren = parenList.at(i);
if (block == cursor->block() &&
(position - block.position() > paren.pos - (paren.type == Parenthesis::Opened ? 1 : 0)))
continue;
if (paren.type == Parenthesis::Opened) {
++ignore;
} else if (ignore > 0) {
--ignore;
} else {
cursor->setPosition(block.position() + paren.pos+1, select ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor);
return true;
}
}
}
}
return false;
}
bool TextBlockUserData::findNextBlockClosingParenthesis(QTextCursor *cursor)
{
QTextBlock block = cursor->block();
int position = cursor->position();
int ignore = 0;
while (block.isValid()) {
Parentheses parenList = TextEditDocumentLayout::parentheses(block);
if (!parenList.isEmpty() && !TextEditDocumentLayout::ifdefedOut(block)) {
for (int i = 0; i < parenList.count(); ++i) {
Parenthesis paren = parenList.at(i);
if (paren.chr != QLatin1Char('{') && paren.chr != QLatin1Char('}')
&& paren.chr != QLatin1Char('+') && paren.chr != QLatin1Char('-')
&& paren.chr != QLatin1Char('[') && paren.chr != QLatin1Char(']'))
if (block == cursor->block() &&
(position - block.position() > paren.pos - (paren.type == Parenthesis::Opened ? 1 : 0)))
continue;
if (paren.type == Parenthesis::Opened) {
++ignore;
} else if (ignore > 0) {
--ignore;
} else {
cursor->setPosition(block.position() + paren.pos+1);
return true;
}
}
}
block = block.next();
}
return false;
}
TextBlockUserData::MatchType TextBlockUserData::matchCursorBackward(QTextCursor *cursor)
{
cursor->clearSelection();
const QTextBlock block = cursor->block();
if (!TextEditDocumentLayout::hasParentheses(block) || TextEditDocumentLayout::ifdefedOut(block))
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
return NoMatch;
const int relPos = cursor->position() - block.position();
Parentheses parentheses = TextEditDocumentLayout::parentheses(block);
const Parentheses::const_iterator cend = parentheses.constEnd();
for (Parentheses::const_iterator it = parentheses.constBegin();it != cend; ++it) {
const Parenthesis &paren = *it;
if (paren.pos == relPos - 1
&& paren.type == Parenthesis::Closed) {
return checkClosedParenthesis(cursor, paren.chr);
}
}
return NoMatch;
}
TextBlockUserData::MatchType TextBlockUserData::matchCursorForward(QTextCursor *cursor)
{
cursor->clearSelection();
const QTextBlock block = cursor->block();
if (!TextEditDocumentLayout::hasParentheses(block) || TextEditDocumentLayout::ifdefedOut(block))
return NoMatch;
const int relPos = cursor->position() - block.position();
Parentheses parentheses = TextEditDocumentLayout::parentheses(block);
const Parentheses::const_iterator cend = parentheses.constEnd();
for (Parentheses::const_iterator it = parentheses.constBegin();it != cend; ++it) {
const Parenthesis &paren = *it;
if (paren.pos == relPos
&& paren.type == Parenthesis::Opened) {