diff --git a/plugins/patchreview/patchhighlighter.cpp b/plugins/patchreview/patchhighlighter.cpp index e441e44d4..d6f0e5411 100644 --- a/plugins/patchreview/patchhighlighter.cpp +++ b/plugins/patchreview/patchhighlighter.cpp @@ -1,627 +1,692 @@ /*************************************************************************** Copyright 2006 David Nolden ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include "patchhighlighter.h" #include #include #include "patchreview.h" #include "debug.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace KDevelop; namespace { QPointer currentTooltip; KTextEditor::MovingRange* currentTooltipMark; QSize sizeHintForHtml( QString html, QSize maxSize ) { QTextDocument doc; doc.setHtml( html ); QSize ret; if( doc.idealWidth() > maxSize.width() ) { doc.setPageSize( QSize( maxSize.width(), 30 ) ); ret.setWidth( maxSize.width() ); }else{ ret.setWidth( doc.idealWidth() ); } ret.setHeight( doc.size().height() ); if( ret.height() > maxSize.height() ) ret.setHeight( maxSize.height() ); return ret; } } +const unsigned int PatchHighlighter::m_allmarks = + KTextEditor::MarkInterface::markType22 | KTextEditor::MarkInterface::markType23 | + KTextEditor::MarkInterface::markType24 | KTextEditor::MarkInterface::markType25 | + KTextEditor::MarkInterface::markType26 | KTextEditor::MarkInterface::markType27; + void PatchHighlighter::showToolTipForMark(const QPoint& pos, KTextEditor::MovingRange* markRange) { if( currentTooltipMark == markRange && currentTooltip ) return; delete currentTooltip; //Got the difference - Diff2::Difference* diff = m_differencesForRanges[markRange]; + Diff2::Difference* diff = m_ranges[markRange]; QString html; #if 0 if( diff->hasConflict() ) html += i18n( "Conflict
" ); #endif Diff2::DifferenceStringList lines; html += QLatin1String(""); if( diff->applied() ) { if( !m_plugin->patch()->isAlreadyApplied() ) html += i18n( "Applied.
" ); if( isInsertion( diff ) ) { html += i18n( "Insertion
" ); } else { if( isRemoval( diff ) ) html += i18n( "Removal
" ); html += i18n( "Previous:
" ); lines = diff->sourceLines(); } } else { if( m_plugin->patch()->isAlreadyApplied() ) html += i18n( "Reverted.
" ); if( isRemoval( diff ) ) { html += i18n( "Removal
" ); } else { if( isInsertion( diff ) ) html += i18n( "Insertion
" ); html += i18n( "Alternative:
" ); lines = diff->destinationLines(); } } html += QLatin1String("
"); for( int a = 0; a < lines.size(); ++a ) { Diff2::DifferenceString* line = lines[a]; uint currentPos = 0; QString string = line->string(); Diff2::MarkerList markers = line->markerList(); for( int b = 0; b < markers.size(); ++b ) { QString spanText = string.mid( currentPos, markers[b]->offset() - currentPos ).toHtmlEscaped(); if( markers[b]->type() == Diff2::Marker::End && ( currentPos != 0 || markers[b]->offset() != static_cast( string.size() ) ) ) { html += "" + spanText + ""; }else{ html += spanText; } currentPos = markers[b]->offset(); } html += string.mid( currentPos, string.length()-currentPos ).toHtmlEscaped(); html += QLatin1String("
"); } auto browser = new QTextBrowser; browser->setPalette( QApplication::palette() ); browser->setHtml( html ); int maxHeight = 500; browser->setMinimumSize( sizeHintForHtml( html, QSize( ( ICore::self()->uiController()->activeMainWindow()->width()*2 )/3, maxHeight ) ) ); browser->setMaximumSize( browser->minimumSize() + QSize( 10, 10 ) ); if( browser->minimumHeight() != maxHeight ) browser->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); QVBoxLayout* layout = new QVBoxLayout; layout->setMargin( 0 ); layout->addWidget( browser ); KDevelop::ActiveToolTip* tooltip = new KDevelop::ActiveToolTip( ICore::self()->uiController()->activeMainWindow(), pos + QPoint( 5, -browser->sizeHint().height() - 30 ) ); tooltip->setLayout( layout ); tooltip->resize( tooltip->sizeHint() + QSize( 10, 10 ) ); tooltip->move( pos - QPoint( 0, 20 + tooltip->height() ) ); tooltip->setHandleRect( QRect( pos - QPoint( 15, 15 ), pos + QPoint( 15, 15 ) ) ); currentTooltip = tooltip; currentTooltipMark = markRange; ActiveToolTip::showToolTip( tooltip ); } void PatchHighlighter::markClicked( KTextEditor::Document* doc, const KTextEditor::Mark& mark, bool& handled ) { - m_applying = true; - if( handled ) + if( handled || !(mark.type & m_allmarks) ) return; - handled = true; + auto range_diff = rangeForMark(mark); + m_applying = true; -// TODO: reconsider workaround -// if( doc->activeView() ) ///This is a workaround, if the cursor is somewhere else, the editor will always jump there when a mark was clicked -// doc->activeView()->setCursorPosition( KTextEditor::Cursor( mark.line, 0 ) ); + if (range_diff.first) { + handled = true; - KTextEditor::MovingRange* range = rangeForMark( mark ); + KTextEditor::MovingRange *&range = range_diff.first; + Diff2::Difference *&diff = range_diff.second; - if( range ) { QString currentText = doc->text( range->toRange() ); - Diff2::Difference* diff = m_differencesForRanges[range]; removeLineMarker( range ); QString sourceText; QString targetText; for( int a = 0; a < diff->sourceLineCount(); ++a ) { sourceText += diff->sourceLineAt( a )->string(); if( !sourceText.endsWith( '\n' ) ) sourceText += '\n'; } for( int a = 0; a < diff->destinationLineCount(); ++a ) { targetText += diff->destinationLineAt( a )->string(); if( !targetText.endsWith( '\n' ) ) targetText += '\n'; } - QString replace; - QString replaceWith; - - if( !diff->applied() ) { - replace = sourceText; - replaceWith = targetText; - }else { - replace = targetText; - replaceWith = sourceText; - } + bool applied = diff->applied(); + QString &replace(applied ? targetText : sourceText); + QString &replaceWith(applied ? sourceText : targetText); if( currentText.simplified() != replace.simplified() ) { KMessageBox::error( ICore::self()->uiController()->activeMainWindow(), i18n( "Could not apply the change: Text should be \"%1\", but is \"%2\".", replace, currentText ) ); + + m_applying = false; return; } - diff->apply( !diff->applied() ); + diff->apply(!applied); KTextEditor::Cursor start = range->start().toCursor(); range->document()->replaceText( range->toRange(), replaceWith ); uint replaceWithLines = replaceWith.count( '\n' ); KTextEditor::Range newRange( start, KTextEditor::Cursor(start.line() + replaceWithLines, start.column()) ); range->setRange( newRange ); addLineMarker( range, diff ); - } - { - // After applying the change, show the tooltip again, mainly to update an old tooltip - delete currentTooltip; - bool h = false; - markToolTipRequested( doc, mark, QCursor::pos(), h ); + { + // After applying the change, show the tooltip again, mainly to update an old tooltip + delete currentTooltip; + currentTooltip = nullptr; + bool h = false; + markToolTipRequested( doc, mark, QCursor::pos(), h ); + } } + m_applying = false; } -KTextEditor::MovingRange* PatchHighlighter::rangeForMark( const KTextEditor::Mark& mark ) { - for( QMap::const_iterator it = m_differencesForRanges.constBegin(); it != m_differencesForRanges.constEnd(); ++it ) { - if( it.key()->start().line() == mark.line ) - { - return it.key(); +QPair PatchHighlighter::rangeForMark( const KTextEditor::Mark &mark ) { + if (!m_applying) { + for( QMap::const_iterator it = m_ranges.constBegin(); it != m_ranges.constEnd(); ++it ) { + if (it.value() && it.key()->start().line() <= mark.line && mark.line <= it.key()->end().line()) { + return qMakePair(it.key(), it.value()); + } } } - return nullptr; + return qMakePair(nullptr, nullptr); } void PatchHighlighter::markToolTipRequested( KTextEditor::Document*, const KTextEditor::Mark& mark, QPoint pos, bool& handled ) { if( handled ) return; - handled = true; - - int myMarksPattern = KTextEditor::MarkInterface::markType22 | KTextEditor::MarkInterface::markType23 | KTextEditor::MarkInterface::markType24 | KTextEditor::MarkInterface::markType25 | KTextEditor::MarkInterface::markType26 | KTextEditor::MarkInterface::markType27; - if( mark.type & myMarksPattern ) { + if( mark.type & m_allmarks ) { //There is a mark in this line. Show the old text. - KTextEditor::MovingRange* range = rangeForMark( mark ); - if( range ) - showToolTipForMark( pos, range ); + auto range = rangeForMark(mark); + if( range.first ) { + showToolTipForMark( pos, range.first ); + handled = true; + } } } bool PatchHighlighter::isInsertion( Diff2::Difference* diff ) { return diff->sourceLineCount() == 0; } bool PatchHighlighter::isRemoval( Diff2::Difference* diff ) { return diff->destinationLineCount() == 0; } -QStringList PatchHighlighter::splitAndAddNewlines( const QString& text ) const { - QStringList result = text.split( '\n', QString::KeepEmptyParts ); - for( QStringList::iterator iter = result.begin(); iter != result.end(); ++iter ) { - iter->append( '\n' ); - } - if ( !result.isEmpty() ) { - QString & last = result.last(); - last.remove( last.size() - 1, 1 ); - } - return result; -} - void PatchHighlighter::performContentChange( KTextEditor::Document* doc, const QStringList& oldLines, const QStringList& newLines, int editLineNumber ) { QPair, QList > diffChange = m_model->linesChanged( oldLines, newLines, editLineNumber ); QList inserted = diffChange.first; QList removed = diffChange.second; + foreach(Diff2::Difference* d, removed) { + foreach(Diff2::DifferenceString* s, d->sourceLines()) + qCDebug(PLUGIN_PATCHREVIEW) << "removed source" << s->string(); + foreach(Diff2::DifferenceString* s, d->destinationLines()) + qCDebug(PLUGIN_PATCHREVIEW) << "removed destination" << s->string(); + } + foreach(Diff2::Difference* d, inserted) { + foreach(Diff2::DifferenceString* s, d->sourceLines()) + qCDebug(PLUGIN_PATCHREVIEW) << "inserted source" << s->string(); + foreach(Diff2::DifferenceString* s, d->destinationLines()) + qCDebug(PLUGIN_PATCHREVIEW) << "inserted destination" << s->string(); + } + // Remove all ranges that are in the same line (the line markers) - foreach( KTextEditor::MovingRange* r, m_differencesForRanges.keys() ) { - Diff2::Difference* diff = m_differencesForRanges[r]; - if ( removed.contains( diff ) ) { - removeLineMarker( r ); - m_ranges.remove( r ); - m_differencesForRanges.remove( r ); + for (auto it = m_ranges.begin(); it != m_ranges.end();) { + if (removed.contains(it.value())) { + KTextEditor::MovingRange* r = it.key(); + removeLineMarker(r); // is altering m_ranges + it = m_ranges.erase(it); + delete r; - delete diff; + } else { + ++it; } } + qDeleteAll(removed); KTextEditor::MovingInterface* moving = dynamic_cast( doc ); if ( !moving ) return; foreach( Diff2::Difference* diff, inserted ) { int lineStart = diff->destinationLineNumber(); if ( lineStart > 0 ) { --lineStart; } int lineEnd = diff->destinationLineEnd(); if ( lineEnd > 0 ) { --lineEnd; } KTextEditor::Range newRange( lineStart, 0, lineEnd, 0 ); KTextEditor::MovingRange * r = moving->newMovingRange( newRange ); - m_differencesForRanges[r] = diff; - m_ranges.insert( r ); + m_ranges[r] = diff; addLineMarker( r, diff ); } } void PatchHighlighter::textRemoved( KTextEditor::Document* doc, const KTextEditor::Range& range, const QString& oldText ) { if ( m_applying ) { // Do not interfere with patch application return; } qCDebug(PLUGIN_PATCHREVIEW) << "removal range" << range; qCDebug(PLUGIN_PATCHREVIEW) << "removed text" << oldText; - QStringList removedLines = splitAndAddNewlines( oldText ); - int startLine = range.start().line(); - QString remainingLine = doc->line( startLine ); - remainingLine += '\n'; - QString prefix = remainingLine.mid( 0, range.start().column() ); - QString suffix = remainingLine.mid( range.start().column() ); - if ( !removedLines.empty() ) { - removedLines.first() = prefix + removedLines.first(); - removedLines.last() = removedLines.last() + suffix; - } - performContentChange( doc, removedLines, QStringList() << remainingLine, startLine + 1 ); + + KTextEditor::Cursor cursor = range.start(); + int startLine = cursor.line(); + QStringList removedLines; + QStringList remainingLines; + if (startLine > 0) { + QString above = doc->line(--startLine); + removedLines << above; + remainingLines << above; + } + QString changed = doc->line(cursor.line()) + '\n'; + removedLines << changed.mid(0, cursor.column()) + oldText + changed.mid(cursor.column()); + remainingLines << changed; + if (doc->documentRange().end().line() > cursor.line()) { + QString below = doc->line(cursor.line() + 1); + removedLines << below; + remainingLines << below; + } + + performContentChange(doc, removedLines, remainingLines, startLine + 1); } -void PatchHighlighter::highlightFromScratch(KTextEditor::Document* doc) +void PatchHighlighter::newlineRemoved(KTextEditor::Document* doc, int line) { + if ( m_applying ) { // Do not interfere with patch application + return; + } + qCDebug(PLUGIN_PATCHREVIEW) << "remove newline" << line; + + KTextEditor::Cursor cursor = m_doc->cursorPosition(); + + int startLine = line - 1; + QStringList removedLines; + QStringList remainingLines; + if (startLine > 0) { + QString above = doc->line(--startLine); + removedLines << above; + remainingLines << above; + } + QString changed = doc->line(line - 1); + if (cursor.line() == line - 1) { + removedLines << changed.mid(0, cursor.column()); + removedLines << changed.mid(cursor.column()); + } else { + removedLines << changed; + removedLines << QString(); + } + remainingLines << changed; + if (doc->documentRange().end().line() >= line) { + QString below = doc->line(line); + removedLines << below; + remainingLines << below; + } + + performContentChange(doc, removedLines, remainingLines, startLine + 1); +} + +void PatchHighlighter::documentReloaded(KTextEditor::Document* doc) { qCDebug(PLUGIN_PATCHREVIEW) << "re-doing"; //The document was loaded / reloaded if ( !m_model->differences() ) return; KTextEditor::MovingInterface* moving = dynamic_cast( doc ); if ( !moving ) return; KTextEditor::MarkInterface* markIface = dynamic_cast( doc ); if( !markIface ) return; clear(); KColorScheme scheme( QPalette::Active ); QImage tintedInsertion = QIcon::fromTheme( QStringLiteral("insert-text") ).pixmap( 16, 16 ).toImage(); KIconEffect::colorize( tintedInsertion, scheme.foreground( KColorScheme::NegativeText ).color(), 1.0 ); QImage tintedRemoval = QIcon::fromTheme( QStringLiteral("edit-delete") ).pixmap( 16, 16 ).toImage(); KIconEffect::colorize( tintedRemoval, scheme.foreground( KColorScheme::NegativeText ).color(), 1.0 ); QImage tintedChange = QIcon::fromTheme( QStringLiteral("text-field") ).pixmap( 16, 16 ).toImage(); KIconEffect::colorize( tintedChange, scheme.foreground( KColorScheme::NegativeText ).color(), 1.0 ); markIface->setMarkDescription( KTextEditor::MarkInterface::markType22, i18n( "Insertion" ) ); markIface->setMarkPixmap( KTextEditor::MarkInterface::markType22, QPixmap::fromImage( tintedInsertion ) ); markIface->setMarkDescription( KTextEditor::MarkInterface::markType23, i18n( "Removal" ) ); markIface->setMarkPixmap( KTextEditor::MarkInterface::markType23, QPixmap::fromImage( tintedRemoval ) ); markIface->setMarkDescription( KTextEditor::MarkInterface::markType24, i18n( "Change" ) ); markIface->setMarkPixmap( KTextEditor::MarkInterface::markType24, QPixmap::fromImage( tintedChange ) ); markIface->setMarkDescription( KTextEditor::MarkInterface::markType25, i18n( "Insertion" ) ); markIface->setMarkPixmap( KTextEditor::MarkInterface::markType25, QIcon::fromTheme( QStringLiteral("insert-text") ).pixmap( 16, 16 ) ); markIface->setMarkDescription( KTextEditor::MarkInterface::markType26, i18n( "Removal" ) ); markIface->setMarkPixmap( KTextEditor::MarkInterface::markType26, QIcon::fromTheme( QStringLiteral("edit-delete") ).pixmap( 16, 16 ) ); markIface->setMarkDescription( KTextEditor::MarkInterface::markType27, i18n( "Change" ) ); markIface->setMarkPixmap( KTextEditor::MarkInterface::markType27, QIcon::fromTheme( QStringLiteral("text-field") ).pixmap( 16, 16 ) ); for ( Diff2::DifferenceList::const_iterator it = m_model->differences()->constBegin(); it != m_model->differences()->constEnd(); ++it ) { Diff2::Difference* diff = *it; int line, lineCount; Diff2::DifferenceStringList lines; if( diff->applied() ) { line = diff->destinationLineNumber(); lineCount = diff->destinationLineCount(); lines = diff->destinationLines(); } else { line = diff->sourceLineNumber(); lineCount = diff->sourceLineCount(); lines = diff->sourceLines(); } if ( line > 0 ) line -= 1; KTextEditor::Cursor c( line, 0 ); KTextEditor::Cursor endC( line + lineCount, 0 ); if ( doc->lines() <= c.line() ) c.setLine( doc->lines() - 1 ); if ( doc->lines() <= endC.line() ) endC.setLine( doc->lines() ); if ( endC.isValid() && c.isValid() ) { KTextEditor::MovingRange * r = moving->newMovingRange( KTextEditor::Range( c, endC ) ); - m_ranges << r; - - m_differencesForRanges[r] = *it; - + m_ranges[r] = diff; addLineMarker( r, diff ); } } } void PatchHighlighter::textInserted(KTextEditor::Document* doc, const KTextEditor::Cursor& cursor, const QString& text) { - KTextEditor::Range range(cursor, KTextEditor::Cursor(text.count('\n'), text.size()-text.lastIndexOf('\n')+1)); - if( range == doc->documentRange() ) { - highlightFromScratch(doc); - } else { - if ( m_applying ) { // Do not interfere with patch application - return; - } - qCDebug(PLUGIN_PATCHREVIEW) << "insertion range" << range; - QString text = doc->text( range ); - qCDebug(PLUGIN_PATCHREVIEW) << "inserted text" << text; - QStringList insertedLines = splitAndAddNewlines( text ); - int startLine = range.start().line(); - int endLine = range.end().line(); - QString prefix = doc->line( startLine ).mid( 0, range.start().column() ); - QString suffix = doc->line( endLine ).mid( range.end().column() ); - suffix += '\n'; - QString removedLine = prefix + suffix; - if ( !insertedLines.empty() ) { - insertedLines.first() = prefix + insertedLines.first(); - insertedLines.last() = insertedLines.last() + suffix; - } - performContentChange( doc, QStringList() << removedLine, insertedLines, startLine + 1 ); + if ( m_applying ) { // Do not interfere with patch application + return; } + + int startLine = cursor.line(); + int endColumn = cursor.column() + text.length(); + + qCDebug(PLUGIN_PATCHREVIEW) << "insertion range" << + KTextEditor::Range(cursor, KTextEditor::Cursor(startLine, endColumn)); + qCDebug(PLUGIN_PATCHREVIEW) << "inserted text" << text; + + QStringList removedLines; + QStringList insertedLines; + if (startLine > 0) { + QString above = doc->line(--startLine) + '\n'; + removedLines << above; + insertedLines << above; + } + QString changed = doc->line(cursor.line()) + '\n'; + removedLines << changed.mid(0, cursor.column()) + changed.mid(endColumn); + insertedLines << changed; + if (doc->documentRange().end().line() > cursor.line()) { + QString below = doc->line(cursor.line() + 1) + '\n'; + removedLines << below; + insertedLines << below; + } + + performContentChange(doc, removedLines, insertedLines, startLine + 1); +} + +void PatchHighlighter::newlineInserted(KTextEditor::Document* doc, const KTextEditor::Cursor& cursor) +{ + if ( m_applying ) { // Do not interfere with patch application + return; + } + qCDebug(PLUGIN_PATCHREVIEW) << "newline range" << + KTextEditor::Range(cursor, KTextEditor::Cursor(cursor.line() + 1, 0)); + + int startLine = cursor.line(); + QStringList removedLines; + QStringList insertedLines; + if (startLine > 0) { + QString above = doc->line(--startLine) + '\n'; + removedLines << above; + insertedLines << above; + } + insertedLines << QString('\n'); + if (doc->documentRange().end().line() > cursor.line()) { + QString below = doc->line(cursor.line() + 1) + '\n'; + removedLines << below; + insertedLines << below; + } + + performContentChange(doc, removedLines, insertedLines, startLine + 1); } PatchHighlighter::PatchHighlighter( Diff2::DiffModel* model, IDocument* kdoc, PatchReviewPlugin* plugin, bool updatePatchFromEdits ) throw( QString ) : m_doc( kdoc ), m_plugin( plugin ), m_model( model ), m_applying( false ) { KTextEditor::Document* doc = kdoc->textDocument(); // connect( kdoc, SIGNAL(destroyed(QObject*)), this, SLOT(documentDestroyed()) ); if (updatePatchFromEdits) { connect(doc, &KTextEditor::Document::textInserted, this, &PatchHighlighter::textInserted); + connect(doc, &KTextEditor::Document::lineWrapped, this, &PatchHighlighter::newlineInserted); connect(doc, &KTextEditor::Document::textRemoved, this, &PatchHighlighter::textRemoved); + connect(doc, &KTextEditor::Document::lineUnwrapped, this, &PatchHighlighter::newlineRemoved); } + connect(doc, &KTextEditor::Document::reloaded, this, &PatchHighlighter::documentReloaded); connect(doc, &KTextEditor::Document::destroyed, this, &PatchHighlighter::documentDestroyed); if ( doc->lines() == 0 ) return; if (qobject_cast(doc)) { //can't use new signal/slot syntax here, MarkInterface is not a QObject connect(doc, SIGNAL(markToolTipRequested(KTextEditor::Document*,KTextEditor::Mark,QPoint,bool&)), this, SLOT(markToolTipRequested(KTextEditor::Document*,KTextEditor::Mark,QPoint,bool&))); connect(doc, SIGNAL(markClicked(KTextEditor::Document*,KTextEditor::Mark,bool&)), this, SLOT(markClicked(KTextEditor::Document*,KTextEditor::Mark,bool&))); } if (qobject_cast(doc)) { //can't use new signal/slot syntax here, MovingInterface is not a QObject connect(doc, SIGNAL(aboutToDeleteMovingInterfaceContent(KTextEditor::Document*)), this, SLOT(aboutToDeleteMovingInterfaceContent(KTextEditor::Document*))); connect(doc, SIGNAL(aboutToInvalidateMovingInterfaceContent(KTextEditor::Document*)), this, SLOT(aboutToDeleteMovingInterfaceContent(KTextEditor::Document*))); } - highlightFromScratch(doc); + documentReloaded(doc); } void PatchHighlighter::removeLineMarker( KTextEditor::MovingRange* range ) { KTextEditor::MovingInterface* moving = dynamic_cast( range->document() ); if ( !moving ) return; KTextEditor::MarkInterface* markIface = dynamic_cast( range->document() ); if( !markIface ) return; - markIface->removeMark( range->start().line(), KTextEditor::MarkInterface::markType22 ); - markIface->removeMark( range->start().line(), KTextEditor::MarkInterface::markType23 ); - markIface->removeMark( range->start().line(), KTextEditor::MarkInterface::markType24 ); - markIface->removeMark( range->start().line(), KTextEditor::MarkInterface::markType25 ); - markIface->removeMark( range->start().line(), KTextEditor::MarkInterface::markType26 ); - markIface->removeMark( range->start().line(), KTextEditor::MarkInterface::markType27 ); + for (int line = range->start().line(); line <= range->end().line(); ++line) { + markIface->removeMark(line, m_allmarks); + } // Remove all ranges that are in the same line (the line markers) - foreach( KTextEditor::MovingRange* r, m_ranges ) - { - if( r != range && range->contains( r->toRange() ) ) - { - delete r; - m_ranges.remove( r ); - m_differencesForRanges.remove( r ); + for (auto it = m_ranges.begin(); it != m_ranges.end();) { + if (it.key() != range && range->overlaps(it.key()->toRange())) { + delete it.key(); + it = m_ranges.erase(it); + } else { + ++it; } } } void PatchHighlighter::addLineMarker( KTextEditor::MovingRange* range, Diff2::Difference* diff ) { KTextEditor::MovingInterface* moving = dynamic_cast( range->document() ); if ( !moving ) return; KTextEditor::MarkInterface* markIface = dynamic_cast( range->document() ); if( !markIface ) return; KTextEditor::Attribute::Ptr t( new KTextEditor::Attribute() ); bool isOriginalState = diff->applied() == m_plugin->patch()->isAlreadyApplied(); if( isOriginalState ) { t->setProperty( QTextFormat::BackgroundBrush, QBrush( ColorCache::self()->blendBackground( QColor( 0, 255, 255 ), 20 ) ) ); }else{ t->setProperty( QTextFormat::BackgroundBrush, QBrush( ColorCache::self()->blendBackground( QColor( 255, 0, 255 ), 20 ) ) ); } range->setAttribute( t ); range->setZDepth( -500 ); KTextEditor::MarkInterface::MarkTypes mark; if( isOriginalState ) { mark = KTextEditor::MarkInterface::markType27; if( isInsertion( diff ) ) mark = KTextEditor::MarkInterface::markType25; if( isRemoval( diff ) ) mark = KTextEditor::MarkInterface::markType26; }else{ mark = KTextEditor::MarkInterface::markType24; if( isInsertion( diff ) ) mark = KTextEditor::MarkInterface::markType22; if( isRemoval( diff ) ) mark = KTextEditor::MarkInterface::markType23; } markIface->addMark( range->start().line(), mark ); Diff2::DifferenceStringList lines; if( diff->applied() ) lines = diff->destinationLines(); else lines = diff->sourceLines(); for( int a = 0; a < lines.size(); ++a ) { Diff2::DifferenceString* line = lines[a]; int currentPos = 0; QString string = line->string(); Diff2::MarkerList markers = line->markerList(); for( int b = 0; b < markers.size(); ++b ) { if( markers[b]->type() == Diff2::Marker::End ) { if( currentPos != 0 || markers[b]->offset() != static_cast( string.size() ) ) { KTextEditor::MovingRange * r2 = moving->newMovingRange( KTextEditor::Range( KTextEditor::Cursor( a + range->start().line(), currentPos ), KTextEditor::Cursor( a + range->start().line(), markers[b]->offset() ) ) ); - m_ranges << r2; + m_ranges[r2] = nullptr; KTextEditor::Attribute::Ptr t( new KTextEditor::Attribute() ); t->setProperty( QTextFormat::BackgroundBrush, QBrush( ColorCache::self()->blendBackground( QColor( 255, 0, 0 ), 70 ) ) ); r2->setAttribute( t ); r2->setZDepth( -600 ); } } currentPos = markers[b]->offset(); } } } void PatchHighlighter::clear() { if( m_ranges.empty() ) return; KTextEditor::MovingInterface* moving = dynamic_cast( m_doc->textDocument() ); if ( !moving ) return; KTextEditor::MarkInterface* markIface = dynamic_cast( m_doc->textDocument() ); if( !markIface ) return; - QHash marks = markIface->marks(); - foreach( int line, marks.keys() ) { - markIface->removeMark( line, KTextEditor::MarkInterface::markType22 ); - markIface->removeMark( line, KTextEditor::MarkInterface::markType23 ); - markIface->removeMark( line, KTextEditor::MarkInterface::markType24 ); - markIface->removeMark( line, KTextEditor::MarkInterface::markType25 ); - markIface->removeMark( line, KTextEditor::MarkInterface::markType26 ); - markIface->removeMark( line, KTextEditor::MarkInterface::markType27 ); + foreach( int line, markIface->marks().keys() ) { + markIface->removeMark( line, m_allmarks ); } - qDeleteAll( m_ranges ); + // Diff is taking care of its own objects (except removed ones) + qDeleteAll( m_ranges.keys() ); m_ranges.clear(); - m_differencesForRanges.clear(); } PatchHighlighter::~PatchHighlighter() { clear(); } IDocument* PatchHighlighter::doc() { return m_doc; } void PatchHighlighter::documentDestroyed() { qCDebug(PLUGIN_PATCHREVIEW) << "document destroyed"; m_ranges.clear(); - m_differencesForRanges.clear(); } void PatchHighlighter::aboutToDeleteMovingInterfaceContent( KTextEditor::Document* ) { qCDebug(PLUGIN_PATCHREVIEW) << "about to delete"; clear(); } QList< KTextEditor::MovingRange* > PatchHighlighter::ranges() const { - return m_differencesForRanges.keys(); + return m_ranges.keys(); } diff --git a/plugins/patchreview/patchhighlighter.h b/plugins/patchreview/patchhighlighter.h index b3341a925..a2ce62b4d 100644 --- a/plugins/patchreview/patchhighlighter.h +++ b/plugins/patchreview/patchhighlighter.h @@ -1,84 +1,85 @@ /*************************************************************************** Copyright 2006 David Nolden ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #ifndef KDEVPLATFORM_PLUGIN_PATCHHIGHLIGHTER_H #define KDEVPLATFORM_PLUGIN_PATCHHIGHLIGHTER_H +#include #include +#include #include #include -#include #include namespace Diff2 { class Difference; class DiffModel; } class PatchReviewPlugin; namespace KDevelop { class IDocument; } namespace KTextEditor { class Document; class Range; class Cursor; class Mark; class MovingRange; } ///Delete itself when the document(or textDocument), or Diff-Model is deleted. class PatchHighlighter : public QObject { Q_OBJECT public: PatchHighlighter( Diff2::DiffModel* model, KDevelop::IDocument* doc, PatchReviewPlugin* plugin, bool updatePatchFromEdits ) throw( QString ); ~PatchHighlighter() override; KDevelop::IDocument* doc(); QList< KTextEditor::MovingRange* > ranges() const; private Q_SLOTS: + void documentReloaded( KTextEditor::Document* ); void documentDestroyed(); void aboutToDeleteMovingInterfaceContent( KTextEditor::Document* ); private: - void highlightFromScratch(KTextEditor::Document* doc); - void addLineMarker( KTextEditor::MovingRange* arg1, Diff2::Difference* arg2 ); void removeLineMarker( KTextEditor::MovingRange* range ); - QStringList splitAndAddNewlines( const QString& text ) const; void performContentChange( KTextEditor::Document* doc, const QStringList& oldLines, const QStringList& newLines, int editLineNumber ); - KTextEditor::MovingRange* rangeForMark(const KTextEditor::Mark& mark); + QPair rangeForMark(const KTextEditor::Mark &mark); void clear(); - QSet< KTextEditor::MovingRange* > m_ranges; - QMap< KTextEditor::MovingRange*, Diff2::Difference* > m_differencesForRanges; + QMap< KTextEditor::MovingRange*, Diff2::Difference* > m_ranges; KDevelop::IDocument* m_doc; PatchReviewPlugin* m_plugin; Diff2::DiffModel* m_model; bool m_applying; + static const unsigned int m_allmarks; public Q_SLOTS: void markToolTipRequested( KTextEditor::Document*, const KTextEditor::Mark&, QPoint, bool & ); void showToolTipForMark(const QPoint& arg1, KTextEditor::MovingRange* arg2); bool isRemoval( Diff2::Difference* ); bool isInsertion( Diff2::Difference* ); void markClicked( KTextEditor::Document*, const KTextEditor::Mark&, bool& ); void textInserted(KTextEditor::Document* doc, const KTextEditor::Cursor& cursor, const QString& text); + void newlineInserted(KTextEditor::Document* doc, const KTextEditor::Cursor& cursor); void textRemoved( KTextEditor::Document*, const KTextEditor::Range&, const QString& oldText ); + void newlineRemoved(KTextEditor::Document*, int line); }; #endif