forked from qt-creator/qt-creator
TextEditor: move snippet overlay into own cpp file
Change-Id: I3343d9abf19e4edc7bd88077bf8fe6666a901e1b Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -81,6 +81,7 @@ add_qtc_plugin(TextEditor
|
|||||||
snippets/snippet.cpp snippets/snippet.h
|
snippets/snippet.cpp snippets/snippet.h
|
||||||
snippets/snippetassistcollector.cpp snippets/snippetassistcollector.h
|
snippets/snippetassistcollector.cpp snippets/snippetassistcollector.h
|
||||||
snippets/snippeteditor.cpp snippets/snippeteditor.h
|
snippets/snippeteditor.cpp snippets/snippeteditor.h
|
||||||
|
snippets/snippetoverlay.cpp snippets/snippetoverlay.h
|
||||||
snippets/snippetprovider.cpp snippets/snippetprovider.h
|
snippets/snippetprovider.cpp snippets/snippetprovider.h
|
||||||
snippets/snippetscollection.cpp snippets/snippetscollection.h
|
snippets/snippetscollection.cpp snippets/snippetscollection.h
|
||||||
snippets/snippetssettings.cpp snippets/snippetssettings.h
|
snippets/snippetssettings.cpp snippets/snippetssettings.h
|
||||||
|
|||||||
112
src/plugins/texteditor/snippets/snippetoverlay.cpp
Normal file
112
src/plugins/texteditor/snippets/snippetoverlay.cpp
Normal 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 ¤tText = 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
|
||||||
54
src/plugins/texteditor/snippets/snippetoverlay.h
Normal file
54
src/plugins/texteditor/snippets/snippetoverlay.h
Normal 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
|
||||||
|
|
||||||
@@ -42,6 +42,7 @@
|
|||||||
#include "icodestylepreferences.h"
|
#include "icodestylepreferences.h"
|
||||||
#include "refactoroverlay.h"
|
#include "refactoroverlay.h"
|
||||||
#include "snippets/snippet.h"
|
#include "snippets/snippet.h"
|
||||||
|
#include "snippets/snippetoverlay.h"
|
||||||
#include "storagesettings.h"
|
#include "storagesettings.h"
|
||||||
#include "syntaxhighlighter.h"
|
#include "syntaxhighlighter.h"
|
||||||
#include "tabsettings.h"
|
#include "tabsettings.h"
|
||||||
|
|||||||
@@ -97,7 +97,8 @@ SOURCES += texteditorplugin.cpp \
|
|||||||
commentssettings.cpp \
|
commentssettings.cpp \
|
||||||
marginsettings.cpp \
|
marginsettings.cpp \
|
||||||
formattexteditor.cpp \
|
formattexteditor.cpp \
|
||||||
command.cpp
|
command.cpp \
|
||||||
|
snippets/snippetoverlay.cpp
|
||||||
|
|
||||||
HEADERS += texteditorplugin.h \
|
HEADERS += texteditorplugin.h \
|
||||||
plaintexteditorfactory.h \
|
plaintexteditorfactory.h \
|
||||||
@@ -192,7 +193,8 @@ HEADERS += texteditorplugin.h \
|
|||||||
formattexteditor.h \
|
formattexteditor.h \
|
||||||
command.h \
|
command.h \
|
||||||
indenter.h \
|
indenter.h \
|
||||||
formatter.h
|
formatter.h \
|
||||||
|
snippets/snippetoverlay.h
|
||||||
|
|
||||||
FORMS += \
|
FORMS += \
|
||||||
displaysettingspage.ui \
|
displaysettingspage.ui \
|
||||||
|
|||||||
@@ -205,8 +205,6 @@ Project {
|
|||||||
name: "Snippets"
|
name: "Snippets"
|
||||||
prefix: "snippets/"
|
prefix: "snippets/"
|
||||||
files: [
|
files: [
|
||||||
"snippetprovider.cpp",
|
|
||||||
"snippetprovider.h",
|
|
||||||
"reuse.h",
|
"reuse.h",
|
||||||
"snippet.cpp",
|
"snippet.cpp",
|
||||||
"snippet.h",
|
"snippet.h",
|
||||||
@@ -214,6 +212,10 @@ Project {
|
|||||||
"snippetassistcollector.h",
|
"snippetassistcollector.h",
|
||||||
"snippeteditor.cpp",
|
"snippeteditor.cpp",
|
||||||
"snippeteditor.h",
|
"snippeteditor.h",
|
||||||
|
"snippetoverlay.cpp",
|
||||||
|
"snippetoverlay.h",
|
||||||
|
"snippetprovider.cpp",
|
||||||
|
"snippetprovider.h",
|
||||||
"snippetscollection.cpp",
|
"snippetscollection.cpp",
|
||||||
"snippetscollection.h",
|
"snippetscollection.h",
|
||||||
"snippetssettings.cpp",
|
"snippetssettings.cpp",
|
||||||
|
|||||||
@@ -25,7 +25,6 @@
|
|||||||
|
|
||||||
#include "texteditoroverlay.h"
|
#include "texteditoroverlay.h"
|
||||||
#include "texteditor.h"
|
#include "texteditor.h"
|
||||||
#include "snippets/snippet.h"
|
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QMap>
|
#include <QMap>
|
||||||
@@ -485,80 +484,3 @@ bool TextEditorOverlay::hasFirstSelectionBeginMoved() const
|
|||||||
return false;
|
return false;
|
||||||
return m_selections.at(0).m_cursor_begin.position() != m_firstSelectionOriginalBegin;
|
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 ¤tText = 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ QT_FORWARD_DECLARE_CLASS(QWidget)
|
|||||||
QT_FORWARD_DECLARE_CLASS(QPainterPath)
|
QT_FORWARD_DECLARE_CLASS(QPainterPath)
|
||||||
|
|
||||||
namespace TextEditor {
|
namespace TextEditor {
|
||||||
class NameMangler;
|
|
||||||
class TextEditorWidget;
|
class TextEditorWidget;
|
||||||
|
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
@@ -117,22 +116,5 @@ private:
|
|||||||
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;
|
|
||||||
QList<NameMangler *> m_manglers;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
} // namespace TextEditor
|
} // namespace TextEditor
|
||||||
|
|||||||
Reference in New Issue
Block a user