Editor: add specialized snippet overlay

Split out the snippet functionality into a new derived
overlay implementation.

Change-Id: I2d7fffabe16ce6348ed067eb52ff221420a23285
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2021-04-16 09:35:46 +02:00
parent 8b59604bd4
commit b3c2120dff
3 changed files with 43 additions and 26 deletions

View File

@@ -665,7 +665,7 @@ public:
int extraAreaPreviousMarkTooltipRequestedLine = -1; int extraAreaPreviousMarkTooltipRequestedLine = -1;
TextEditorOverlay *m_overlay = nullptr; TextEditorOverlay *m_overlay = nullptr;
TextEditorOverlay *m_snippetOverlay = nullptr; SnippetOverlay *m_snippetOverlay = nullptr;
TextEditorOverlay *m_searchResultOverlay = nullptr; TextEditorOverlay *m_searchResultOverlay = nullptr;
bool snippetCheckCursor(const QTextCursor &cursor); bool snippetCheckCursor(const QTextCursor &cursor);
void snippetTabOrBacktab(bool forward); void snippetTabOrBacktab(bool forward);
@@ -1004,7 +1004,7 @@ void TextEditorWidgetPrivate::ctor(const QSharedPointer<TextDocument> &doc)
q->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); q->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
m_overlay = new TextEditorOverlay(q); m_overlay = new TextEditorOverlay(q);
m_snippetOverlay = new TextEditorOverlay(q); m_snippetOverlay = new SnippetOverlay(q);
m_searchResultOverlay = new TextEditorOverlay(q); m_searchResultOverlay = new TextEditorOverlay(q);
m_refactorOverlay = new RefactorOverlay(q); m_refactorOverlay = new RefactorOverlay(q);

View File

@@ -72,8 +72,6 @@ void TextEditorOverlay::clear()
return; return;
m_selections.clear(); m_selections.clear();
m_firstSelectionOriginalBegin = -1; m_firstSelectionOriginalBegin = -1;
m_equivalentSelections.clear();
m_manglers.clear();
update(); update();
} }
@@ -481,13 +479,27 @@ QTextCursor TextEditorOverlay::assembleCursorForSelection(int selectionIndex) co
return cursor; return cursor;
} }
void TextEditorOverlay::mapEquivalentSelections() bool TextEditorOverlay::hasFirstSelectionBeginMoved() const
{
if (m_firstSelectionOriginalBegin == -1 || m_selections.isEmpty())
return false;
return m_selections.at(0).m_cursor_begin.position() != m_firstSelectionOriginalBegin;
}
void SnippetOverlay::clear()
{
TextEditorOverlay::clear();
m_equivalentSelections.clear();
m_manglers.clear();
}
void SnippetOverlay::mapEquivalentSelections()
{ {
m_equivalentSelections.clear(); m_equivalentSelections.clear();
m_equivalentSelections.resize(m_selections.size()); m_equivalentSelections.resize(selections().size());
QMultiMap<QString, int> all; QMultiMap<QString, int> all;
for (int i = 0; i < m_selections.size(); ++i) for (int i = 0; i < selections().size(); ++i)
all.insert(selectionText(i).toLower(), i); all.insert(selectionText(i).toLower(), i);
const QList<QString> &uniqueKeys = all.uniqueKeys(); const QList<QString> &uniqueKeys = all.uniqueKeys();
@@ -506,7 +518,7 @@ void TextEditorOverlay::mapEquivalentSelections()
} }
} }
void TextEditorOverlay::updateEquivalentSelections(const QTextCursor &cursor) void SnippetOverlay::updateEquivalentSelections(const QTextCursor &cursor)
{ {
int selectionIndex = selectionIndexForCursor(cursor); int selectionIndex = selectionIndexForCursor(cursor);
if (selectionIndex == -1) if (selectionIndex == -1)
@@ -528,12 +540,12 @@ void TextEditorOverlay::updateEquivalentSelections(const QTextCursor &cursor)
} }
} }
void TextEditorOverlay::setNameMangler(const QList<NameMangler *> &manglers) void SnippetOverlay::setNameMangler(const QList<NameMangler *> &manglers)
{ {
m_manglers = manglers; m_manglers = manglers;
} }
void TextEditorOverlay::mangle() void SnippetOverlay::mangle()
{ {
for (int i = 0; i < m_manglers.count(); ++i) { for (int i = 0; i < m_manglers.count(); ++i) {
if (!m_manglers.at(i)) if (!m_manglers.at(i))
@@ -550,10 +562,3 @@ void TextEditorOverlay::mangle()
} }
} }
} }
bool TextEditorOverlay::hasFirstSelectionBeginMoved() const
{
if (m_firstSelectionOriginalBegin == -1 || m_selections.isEmpty())
return false;
return m_selections.at(0).m_cursor_begin.position() != m_firstSelectionOriginalBegin;
}

View File

@@ -74,7 +74,7 @@ public:
void setAlpha(bool enabled) { m_alpha = enabled; } void setAlpha(bool enabled) { m_alpha = enabled; }
void clear(); virtual void clear();
enum OverlaySelectionFlags { enum OverlaySelectionFlags {
LockSize = 1, LockSize = 1,
@@ -95,20 +95,17 @@ public:
bool hasCursorInSelection(const QTextCursor &cursor) const; bool hasCursorInSelection(const QTextCursor &cursor) const;
void mapEquivalentSelections();
void updateEquivalentSelections(const QTextCursor &cursor);
void setNameMangler(const QList<NameMangler *> &manglers);
void mangle();
bool hasFirstSelectionBeginMoved() const; bool hasFirstSelectionBeginMoved() const;
protected:
int selectionIndexForCursor(const QTextCursor &cursor) const;
QString selectionText(int selectionIndex) const;
QTextCursor assembleCursorForSelection(int selectionIndex) const;
private: private:
QPainterPath createSelectionPath(const QTextCursor &begin, const QTextCursor &end, const QRect& clip); QPainterPath createSelectionPath(const QTextCursor &begin, const QTextCursor &end, const QRect& clip);
void paintSelection(QPainter *painter, const OverlaySelection &selection); void paintSelection(QPainter *painter, const OverlaySelection &selection);
void fillSelection(QPainter *painter, const OverlaySelection &selection, const QColor &color); void fillSelection(QPainter *painter, const OverlaySelection &selection, const QColor &color);
int selectionIndexForCursor(const QTextCursor &cursor) const;
QString selectionText(int selectionIndex) const;
QTextCursor assembleCursorForSelection(int selectionIndex) const;
bool m_visible; bool m_visible;
bool m_alpha; bool m_alpha;
@@ -118,6 +115,21 @@ private:
TextEditorWidget *m_editor; TextEditorWidget *m_editor;
QWidget *m_viewport; QWidget *m_viewport;
QList<OverlaySelection> m_selections; QList<OverlaySelection> m_selections;
};
class SnippetOverlay : public TextEditorOverlay
{
public:
using TextEditorOverlay::TextEditorOverlay;
void clear() override;
void mapEquivalentSelections();
void updateEquivalentSelections(const QTextCursor &cursor);
void setNameMangler(const QList<NameMangler *> &manglers);
void mangle();
private:
QVector<QList<int> > m_equivalentSelections; QVector<QList<int> > m_equivalentSelections;
QList<NameMangler *> m_manglers; QList<NameMangler *> m_manglers;
}; };