Beautifier: Move formatting tools to TextEditor

Formatting is moved from Beautifier plugin to formattexteditor.h/.cpp.
Diff and Differ classes are extracted from DiffEditor to Utils
to prevent extra TextEditor dependencies.

This change will make possible to use formatCurrentFile
and similar functions not only from Beautifier code.

Change-Id: Ic5ca668afe88f4e9376d49e6bd3594807172b0dd
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Ivan Donchevskii
2018-08-29 14:36:47 +02:00
parent 7777df42cf
commit 5c658ac968
26 changed files with 503 additions and 397 deletions

View File

@@ -11,7 +11,6 @@ HEADERS += \
diffeditorfactory.h \
diffeditorplugin.h \
diffeditorwidgetcontroller.h \
differ.h \
diffutils.h \
diffview.h \
selectabletexteditorwidget.h \
@@ -27,7 +26,6 @@ SOURCES += \
diffeditorfactory.cpp \
diffeditorplugin.cpp \
diffeditorwidgetcontroller.cpp \
differ.cpp \
diffutils.cpp \
diffview.cpp \
selectabletexteditorwidget.cpp \

View File

@@ -32,8 +32,6 @@ QtcPlugin {
"diffeditorplugin.h",
"diffeditorwidgetcontroller.cpp",
"diffeditorwidgetcontroller.h",
"differ.cpp",
"differ.h",
"diffutils.cpp",
"diffutils.h",
"diffview.cpp",

View File

@@ -29,7 +29,6 @@
#include "diffeditorcontroller.h"
#include "diffeditordocument.h"
#include "diffeditorfactory.h"
#include "differ.h"
#include <QAction>
#include <QFileDialog>
@@ -50,10 +49,12 @@
#include <texteditor/texteditor.h>
#include <utils/algorithm.h>
#include <utils/differ.h>
#include <utils/mapreduce.h>
#include <utils/qtcassert.h>
using namespace Core;
using namespace Utils;
namespace DiffEditor {
namespace Internal {

File diff suppressed because it is too large Load Diff

View File

@@ -1,117 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2016 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 "diffeditor_global.h"
#include <QString>
QT_BEGIN_NAMESPACE
template <class K, class T>
class QMap;
class QFutureInterfaceBase;
QT_END_NAMESPACE
namespace DiffEditor {
class DIFFEDITOR_EXPORT Diff
{
public:
enum Command {
Delete,
Insert,
Equal
};
Command command;
QString text;
Diff(Command com, const QString &txt = QString());
Diff();
bool operator==(const Diff &other) const;
bool operator!=(const Diff &other) const;
QString toString() const;
static QString commandString(Command com);
};
class DIFFEDITOR_EXPORT Differ
{
public:
enum DiffMode
{
CharMode,
WordMode,
LineMode
};
Differ(QFutureInterfaceBase *jobController = nullptr);
QList<Diff> diff(const QString &text1, const QString &text2);
QList<Diff> unifiedDiff(const QString &text1, const QString &text2);
void setDiffMode(DiffMode mode);
DiffMode diffMode() const;
static QList<Diff> merge(const QList<Diff> &diffList);
static QList<Diff> cleanupSemantics(const QList<Diff> &diffList);
static QList<Diff> cleanupSemanticsLossless(const QList<Diff> &diffList);
static void splitDiffList(const QList<Diff> &diffList,
QList<Diff> *leftDiffList,
QList<Diff> *rightDiffList);
static QList<Diff> moveWhitespaceIntoEqualities(const QList<Diff> &input);
static void diffWithWhitespaceReduced(const QString &leftInput,
const QString &rightInput,
QList<Diff> *leftOutput,
QList<Diff> *rightOutput);
static void unifiedDiffWithWhitespaceReduced(const QString &leftInput,
const QString &rightInput,
QList<Diff> *leftOutput,
QList<Diff> *rightOutput);
static void ignoreWhitespaceBetweenEqualities(const QList<Diff> &leftInput,
const QList<Diff> &rightInput,
QList<Diff> *leftOutput,
QList<Diff> *rightOutput);
static void diffBetweenEqualities(const QList<Diff> &leftInput,
const QList<Diff> &rightInput,
QList<Diff> *leftOutput,
QList<Diff> *rightOutput);
private:
QList<Diff> preprocess1AndDiff(const QString &text1, const QString &text2);
QList<Diff> preprocess2AndDiff(const QString &text1, const QString &text2);
QList<Diff> diffMyers(const QString &text1, const QString &text2);
QList<Diff> diffMyersSplit(const QString &text1, int x,
const QString &text2, int y);
QList<Diff> diffNonCharMode(const QString &text1, const QString &text2);
QStringList encode(const QString &text1,
const QString &text2,
QString *encodedText1,
QString *encodedText2);
QString encode(const QString &text,
QStringList *lines,
QMap<QString, int> *lineToCode);
int findSubtextEnd(const QString &text,
int subTextStart);
DiffMode m_diffMode = Differ::LineMode;
DiffMode m_currentDiffMode = Differ::LineMode;
QFutureInterfaceBase *m_jobController = nullptr;
};
} // namespace DiffEditor

View File

@@ -24,15 +24,17 @@
****************************************************************************/
#include "diffutils.h"
#include "differ.h"
#include "texteditor/fontsettings.h"
#include <texteditor/fontsettings.h>
#include <utils/differ.h>
#include <QFutureInterfaceBase>
#include <QRegularExpression>
#include <QStringList>
#include <QTextStream>
using namespace Utils;
namespace DiffEditor {
static QList<TextLineData> assemblyRows(const QList<TextLineData> &lines,
@@ -103,7 +105,7 @@ static void handleDifference(const QString &text,
* The number of equalities on both lists must be the same.
*/
ChunkData DiffUtils::calculateOriginalData(const QList<Diff> &leftDiffList,
const QList<Diff> &rightDiffList)
const QList<Diff> &rightDiffList)
{
int i = 0;
int j = 0;

View File

@@ -36,9 +36,9 @@ QT_END_NAMESPACE
namespace TextEditor { class FontSettings; }
namespace DiffEditor {
namespace Utils { class Diff; }
class Diff;
namespace DiffEditor {
class DIFFEDITOR_EXPORT DiffFileInfo {
public:
@@ -128,8 +128,8 @@ public:
GitFormat = AddLevel | 0x2, // Add line 'diff ..' as git does
};
static ChunkData calculateOriginalData(const QList<Diff> &leftDiffList,
const QList<Diff> &rightDiffList);
static ChunkData calculateOriginalData(const QList<Utils::Diff> &leftDiffList,
const QList<Utils::Diff> &rightDiffList);
static FileData calculateContextData(const ChunkData &originalData,
int contextLineCount,
int joinChunkThreshold = 1);