TextEditor: move snippet overlay into own cpp file

Change-Id: I3343d9abf19e4edc7bd88077bf8fe6666a901e1b
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2021-04-22 10:59:32 +02:00
parent 7880950eca
commit ec2449cae4
8 changed files with 176 additions and 100 deletions

View File

@@ -81,6 +81,7 @@ add_qtc_plugin(TextEditor
snippets/snippet.cpp snippets/snippet.h
snippets/snippetassistcollector.cpp snippets/snippetassistcollector.h
snippets/snippeteditor.cpp snippets/snippeteditor.h
snippets/snippetoverlay.cpp snippets/snippetoverlay.h
snippets/snippetprovider.cpp snippets/snippetprovider.h
snippets/snippetscollection.cpp snippets/snippetscollection.h
snippets/snippetssettings.cpp snippets/snippetssettings.h

View File

@@ -0,0 +1,112 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "snippetoverlay.h"
#include "snippet.h"
namespace TextEditor {
namespace Internal {
void SnippetOverlay::clear()
{
TextEditorOverlay::clear();
m_equivalentSelections.clear();
m_manglers.clear();
}
void SnippetOverlay::mapEquivalentSelections()
{
m_equivalentSelections.clear();
m_equivalentSelections.resize(selections().size());
QMultiMap<QString, int> all;
for (int i = 0; i < selections().size(); ++i)
all.insert(selectionText(i).toLower(), i);
const QList<QString> &uniqueKeys = all.uniqueKeys();
foreach (const QString &key, uniqueKeys) {
QList<int> indexes;
const auto cAll = all;
QMultiMap<QString, int>::const_iterator lbit = cAll.lowerBound(key);
QMultiMap<QString, int>::const_iterator ubit = cAll.upperBound(key);
while (lbit != ubit) {
indexes.append(lbit.value());
++lbit;
}
foreach (int index, indexes)
m_equivalentSelections[index] = indexes;
}
}
void SnippetOverlay::updateEquivalentSelections(const QTextCursor &cursor)
{
int selectionIndex = selectionIndexForCursor(cursor);
if (selectionIndex == -1)
return;
const QString &currentText = selectionText(selectionIndex);
const QList<int> &equivalents = m_equivalentSelections.at(selectionIndex);
foreach (int i, equivalents) {
if (i == selectionIndex)
continue;
const QString &equivalentText = selectionText(i);
if (currentText != equivalentText) {
QTextCursor selectionCursor = assembleCursorForSelection(i);
selectionCursor.joinPreviousEditBlock();
selectionCursor.removeSelectedText();
selectionCursor.insertText(currentText);
selectionCursor.endEditBlock();
}
}
}
void SnippetOverlay::setNameMangler(const QList<NameMangler *> &manglers)
{
m_manglers = manglers;
}
void SnippetOverlay::mangle()
{
for (int i = 0; i < m_manglers.count(); ++i) {
if (!m_manglers.at(i))
continue;
const QString current = selectionText(i);
const QString result = m_manglers.at(i)->mangle(current);
if (result != current) {
QTextCursor selectionCursor = assembleCursorForSelection(i);
selectionCursor.joinPreviousEditBlock();
selectionCursor.removeSelectedText();
selectionCursor.insertText(result);
selectionCursor.endEditBlock();
}
}
}
} // namespace Internal
} // namespace TextEditor

View File

@@ -0,0 +1,54 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "texteditor/texteditoroverlay.h"
namespace TextEditor {
class NameMangler;
namespace Internal {
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;
QList<NameMangler *> m_manglers;
};
} // namespace Internal
} // namespace TextEditor

View File

@@ -42,6 +42,7 @@
#include "icodestylepreferences.h"
#include "refactoroverlay.h"
#include "snippets/snippet.h"
#include "snippets/snippetoverlay.h"
#include "storagesettings.h"
#include "syntaxhighlighter.h"
#include "tabsettings.h"

View File

@@ -97,7 +97,8 @@ SOURCES += texteditorplugin.cpp \
commentssettings.cpp \
marginsettings.cpp \
formattexteditor.cpp \
command.cpp
command.cpp \
snippets/snippetoverlay.cpp
HEADERS += texteditorplugin.h \
plaintexteditorfactory.h \
@@ -192,7 +193,8 @@ HEADERS += texteditorplugin.h \
formattexteditor.h \
command.h \
indenter.h \
formatter.h
formatter.h \
snippets/snippetoverlay.h
FORMS += \
displaysettingspage.ui \

View File

@@ -205,8 +205,6 @@ Project {
name: "Snippets"
prefix: "snippets/"
files: [
"snippetprovider.cpp",
"snippetprovider.h",
"reuse.h",
"snippet.cpp",
"snippet.h",
@@ -214,6 +212,10 @@ Project {
"snippetassistcollector.h",
"snippeteditor.cpp",
"snippeteditor.h",
"snippetoverlay.cpp",
"snippetoverlay.h",
"snippetprovider.cpp",
"snippetprovider.h",
"snippetscollection.cpp",
"snippetscollection.h",
"snippetssettings.cpp",

View File

@@ -25,7 +25,6 @@
#include "texteditoroverlay.h"
#include "texteditor.h"
#include "snippets/snippet.h"
#include <QDebug>
#include <QMap>
@@ -485,80 +484,3 @@ bool TextEditorOverlay::hasFirstSelectionBeginMoved() const
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.resize(selections().size());
QMultiMap<QString, int> all;
for (int i = 0; i < selections().size(); ++i)
all.insert(selectionText(i).toLower(), i);
const QList<QString> &uniqueKeys = all.uniqueKeys();
foreach (const QString &key, uniqueKeys) {
QList<int> indexes;
const auto cAll = all;
QMultiMap<QString, int>::const_iterator lbit = cAll.lowerBound(key);
QMultiMap<QString, int>::const_iterator ubit = cAll.upperBound(key);
while (lbit != ubit) {
indexes.append(lbit.value());
++lbit;
}
foreach (int index, indexes)
m_equivalentSelections[index] = indexes;
}
}
void SnippetOverlay::updateEquivalentSelections(const QTextCursor &cursor)
{
int selectionIndex = selectionIndexForCursor(cursor);
if (selectionIndex == -1)
return;
const QString &currentText = selectionText(selectionIndex);
const QList<int> &equivalents = m_equivalentSelections.at(selectionIndex);
foreach (int i, equivalents) {
if (i == selectionIndex)
continue;
const QString &equivalentText = selectionText(i);
if (currentText != equivalentText) {
QTextCursor selectionCursor = assembleCursorForSelection(i);
selectionCursor.joinPreviousEditBlock();
selectionCursor.removeSelectedText();
selectionCursor.insertText(currentText);
selectionCursor.endEditBlock();
}
}
}
void SnippetOverlay::setNameMangler(const QList<NameMangler *> &manglers)
{
m_manglers = manglers;
}
void SnippetOverlay::mangle()
{
for (int i = 0; i < m_manglers.count(); ++i) {
if (!m_manglers.at(i))
continue;
const QString current = selectionText(i);
const QString result = m_manglers.at(i)->mangle(current);
if (result != current) {
QTextCursor selectionCursor = assembleCursorForSelection(i);
selectionCursor.joinPreviousEditBlock();
selectionCursor.removeSelectedText();
selectionCursor.insertText(result);
selectionCursor.endEditBlock();
}
}
}

View File

@@ -35,7 +35,6 @@ QT_FORWARD_DECLARE_CLASS(QWidget)
QT_FORWARD_DECLARE_CLASS(QPainterPath)
namespace TextEditor {
class NameMangler;
class TextEditorWidget;
namespace Internal {
@@ -117,22 +116,5 @@ private:
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;
QList<NameMangler *> m_manglers;
};
} // namespace Internal
} // namespace TextEditor