forked from qt-creator/qt-creator
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:
@@ -29,7 +29,6 @@
|
|||||||
|
|
||||||
#include <texteditor/codeassist/keywordscompletionassist.h>
|
#include <texteditor/codeassist/keywordscompletionassist.h>
|
||||||
#include <coreplugin/editormanager/ieditorfactory.h>
|
#include <coreplugin/editormanager/ieditorfactory.h>
|
||||||
#include <texteditor/normalindenter.h>
|
|
||||||
#include <texteditor/textdocument.h>
|
#include <texteditor/textdocument.h>
|
||||||
#include <texteditor/texteditoractionhandler.h>
|
#include <texteditor/texteditoractionhandler.h>
|
||||||
#include <texteditor/texteditorconstants.h>
|
#include <texteditor/texteditorconstants.h>
|
||||||
|
@@ -36,10 +36,10 @@
|
|||||||
#include <coreplugin/dialogs/promptoverwritedialog.h>
|
#include <coreplugin/dialogs/promptoverwritedialog.h>
|
||||||
#include <texteditor/icodestylepreferences.h>
|
#include <texteditor/icodestylepreferences.h>
|
||||||
#include <texteditor/icodestylepreferencesfactory.h>
|
#include <texteditor/icodestylepreferencesfactory.h>
|
||||||
#include <texteditor/normalindenter.h>
|
|
||||||
#include <texteditor/storagesettings.h>
|
#include <texteditor/storagesettings.h>
|
||||||
#include <texteditor/tabsettings.h>
|
#include <texteditor/tabsettings.h>
|
||||||
#include <texteditor/texteditorsettings.h>
|
#include <texteditor/texteditorsettings.h>
|
||||||
|
#include <texteditor/textindenter.h>
|
||||||
|
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
#include <utils/mimetypes/mimedatabase.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()));
|
indenter->setFileName(Utils::FilePath::fromString(file->path()));
|
||||||
}
|
}
|
||||||
if (!indenter)
|
if (!indenter)
|
||||||
indenter = new NormalIndenter(&doc);
|
indenter = new TextIndenter(&doc);
|
||||||
ICodeStylePreferences *codeStylePrefs = codeStylePreferences(baseProject, languageId);
|
ICodeStylePreferences *codeStylePrefs = codeStylePreferences(baseProject, languageId);
|
||||||
indenter->setCodeStylePreferences(codeStylePrefs);
|
indenter->setCodeStylePreferences(codeStylePrefs);
|
||||||
|
|
||||||
|
@@ -35,15 +35,15 @@
|
|||||||
#include <utils/stringutils.h>
|
#include <utils/stringutils.h>
|
||||||
|
|
||||||
#include <coreplugin/icore.h>
|
#include <coreplugin/icore.h>
|
||||||
#include <texteditor/texteditorsettings.h>
|
#include <projectexplorer/editorconfiguration.h>
|
||||||
#include <texteditor/icodestylepreferences.h>
|
|
||||||
#include <texteditor/icodestylepreferencesfactory.h>
|
|
||||||
#include <texteditor/normalindenter.h>
|
|
||||||
#include <texteditor/tabsettings.h>
|
|
||||||
#include <texteditor/storagesettings.h>
|
|
||||||
#include <projectexplorer/project.h>
|
#include <projectexplorer/project.h>
|
||||||
#include <projectexplorer/projecttree.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 <utils/mimetypes/mimedatabase.h>
|
||||||
#
|
#
|
||||||
#include <QPointer>
|
#include <QPointer>
|
||||||
@@ -259,7 +259,7 @@ void ProjectFileWizardExtension::applyCodeStyle(GeneratedFile *file) const
|
|||||||
indenter->setFileName(Utils::FilePath::fromString(file->path()));
|
indenter->setFileName(Utils::FilePath::fromString(file->path()));
|
||||||
}
|
}
|
||||||
if (!indenter)
|
if (!indenter)
|
||||||
indenter = new NormalIndenter(&doc);
|
indenter = new TextIndenter(&doc);
|
||||||
|
|
||||||
ICodeStylePreferences *codeStylePrefs = codeStylePreferences(baseProject, languageId);
|
ICodeStylePreferences *codeStylePrefs = codeStylePreferences(baseProject, languageId);
|
||||||
indenter->setCodeStylePreferences(codeStylePrefs);
|
indenter->setCodeStylePreferences(codeStylePrefs);
|
||||||
|
@@ -64,7 +64,6 @@ add_qtc_plugin(TextEditor
|
|||||||
ioutlinewidget.h
|
ioutlinewidget.h
|
||||||
linenumberfilter.cpp linenumberfilter.h
|
linenumberfilter.cpp linenumberfilter.h
|
||||||
marginsettings.cpp marginsettings.h
|
marginsettings.cpp marginsettings.h
|
||||||
normalindenter.cpp normalindenter.h
|
|
||||||
outlinefactory.cpp outlinefactory.h
|
outlinefactory.cpp outlinefactory.h
|
||||||
plaintexteditorfactory.cpp plaintexteditorfactory.h
|
plaintexteditorfactory.cpp plaintexteditorfactory.h
|
||||||
quickfix.cpp quickfix.h
|
quickfix.cpp quickfix.h
|
||||||
|
@@ -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);
|
|
||||||
}
|
|
@@ -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
|
|
@@ -24,14 +24,14 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include "plaintexteditorfactory.h"
|
#include "plaintexteditorfactory.h"
|
||||||
#include "texteditor.h"
|
#include "basehoverhandler.h"
|
||||||
#include "textdocument.h"
|
#include "textdocument.h"
|
||||||
#include "normalindenter.h"
|
#include "texteditor.h"
|
||||||
#include "texteditoractionhandler.h"
|
#include "texteditoractionhandler.h"
|
||||||
#include "texteditorconstants.h"
|
#include "texteditorconstants.h"
|
||||||
#include "texteditorplugin.h"
|
#include "texteditorplugin.h"
|
||||||
#include "texteditorsettings.h"
|
#include "texteditorsettings.h"
|
||||||
#include "basehoverhandler.h"
|
#include "textindenter.h"
|
||||||
|
|
||||||
#include <coreplugin/coreconstants.h>
|
#include <coreplugin/coreconstants.h>
|
||||||
#include <coreplugin/infobar.h>
|
#include <coreplugin/infobar.h>
|
||||||
@@ -65,7 +65,6 @@ PlainTextEditorFactory::PlainTextEditorFactory()
|
|||||||
|
|
||||||
setDocumentCreator([]() { return new TextDocument(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID); });
|
setDocumentCreator([]() { return new TextDocument(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID); });
|
||||||
setEditorWidgetCreator([]() { return new PlainTextEditorWidget; });
|
setEditorWidgetCreator([]() { return new PlainTextEditorWidget; });
|
||||||
setIndenterCreator([](QTextDocument *doc) { return new NormalIndenter(doc); });
|
|
||||||
setUseGenericHighlighter(true);
|
setUseGenericHighlighter(true);
|
||||||
|
|
||||||
setEditorActionHandlers(TextEditorActionHandler::Format |
|
setEditorActionHandlers(TextEditorActionHandler::Format |
|
||||||
|
@@ -41,7 +41,6 @@
|
|||||||
#include "highlighter.h"
|
#include "highlighter.h"
|
||||||
#include "highlightersettings.h"
|
#include "highlightersettings.h"
|
||||||
#include "icodestylepreferences.h"
|
#include "icodestylepreferences.h"
|
||||||
#include "indenter.h"
|
|
||||||
#include "refactoroverlay.h"
|
#include "refactoroverlay.h"
|
||||||
#include "snippets/snippet.h"
|
#include "snippets/snippet.h"
|
||||||
#include "storagesettings.h"
|
#include "storagesettings.h"
|
||||||
@@ -52,6 +51,7 @@
|
|||||||
#include "texteditorconstants.h"
|
#include "texteditorconstants.h"
|
||||||
#include "texteditoroverlay.h"
|
#include "texteditoroverlay.h"
|
||||||
#include "texteditorsettings.h"
|
#include "texteditorsettings.h"
|
||||||
|
#include "textindenter.h"
|
||||||
#include "typingsettings.h"
|
#include "typingsettings.h"
|
||||||
|
|
||||||
#include <texteditor/codeassist/assistinterface.h>
|
#include <texteditor/codeassist/assistinterface.h>
|
||||||
@@ -8539,9 +8539,10 @@ namespace Internal {
|
|||||||
class TextEditorFactoryPrivate
|
class TextEditorFactoryPrivate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TextEditorFactoryPrivate(TextEditorFactory *parent) :
|
TextEditorFactoryPrivate(TextEditorFactory *parent)
|
||||||
q(parent),
|
: q(parent)
|
||||||
m_widgetCreator([]() { return new TextEditorWidget; })
|
, m_widgetCreator([]() { return new TextEditorWidget; })
|
||||||
|
, m_indenterCreator([](QTextDocument *d) { return new TextIndenter(d); })
|
||||||
{}
|
{}
|
||||||
|
|
||||||
BaseTextEditor *duplicateTextEditor(BaseTextEditor *other)
|
BaseTextEditor *duplicateTextEditor(BaseTextEditor *other)
|
||||||
|
@@ -41,7 +41,6 @@ SOURCES += texteditorplugin.cpp \
|
|||||||
texteditoroverlay.cpp \
|
texteditoroverlay.cpp \
|
||||||
textdocumentlayout.cpp \
|
textdocumentlayout.cpp \
|
||||||
completionsettings.cpp \
|
completionsettings.cpp \
|
||||||
normalindenter.cpp \
|
|
||||||
textindenter.cpp \
|
textindenter.cpp \
|
||||||
quickfix.cpp \
|
quickfix.cpp \
|
||||||
syntaxhighlighter.cpp \
|
syntaxhighlighter.cpp \
|
||||||
@@ -129,7 +128,6 @@ HEADERS += texteditorplugin.h \
|
|||||||
texteditoroverlay.h \
|
texteditoroverlay.h \
|
||||||
textdocumentlayout.h \
|
textdocumentlayout.h \
|
||||||
completionsettings.h \
|
completionsettings.h \
|
||||||
normalindenter.h \
|
|
||||||
textindenter.h \
|
textindenter.h \
|
||||||
quickfix.h \
|
quickfix.h \
|
||||||
syntaxhighlighter.h \
|
syntaxhighlighter.h \
|
||||||
|
@@ -104,8 +104,6 @@ Project {
|
|||||||
"linenumberfilter.h",
|
"linenumberfilter.h",
|
||||||
"marginsettings.cpp",
|
"marginsettings.cpp",
|
||||||
"marginsettings.h",
|
"marginsettings.h",
|
||||||
"normalindenter.cpp",
|
|
||||||
"normalindenter.h",
|
|
||||||
"outlinefactory.cpp",
|
"outlinefactory.cpp",
|
||||||
"outlinefactory.h",
|
"outlinefactory.h",
|
||||||
"plaintexteditorfactory.cpp",
|
"plaintexteditorfactory.cpp",
|
||||||
|
@@ -36,6 +36,43 @@ TextIndenter::TextIndenter(QTextDocument *doc)
|
|||||||
|
|
||||||
TextIndenter::~TextIndenter() = default;
|
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,
|
IndentationForBlock TextIndenter::indentationForBlocks(const QVector<QTextBlock> &blocks,
|
||||||
const TabSettings &tabSettings,
|
const TabSettings &tabSettings,
|
||||||
int /*cursorPositionInEditor*/)
|
int /*cursorPositionInEditor*/)
|
||||||
|
@@ -44,6 +44,10 @@ public:
|
|||||||
explicit TextIndenter(QTextDocument *doc);
|
explicit TextIndenter(QTextDocument *doc);
|
||||||
~TextIndenter() override;
|
~TextIndenter() override;
|
||||||
|
|
||||||
|
int indentFor(const QTextBlock &block,
|
||||||
|
const TabSettings &tabSettings,
|
||||||
|
int cursorPositionInEditor = -1) override;
|
||||||
|
|
||||||
IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
|
IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
|
||||||
const TabSettings &tabSettings,
|
const TabSettings &tabSettings,
|
||||||
int cursorPositionInEditor = -1) override;
|
int cursorPositionInEditor = -1) override;
|
||||||
|
Reference in New Issue
Block a user