From a645f78cd69860b57efba4c0a528fec234367327 Mon Sep 17 00:00:00 2001 From: Francois Ferrand Date: Tue, 21 Oct 2014 17:50:10 +0200 Subject: [PATCH] Add API in texteditor to let plugins contribute extra selections. Some plugins use extra selections to provide additional editor annotations: code coverage, spelling mistake... This was possible for C++ editor in previous version, using CppModelManagerInterface::setExtraDiagnostics(), but this API has been removed. This commits adds alternative API directly in the editor, allowing to pass a Core::Id instead of the enum value. Change-Id: I3040bd144d6fe0876d861dd28e832729bd5d6602 Reviewed-by: hjk --- src/plugins/texteditor/texteditor.cpp | 71 ++++++++++++++++++--------- src/plugins/texteditor/texteditor.h | 2 + 2 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 06c0a4edeb4..ac22c74c652 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -391,7 +391,8 @@ public: void highlightSearchResults(const QTextBlock &block, TextEditorOverlay *overlay); QTimer m_delayedUpdateTimer; - QList m_extraSelections[TextEditorWidget::NExtraSelectionKinds]; + void setExtraSelections(int kind, const QList &selections); + QHash> m_extraSelections; // block selection mode bool m_inBlockSelectionMode; @@ -521,6 +522,8 @@ TextEditorWidgetPrivate::TextEditorWidgetPrivate(TextEditorWidget *parent) m_cursorPositionLabelAction = m_toolBar->addWidget(m_cursorPositionLabel); m_fileEncodingLabelAction = m_toolBar->addWidget(m_fileEncodingLabel); + + m_extraSelections.reserve(TextEditorWidget::NExtraSelectionKinds); } } // namespace Internal @@ -2542,8 +2545,8 @@ void TextEditorWidgetPrivate::documentAboutToBeReloaded() // remove extra selections (loads of QTextCursor objects) - for (int i = 0; i < TextEditorWidget::NExtraSelectionKinds; ++i) - m_extraSelections[i].clear(); + m_extraSelections.clear(); + m_extraSelections.reserve(TextEditorWidget::NExtraSelectionKinds); q->QPlainTextEdit::setExtraSelections(QList()); // clear all overlays @@ -5815,53 +5818,73 @@ void TextEditorWidget::deleteStartOfWordCamelCase() setTextCursor(c); } -void TextEditorWidget::setExtraSelections(ExtraSelectionKind kind, const QList &selections) +// kind can be either a value from the ExtraSelectionKind enum, or an unique Core::Id identifier. +void TextEditorWidgetPrivate::setExtraSelections(int kind, const QList &selections) { - if (selections.isEmpty() && d->m_extraSelections[kind].isEmpty()) + if (selections.isEmpty() && m_extraSelections[kind].isEmpty()) return; - d->m_extraSelections[kind] = selections; + m_extraSelections[kind] = selections; - if (kind == CodeSemanticsSelection) { - d->m_overlay->clear(); - foreach (const QTextEdit::ExtraSelection &selection, d->m_extraSelections[kind]) { - d->m_overlay->addOverlaySelection(selection.cursor, + if (kind == TextEditorWidget::CodeSemanticsSelection) { + m_overlay->clear(); + foreach (const QTextEdit::ExtraSelection &selection, m_extraSelections[kind]) { + m_overlay->addOverlaySelection(selection.cursor, selection.format.background().color(), selection.format.background().color(), TextEditorOverlay::LockSize); } - d->m_overlay->setVisible(!d->m_overlay->isEmpty()); - } else if (kind == SnippetPlaceholderSelection) { - d->m_snippetOverlay->mangle(); - d->m_snippetOverlay->clear(); - foreach (const QTextEdit::ExtraSelection &selection, d->m_extraSelections[kind]) { - d->m_snippetOverlay->addOverlaySelection(selection.cursor, + m_overlay->setVisible(!m_overlay->isEmpty()); + } else if (kind == TextEditorWidget::SnippetPlaceholderSelection) { + m_snippetOverlay->mangle(); + m_snippetOverlay->clear(); + foreach (const QTextEdit::ExtraSelection &selection, m_extraSelections[kind]) { + m_snippetOverlay->addOverlaySelection(selection.cursor, selection.format.background().color(), selection.format.background().color(), TextEditorOverlay::ExpandBegin); } - d->m_snippetOverlay->mapEquivalentSelections(); - d->m_snippetOverlay->setVisible(!d->m_snippetOverlay->isEmpty()); + m_snippetOverlay->mapEquivalentSelections(); + m_snippetOverlay->setVisible(!m_snippetOverlay->isEmpty()); } else { QList all; - for (int i = 0; i < NExtraSelectionKinds; ++i) { - if (i == CodeSemanticsSelection || i == SnippetPlaceholderSelection) + for (auto i = m_extraSelections.constBegin(); i != m_extraSelections.constEnd(); ++i) { + if (i.key() == TextEditorWidget::CodeSemanticsSelection + || i.key() == TextEditorWidget::SnippetPlaceholderSelection) continue; - all += d->m_extraSelections[i]; + all += i.value(); } - QPlainTextEdit::setExtraSelections(all); + q->QPlainTextEdit::setExtraSelections(all); } } +void TextEditorWidget::setExtraSelections(ExtraSelectionKind kind, const QList &selections) +{ + d->setExtraSelections(kind, selections); +} + QList TextEditorWidget::extraSelections(ExtraSelectionKind kind) const { return d->m_extraSelections[kind]; } +void TextEditorWidget::setExtraSelections(Core::Id kind, const QList &selections) +{ + // Private Core:Id identifiers from the 0-1000 range cannot be used here, they conflict with ExtraSelectionKind + QTC_ASSERT(kind.uniqueIdentifier() >= NExtraSelectionKinds, return); + d->setExtraSelections(kind.uniqueIdentifier(), selections); +} + +QList TextEditorWidget::extraSelections(Core::Id kind) const +{ + // Private Core:Id identifiers from the 0-1000 range cannot be used here, they conflict with ExtraSelectionKind + QTC_ASSERT(kind.uniqueIdentifier() >= NExtraSelectionKinds, return QList()); + return d->m_extraSelections[kind.uniqueIdentifier()]; +} + QString TextEditorWidget::extraSelectionTooltip(int pos) const { QList all; - for (int i = 0; i < NExtraSelectionKinds; ++i) { - const QList &sel = d->m_extraSelections[i]; + foreach (const QList &sel, d->m_extraSelections) { for (int j = 0; j < sel.size(); ++j) { const QTextEdit::ExtraSelection &s = sel.at(j); if (s.cursor.selectionStart() <= pos diff --git a/src/plugins/texteditor/texteditor.h b/src/plugins/texteditor/texteditor.h index 9c1f833814e..aa3c1ff6fc9 100644 --- a/src/plugins/texteditor/texteditor.h +++ b/src/plugins/texteditor/texteditor.h @@ -357,6 +357,8 @@ public: }; void setExtraSelections(ExtraSelectionKind kind, const QList &selections); QList extraSelections(ExtraSelectionKind kind) const; + void setExtraSelections(Core::Id kind, const QList &selections); + QList extraSelections(Core::Id kind) const; QString extraSelectionTooltip(int pos) const; RefactorMarkers refactorMarkers() const;