From 6959618d7b2d9efc8f5f668489523d1833d4b396 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Fri, 28 Feb 2020 13:17:08 +0100 Subject: [PATCH] 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 --- src/plugins/android/javaeditor.cpp | 1 - .../jsonwizard/jsonwizardgeneratorfactory.cpp | 4 +- .../projectfilewizardextension.cpp | 16 ++-- src/plugins/texteditor/CMakeLists.txt | 1 - src/plugins/texteditor/normalindenter.cpp | 74 ------------------- src/plugins/texteditor/normalindenter.h | 43 ----------- .../texteditor/plaintexteditorfactory.cpp | 7 +- src/plugins/texteditor/texteditor.cpp | 9 ++- src/plugins/texteditor/texteditor.pro | 2 - src/plugins/texteditor/texteditor.qbs | 2 - src/plugins/texteditor/textindenter.cpp | 37 ++++++++++ src/plugins/texteditor/textindenter.h | 4 + 12 files changed, 59 insertions(+), 141 deletions(-) delete mode 100644 src/plugins/texteditor/normalindenter.cpp delete mode 100644 src/plugins/texteditor/normalindenter.h diff --git a/src/plugins/android/javaeditor.cpp b/src/plugins/android/javaeditor.cpp index b280977c89f..e984dbcf8ed 100644 --- a/src/plugins/android/javaeditor.cpp +++ b/src/plugins/android/javaeditor.cpp @@ -29,7 +29,6 @@ #include #include -#include #include #include #include diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardgeneratorfactory.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardgeneratorfactory.cpp index 4f4f8882f74..5d9a7e19ef4 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardgeneratorfactory.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardgeneratorfactory.cpp @@ -36,10 +36,10 @@ #include #include #include -#include #include #include #include +#include #include #include @@ -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); diff --git a/src/plugins/projectexplorer/projectfilewizardextension.cpp b/src/plugins/projectexplorer/projectfilewizardextension.cpp index 33be4cdcfe8..ff58e14308b 100644 --- a/src/plugins/projectexplorer/projectfilewizardextension.cpp +++ b/src/plugins/projectexplorer/projectfilewizardextension.cpp @@ -35,15 +35,15 @@ #include #include -#include -#include -#include -#include -#include -#include +#include #include #include -#include +#include +#include +#include +#include +#include +#include #include # #include @@ -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); diff --git a/src/plugins/texteditor/CMakeLists.txt b/src/plugins/texteditor/CMakeLists.txt index 4cc3d394752..368afb47423 100644 --- a/src/plugins/texteditor/CMakeLists.txt +++ b/src/plugins/texteditor/CMakeLists.txt @@ -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 diff --git a/src/plugins/texteditor/normalindenter.cpp b/src/plugins/texteditor/normalindenter.cpp deleted file mode 100644 index e4a8ef4d109..00000000000 --- a/src/plugins/texteditor/normalindenter.cpp +++ /dev/null @@ -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 - -// 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); -} diff --git a/src/plugins/texteditor/normalindenter.h b/src/plugins/texteditor/normalindenter.h deleted file mode 100644 index 5a53caa4a31..00000000000 --- a/src/plugins/texteditor/normalindenter.h +++ /dev/null @@ -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 diff --git a/src/plugins/texteditor/plaintexteditorfactory.cpp b/src/plugins/texteditor/plaintexteditorfactory.cpp index 02e89b42278..f87165b7981 100644 --- a/src/plugins/texteditor/plaintexteditorfactory.cpp +++ b/src/plugins/texteditor/plaintexteditorfactory.cpp @@ -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 #include @@ -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 | diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 8feac6cbdac..2e65082918f 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -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 @@ -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) diff --git a/src/plugins/texteditor/texteditor.pro b/src/plugins/texteditor/texteditor.pro index 386640307d5..6892ddd79c1 100644 --- a/src/plugins/texteditor/texteditor.pro +++ b/src/plugins/texteditor/texteditor.pro @@ -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 \ diff --git a/src/plugins/texteditor/texteditor.qbs b/src/plugins/texteditor/texteditor.qbs index bc8756d2abf..6564c97b0ce 100644 --- a/src/plugins/texteditor/texteditor.qbs +++ b/src/plugins/texteditor/texteditor.qbs @@ -104,8 +104,6 @@ Project { "linenumberfilter.h", "marginsettings.cpp", "marginsettings.h", - "normalindenter.cpp", - "normalindenter.h", "outlinefactory.cpp", "outlinefactory.h", "plaintexteditorfactory.cpp", diff --git a/src/plugins/texteditor/textindenter.cpp b/src/plugins/texteditor/textindenter.cpp index c2d328d3fea..fcb04d5fa68 100644 --- a/src/plugins/texteditor/textindenter.cpp +++ b/src/plugins/texteditor/textindenter.cpp @@ -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 &blocks, const TabSettings &tabSettings, int /*cursorPositionInEditor*/) diff --git a/src/plugins/texteditor/textindenter.h b/src/plugins/texteditor/textindenter.h index d361713074d..c76ad134d4b 100644 --- a/src/plugins/texteditor/textindenter.h +++ b/src/plugins/texteditor/textindenter.h @@ -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 &blocks, const TabSettings &tabSettings, int cursorPositionInEditor = -1) override;