diff options
Diffstat (limited to 'src/plugins/texteditor')
-rw-r--r-- | src/plugins/texteditor/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/plugins/texteditor/snippets/snippetoverlay.cpp | 112 | ||||
-rw-r--r-- | src/plugins/texteditor/snippets/snippetoverlay.h | 54 | ||||
-rw-r--r-- | src/plugins/texteditor/texteditor.cpp | 1 | ||||
-rw-r--r-- | src/plugins/texteditor/texteditor.pro | 6 | ||||
-rw-r--r-- | src/plugins/texteditor/texteditor.qbs | 6 | ||||
-rw-r--r-- | src/plugins/texteditor/texteditoroverlay.cpp | 78 | ||||
-rw-r--r-- | src/plugins/texteditor/texteditoroverlay.h | 18 |
8 files changed, 176 insertions, 100 deletions
diff --git a/src/plugins/texteditor/CMakeLists.txt b/src/plugins/texteditor/CMakeLists.txt index 93d773ddb5..8d5f9236cb 100644 --- a/src/plugins/texteditor/CMakeLists.txt +++ b/src/plugins/texteditor/CMakeLists.txt @@ -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 diff --git a/src/plugins/texteditor/snippets/snippetoverlay.cpp b/src/plugins/texteditor/snippets/snippetoverlay.cpp new file mode 100644 index 0000000000..e80cf8cb94 --- /dev/null +++ b/src/plugins/texteditor/snippets/snippetoverlay.cpp @@ -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 diff --git a/src/plugins/texteditor/snippets/snippetoverlay.h b/src/plugins/texteditor/snippets/snippetoverlay.h new file mode 100644 index 0000000000..0edf0a8e0d --- /dev/null +++ b/src/plugins/texteditor/snippets/snippetoverlay.h @@ -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 + diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 81459f90cd..921b361010 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -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" diff --git a/src/plugins/texteditor/texteditor.pro b/src/plugins/texteditor/texteditor.pro index bbf536459e..6e00135a16 100644 --- a/src/plugins/texteditor/texteditor.pro +++ b/src/plugins/texteditor/texteditor.pro @@ -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 \ diff --git a/src/plugins/texteditor/texteditor.qbs b/src/plugins/texteditor/texteditor.qbs index 5b36e0c76c..a7046a4241 100644 --- a/src/plugins/texteditor/texteditor.qbs +++ b/src/plugins/texteditor/texteditor.qbs @@ -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", diff --git a/src/plugins/texteditor/texteditoroverlay.cpp b/src/plugins/texteditor/texteditoroverlay.cpp index 5522a7ef33..41107e39d9 100644 --- a/src/plugins/texteditor/texteditoroverlay.cpp +++ b/src/plugins/texteditor/texteditoroverlay.cpp @@ -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 ¤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(); - } - } -} diff --git a/src/plugins/texteditor/texteditoroverlay.h b/src/plugins/texteditor/texteditoroverlay.h index a07252c0b2..e995f007c5 100644 --- a/src/plugins/texteditor/texteditoroverlay.h +++ b/src/plugins/texteditor/texteditoroverlay.h @@ -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 |