diff --git a/src/plugins/genericeditor/editor.cpp b/src/plugins/genericeditor/editor.cpp index 3a19588a5f0f34d57c647c6547bca09bea52f3a5..aca25ab374e54026b45ec582805d5e7133ada4b1 100644 --- a/src/plugins/genericeditor/editor.cpp +++ b/src/plugins/genericeditor/editor.cpp @@ -41,6 +41,8 @@ #include <texteditor/basetextdocument.h> #include <texteditor/texteditorsettings.h> #include <texteditor/fontsettings.h> +#include <texteditor/tabsettings.h> +#include <texteditor/normalindenter.h> #include <QtCore/QSharedPointer> #include <QtCore/QFileInfo> @@ -55,6 +57,9 @@ Editor::Editor(QWidget *parent) : TextEditor::BaseTextEditor(parent) connect(file(), SIGNAL(changed()), this, SLOT(configure())); } +Editor::~Editor() +{} + void Editor::unCommentSelection() { Utils::unCommentSelection(this, m_commentDefinition); @@ -78,6 +83,11 @@ void Editor::setFontSettings(const TextEditor::FontSettings & fs) highlighter->rehighlight(); } +void Editor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar) +{ + m_indenter->indentBlock(doc, block, typedChar, tabSettings()); +} + void Editor::configure() { const QString &mimeType = Core::ICore::instance()->mimeDatabase()->findByFile( @@ -99,6 +109,12 @@ void Editor::configure() m_commentDefinition.setSingleLine(definition->singleLineComment()); m_commentDefinition.setMultiLineStart(definition->multiLineCommentStart()); m_commentDefinition.setMultiLineEnd(definition->multiLineCommentEnd()); + + //@todo: It's possible to specify an indenter style in the definition file. However, this + // is not really being used because Kate recommends to configure indentation through + // another editor feature. Maybe we should provide something similar in Creator? + // For now the normal indenter is used. + m_indenter.reset(new TextEditor::NormalIndenter); } catch (const HighlighterException &) { // No highlighter will be set. } diff --git a/src/plugins/genericeditor/editor.h b/src/plugins/genericeditor/editor.h index a1317aca956ab454fa16a31f79500867bc844fc8..be4decf9cff4de045281d6a01d2ddd8ad1b5ac6a 100644 --- a/src/plugins/genericeditor/editor.h +++ b/src/plugins/genericeditor/editor.h @@ -34,11 +34,18 @@ #include <utils/uncommentselection.h> #include <QtCore/QList> +#include <QtCore/QScopedPointer> +#include <QtGui/QTextBlock> QT_BEGIN_NAMESPACE class QString; +class QTextDocument; QT_END_NAMESPACE +namespace TextEditor { +class Indenter; +} + namespace GenericEditor { namespace Internal { @@ -47,6 +54,7 @@ class Editor : public TextEditor::BaseTextEditor Q_OBJECT public: Editor(QWidget *parent = 0); + virtual ~Editor(); virtual void unCommentSelection(); @@ -60,7 +68,13 @@ private slots: void configure(); private: + Editor(const Editor &); + Editor &operator=(const Editor &); + + virtual void indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar); + Utils::CommentDefinition m_commentDefinition; + QScopedPointer<TextEditor::Indenter> m_indenter; }; class EditorEditable : public TextEditor::BaseTextEditorEditable diff --git a/src/plugins/genericeditor/highlightdefinition.h b/src/plugins/genericeditor/highlightdefinition.h index 0546d893160a6fcd65333f0cb22c894fe57060b3..1a78761da3b2271321af8b6120c5f8f0b350a638 100644 --- a/src/plugins/genericeditor/highlightdefinition.h +++ b/src/plugins/genericeditor/highlightdefinition.h @@ -63,8 +63,7 @@ public: void addDelimiters(const QString &characters); void removeDelimiters(const QString &characters); - //Todo: wordWrapDelimiters? - bool isDelimiter(const QChar &character) const; + bool isDelimiter(const QChar &character) const; void setSingleLineComment(const QString &start); const QString &singleLineComment() const; diff --git a/src/plugins/genericeditor/highlightdefinitionhandler.cpp b/src/plugins/genericeditor/highlightdefinitionhandler.cpp index fedd7c14686efc14749b24d79e3300800c336a8d..034c1bf3c1cc2f330022cad6610de2ca1dd29810 100644 --- a/src/plugins/genericeditor/highlightdefinitionhandler.cpp +++ b/src/plugins/genericeditor/highlightdefinitionhandler.cpp @@ -288,7 +288,7 @@ void HighlightDefinitionHandler::keywordsElementStarted(const QXmlAttributes &at m_definition->setKeywordsSensitive(atts.value(kCaseSensitive)); m_definition->removeDelimiters(atts.value(kWeakDeliminator)); m_definition->addDelimiters(atts.value(kAdditionalDeliminator)); - //Todo: wordWrapDelimiters? + //@todo: wordWrapDelimiters? } void HighlightDefinitionHandler::detectCharStarted(const QXmlAttributes &atts) diff --git a/src/plugins/texteditor/indenter.cpp b/src/plugins/texteditor/indenter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee2edc642ac03616ecb44d92dcdb246da487a211 --- /dev/null +++ b/src/plugins/texteditor/indenter.cpp @@ -0,0 +1,47 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#include "indenter.h" +#include "tabsettings.h" + +using namespace TextEditor; + +Indenter::Indenter() +{} + +Indenter::~Indenter() +{} + +void Indenter::indentBlock(QTextDocument *doc, + QTextBlock block, + QChar typedChar, + const TabSettings &ts) +{ + doIndentBlock(doc, block, typedChar, ts); +} diff --git a/src/plugins/texteditor/indenter.h b/src/plugins/texteditor/indenter.h new file mode 100644 index 0000000000000000000000000000000000000000..c6aff2ac7feee04943cce634be4fcf6990cd8375 --- /dev/null +++ b/src/plugins/texteditor/indenter.h @@ -0,0 +1,63 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#ifndef INDENTER_H +#define INDENTER_H + +#include "texteditor_global.h" + +#include <QtCore/QChar> +#include <QtGui/QTextBlock> + +QT_BEGIN_NAMESPACE +class QTextDocument; +QT_END_NAMESPACE + +namespace TextEditor { + +struct TabSettings; + +class TEXTEDITOR_EXPORT Indenter +{ +public: + Indenter(); + virtual ~Indenter(); + + void indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar, const TabSettings &ts); + +private: + virtual void doIndentBlock(QTextDocument *doc, + QTextBlock block, + QChar typedChar, + const TabSettings &ts) = 0; +}; + +} // namespace TextEditor + +#endif // INDENTER_H diff --git a/src/plugins/texteditor/normalindenter.cpp b/src/plugins/texteditor/normalindenter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a6eb1398c798ff324ad2ec63b97867f3f6ca192 --- /dev/null +++ b/src/plugins/texteditor/normalindenter.cpp @@ -0,0 +1,89 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#include "normalindenter.h" +#include "tabsettings.h" + +#include <QtGui/QTextDocument> + +using namespace TextEditor; + +NormalIndenter::NormalIndenter() +{} + +NormalIndenter::~NormalIndenter() +{} + +// 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)} +// +void NormalIndenter::doIndentBlock(QTextDocument *doc, + QTextBlock block, + QChar typedChar, + const TextEditor::TabSettings &ts) +{ + Q_UNUSED(typedChar) + + // At beginning: Leave as is. + if (block == doc->begin()) + return; + + const QTextBlock previous = block.previous(); + const QString previousText = previous.text(); + // Empty line indicates a start of a new paragraph. Leave as is. + if (previousText.isEmpty() || previousText.trimmed().isEmpty()) + return; + + // Just use previous line. + // Skip blank characters when determining the indentation + int i = 0; + while (i < previousText.size()) { + if (!previousText.at(i).isSpace()) { + ts.indentLine(block, ts.columnAt(previousText, i)); + break; + } + ++i; + } +} diff --git a/src/plugins/texteditor/normalindenter.h b/src/plugins/texteditor/normalindenter.h new file mode 100644 index 0000000000000000000000000000000000000000..7014f9ce3afe88ee431d8044b296c8f25b0a75de --- /dev/null +++ b/src/plugins/texteditor/normalindenter.h @@ -0,0 +1,52 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#ifndef NORMALINDENTER_H +#define NORMALINDENTER_H + +#include "indenter.h" + +namespace TextEditor { + +class TEXTEDITOR_EXPORT NormalIndenter : public Indenter +{ +public: + NormalIndenter(); + virtual ~NormalIndenter(); + +private: + virtual void doIndentBlock(QTextDocument *doc, + QTextBlock block, + QChar typedChar, + const TabSettings &ts); +}; + +} // namespace TextEditor + +#endif // NORMALINDENTER_H diff --git a/src/plugins/texteditor/plaintexteditor.cpp b/src/plugins/texteditor/plaintexteditor.cpp index 5fdd1136a7c718c6b6d4d917b320147098aac822..257280b0b3c47c1e772b4e90188de5c1b71b9f4e 100644 --- a/src/plugins/texteditor/plaintexteditor.cpp +++ b/src/plugins/texteditor/plaintexteditor.cpp @@ -76,49 +76,7 @@ QString PlainTextEditorEditable::id() const return QLatin1String(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID); } -// 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)} -// - void PlainTextEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar) { - Q_UNUSED(typedChar) - - // At beginning: Leave as is. - if (block == doc->begin()) - return; - - const QTextBlock previous = block.previous(); - const QString previousText = previous.text(); - // Empty line indicates a start of a new paragraph. Leave as is. - if (previousText.isEmpty() || previousText.trimmed().isEmpty()) - return; - - // Just use previous line. - // Skip blank characters when determining the indentation - int i = 0; - while (i < previousText.size()) { - if (!previousText.at(i).isSpace()) { - const TextEditor::TabSettings &ts = tabSettings(); - ts.indentLine(block, ts.columnAt(previousText, i)); - break; - } - ++i; - } + m_indenter.indentBlock(doc, block, typedChar, tabSettings()); } diff --git a/src/plugins/texteditor/plaintexteditor.h b/src/plugins/texteditor/plaintexteditor.h index 181d4c4bbd44ca1a46f865351445799a4e8a393b..f2c5673fb045db07d0679c841ebb12a858ed436a 100644 --- a/src/plugins/texteditor/plaintexteditor.h +++ b/src/plugins/texteditor/plaintexteditor.h @@ -31,6 +31,7 @@ #define PLAINTEXTEDITOR_H #include "basetexteditor.h" +#include "normalindenter.h" #include <QtCore/QList> @@ -61,9 +62,12 @@ public: PlainTextEditor(QWidget *parent); protected: - virtual BaseTextEditorEditable *createEditableInterface() { return new PlainTextEditorEditable(this); } - // Indent a text block based on previous line. + virtual BaseTextEditorEditable *createEditableInterface() { return new PlainTextEditorEditable(this); } virtual void indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar); + +private: + // Indent a text block based on previous line. + NormalIndenter m_indenter; }; } // namespace TextEditor diff --git a/src/plugins/texteditor/texteditor.pro b/src/plugins/texteditor/texteditor.pro index a27f857809a36a97a7bfcc9810824dea36aaaabe..bf64eb51c18d282934bc99ee81cd9ffac4c38439 100644 --- a/src/plugins/texteditor/texteditor.pro +++ b/src/plugins/texteditor/texteditor.pro @@ -35,7 +35,9 @@ SOURCES += texteditorplugin.cpp \ texteditoroverlay.cpp \ texteditoroptionspage.cpp \ basetextdocumentlayout.cpp \ - completionsettings.cpp + completionsettings.cpp \ + normalindenter.cpp \ + indenter.cpp HEADERS += texteditorplugin.h \ textfilewizard.h \ @@ -73,7 +75,9 @@ HEADERS += texteditorplugin.h \ texteditoroverlay.h \ texteditoroptionspage.h \ basetextdocumentlayout.h \ - completionsettings.h + completionsettings.h \ + normalindenter.h \ + indenter.h FORMS += behaviorsettingspage.ui \