TextEditor: Use simple text indentation as default

Simple indentation based on the previous line was already available in
the NormalIndenter class. Merge that up the hierarchy chain into
TextIndenter which is the base for other text-based indenters, and make
that the default indenter for text editor factories.

Text editor factories that don't have a special indenter get at least
basic indentation support for free that way.

Change-Id: Ib977a990f10a99bead82bc8a8348c02a106665f1
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2020-02-28 13:17:08 +01:00
parent a77ef4ca7f
commit 6959618d7b
12 changed files with 59 additions and 141 deletions

View File

@@ -29,7 +29,6 @@
#include <texteditor/codeassist/keywordscompletionassist.h>
#include <coreplugin/editormanager/ieditorfactory.h>
#include <texteditor/normalindenter.h>
#include <texteditor/textdocument.h>
#include <texteditor/texteditoractionhandler.h>
#include <texteditor/texteditorconstants.h>

View File

@@ -36,10 +36,10 @@
#include <coreplugin/dialogs/promptoverwritedialog.h>
#include <texteditor/icodestylepreferences.h>
#include <texteditor/icodestylepreferencesfactory.h>
#include <texteditor/normalindenter.h>
#include <texteditor/storagesettings.h>
#include <texteditor/tabsettings.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/textindenter.h>
#include <utils/algorithm.h>
#include <utils/mimetypes/mimedatabase.h>
@@ -101,7 +101,7 @@ bool JsonWizardGenerator::formatFile(const JsonWizard *wizard, GeneratedFile *fi
indenter->setFileName(Utils::FilePath::fromString(file->path()));
}
if (!indenter)
indenter = new NormalIndenter(&doc);
indenter = new TextIndenter(&doc);
ICodeStylePreferences *codeStylePrefs = codeStylePreferences(baseProject, languageId);
indenter->setCodeStylePreferences(codeStylePrefs);

View File

@@ -35,15 +35,15 @@
#include <utils/stringutils.h>
#include <coreplugin/icore.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/icodestylepreferences.h>
#include <texteditor/icodestylepreferencesfactory.h>
#include <texteditor/normalindenter.h>
#include <texteditor/tabsettings.h>
#include <texteditor/storagesettings.h>
#include <projectexplorer/editorconfiguration.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projecttree.h>
#include <projectexplorer/editorconfiguration.h>
#include <texteditor/icodestylepreferences.h>
#include <texteditor/icodestylepreferencesfactory.h>
#include <texteditor/storagesettings.h>
#include <texteditor/tabsettings.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/textindenter.h>
#include <utils/mimetypes/mimedatabase.h>
#
#include <QPointer>
@@ -259,7 +259,7 @@ void ProjectFileWizardExtension::applyCodeStyle(GeneratedFile *file) const
indenter->setFileName(Utils::FilePath::fromString(file->path()));
}
if (!indenter)
indenter = new NormalIndenter(&doc);
indenter = new TextIndenter(&doc);
ICodeStylePreferences *codeStylePrefs = codeStylePreferences(baseProject, languageId);
indenter->setCodeStylePreferences(codeStylePrefs);

View File

@@ -64,7 +64,6 @@ add_qtc_plugin(TextEditor
ioutlinewidget.h
linenumberfilter.cpp linenumberfilter.h
marginsettings.cpp marginsettings.h
normalindenter.cpp normalindenter.h
outlinefactory.cpp outlinefactory.h
plaintexteditorfactory.cpp plaintexteditorfactory.h
quickfix.cpp quickfix.h

View File

@@ -1,74 +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.
**
****************************************************************************/
#include "normalindenter.h"
#include "tabsettings.h"
#include <QTextDocument>
// Indent a text block based on previous line.
// Simple text paragraph layout:
// aaaa aaaa
//
// bbb bb
// bbb bb
//
// - list
// list line2
//
// - listn
//
// ccc
//
// @todo{Add formatting to wrap paragraphs. This requires some
// hoops as the current indentation routines are not prepared
// for additional block being inserted. It might be possible
// to do in 2 steps (indenting/wrapping)}
//
using namespace TextEditor;
NormalIndenter::NormalIndenter(QTextDocument *doc)
: TextIndenter(doc)
{}
int NormalIndenter::indentFor(const QTextBlock &block,
const TabSettings &tabSettings,
int cursorPositionInEditor)
{
Q_UNUSED(tabSettings)
Q_UNUSED(cursorPositionInEditor)
QTextBlock previous = block.previous();
if (!previous.isValid())
return 0;
const QString previousText = previous.text();
// Empty line indicates a start of a new paragraph. Leave as is.
if (previousText.isEmpty() || previousText.trimmed().isEmpty())
return 0;
return tabSettings.indentationColumn(previousText);
}

View File

@@ -1,43 +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 "textindenter.h"
namespace TextEditor {
class TEXTEDITOR_EXPORT NormalIndenter : public TextIndenter
{
public:
explicit NormalIndenter(QTextDocument *doc);
~NormalIndenter() override = default;
int indentFor(const QTextBlock &block,
const TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
};
} // namespace TextEditor

View File

@@ -24,14 +24,14 @@
****************************************************************************/
#include "plaintexteditorfactory.h"
#include "texteditor.h"
#include "basehoverhandler.h"
#include "textdocument.h"
#include "normalindenter.h"
#include "texteditor.h"
#include "texteditoractionhandler.h"
#include "texteditorconstants.h"
#include "texteditorplugin.h"
#include "texteditorsettings.h"
#include "basehoverhandler.h"
#include "textindenter.h"
#include <coreplugin/coreconstants.h>
#include <coreplugin/infobar.h>
@@ -65,7 +65,6 @@ PlainTextEditorFactory::PlainTextEditorFactory()
setDocumentCreator([]() { return new TextDocument(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID); });
setEditorWidgetCreator([]() { return new PlainTextEditorWidget; });
setIndenterCreator([](QTextDocument *doc) { return new NormalIndenter(doc); });
setUseGenericHighlighter(true);
setEditorActionHandlers(TextEditorActionHandler::Format |

View File

@@ -41,7 +41,6 @@
#include "highlighter.h"
#include "highlightersettings.h"
#include "icodestylepreferences.h"
#include "indenter.h"
#include "refactoroverlay.h"
#include "snippets/snippet.h"
#include "storagesettings.h"
@@ -52,6 +51,7 @@
#include "texteditorconstants.h"
#include "texteditoroverlay.h"
#include "texteditorsettings.h"
#include "textindenter.h"
#include "typingsettings.h"
#include <texteditor/codeassist/assistinterface.h>
@@ -8539,9 +8539,10 @@ namespace Internal {
class TextEditorFactoryPrivate
{
public:
TextEditorFactoryPrivate(TextEditorFactory *parent) :
q(parent),
m_widgetCreator([]() { return new TextEditorWidget; })
TextEditorFactoryPrivate(TextEditorFactory *parent)
: q(parent)
, m_widgetCreator([]() { return new TextEditorWidget; })
, m_indenterCreator([](QTextDocument *d) { return new TextIndenter(d); })
{}
BaseTextEditor *duplicateTextEditor(BaseTextEditor *other)

View File

@@ -41,7 +41,6 @@ SOURCES += texteditorplugin.cpp \
texteditoroverlay.cpp \
textdocumentlayout.cpp \
completionsettings.cpp \
normalindenter.cpp \
textindenter.cpp \
quickfix.cpp \
syntaxhighlighter.cpp \
@@ -129,7 +128,6 @@ HEADERS += texteditorplugin.h \
texteditoroverlay.h \
textdocumentlayout.h \
completionsettings.h \
normalindenter.h \
textindenter.h \
quickfix.h \
syntaxhighlighter.h \

View File

@@ -104,8 +104,6 @@ Project {
"linenumberfilter.h",
"marginsettings.cpp",
"marginsettings.h",
"normalindenter.cpp",
"normalindenter.h",
"outlinefactory.cpp",
"outlinefactory.h",
"plaintexteditorfactory.cpp",

View File

@@ -36,6 +36,43 @@ TextIndenter::TextIndenter(QTextDocument *doc)
TextIndenter::~TextIndenter() = default;
// Indent a text block based on previous line.
// Simple text paragraph layout:
// aaaa aaaa
//
// bbb bb
// bbb bb
//
// - list
// list line2
//
// - listn
//
// ccc
//
// @todo{Add formatting to wrap paragraphs. This requires some
// hoops as the current indentation routines are not prepared
// for additional block being inserted. It might be possible
// to do in 2 steps (indenting/wrapping)}
int TextIndenter::indentFor(const QTextBlock &block,
const TabSettings &tabSettings,
int cursorPositionInEditor)
{
Q_UNUSED(tabSettings)
Q_UNUSED(cursorPositionInEditor)
QTextBlock previous = block.previous();
if (!previous.isValid())
return 0;
const QString previousText = previous.text();
// Empty line indicates a start of a new paragraph. Leave as is.
if (previousText.isEmpty() || previousText.trimmed().isEmpty())
return 0;
return tabSettings.indentationColumn(previousText);
}
IndentationForBlock TextIndenter::indentationForBlocks(const QVector<QTextBlock> &blocks,
const TabSettings &tabSettings,
int /*cursorPositionInEditor*/)

View File

@@ -44,6 +44,10 @@ public:
explicit TextIndenter(QTextDocument *doc);
~TextIndenter() override;
int indentFor(const QTextBlock &block,
const TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
const TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;