Commit 06fc31d4 authored by Leandro Melo's avatar Leandro Melo

Indenter is now a separate class which can be reused through composition....

Indenter is now a separate class which can be reused through composition. Added it to the generic editor.
parent 7400557f
......@@ -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.
}
......
......@@ -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
......
......@@ -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;
......
......@@ -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)
......
/**************************************************************************
**
** 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);
}
/**************************************************************************
**
** 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
/**************************************************************************
**
** 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;
}
}
/**************************************************************************
**
** 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
......@@ -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());
}
......@@ -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
......
......@@ -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 \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment