Commit 538f6115 authored by Christian Kamm's avatar Christian Kamm
Browse files

Indenters: Move style setup into the QtStyleCodeFormatter constructors.

This makes sure styles are consistent for the editor and the quick fix
engine.
parent 0355e37e
...@@ -1474,37 +1474,13 @@ bool CPPEditor::isInComment(const QTextCursor &cursor) const ...@@ -1474,37 +1474,13 @@ bool CPPEditor::isInComment(const QTextCursor &cursor) const
return false; return false;
} }
static CppTools::QtStyleCodeFormatter setupCodeFormatter(const TextEditor::TabSettings &ts)
{
CppTools::QtStyleCodeFormatter codeFormatter;
codeFormatter.setIndentSize(ts.m_indentSize);
codeFormatter.setTabSize(ts.m_tabSize);
if (ts.m_indentBraces && ts.m_doubleIndentBlocks) { // gnu style
codeFormatter.setIndentSubstatementBraces(true);
codeFormatter.setIndentSubstatementStatements(true);
codeFormatter.setIndentDeclarationBraces(false);
codeFormatter.setIndentDeclarationMembers(true);
} else if (ts.m_indentBraces) { // whitesmiths style
codeFormatter.setIndentSubstatementBraces(true);
codeFormatter.setIndentSubstatementStatements(false);
codeFormatter.setIndentDeclarationBraces(true);
codeFormatter.setIndentDeclarationMembers(false);
} else { // default Qt style
codeFormatter.setIndentSubstatementBraces(false);
codeFormatter.setIndentSubstatementStatements(true);
codeFormatter.setIndentDeclarationBraces(false);
codeFormatter.setIndentDeclarationMembers(true);
}
return codeFormatter;
}
void CPPEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar) void CPPEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar)
{ {
Q_UNUSED(doc) Q_UNUSED(doc)
Q_UNUSED(typedChar) Q_UNUSED(typedChar)
const TabSettings &ts = tabSettings(); const TabSettings &ts = tabSettings();
CppTools::QtStyleCodeFormatter codeFormatter = setupCodeFormatter(ts); CppTools::QtStyleCodeFormatter codeFormatter(ts);
codeFormatter.updateStateUntil(block); codeFormatter.updateStateUntil(block);
const int depth = codeFormatter.indentFor(block); const int depth = codeFormatter.indentFor(block);
...@@ -1522,7 +1498,7 @@ void CPPEditor::indent(QTextDocument *doc, const QTextCursor &cursor, QChar type ...@@ -1522,7 +1498,7 @@ void CPPEditor::indent(QTextDocument *doc, const QTextCursor &cursor, QChar type
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next(); const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
const TabSettings &ts = tabSettings(); const TabSettings &ts = tabSettings();
CppTools::QtStyleCodeFormatter codeFormatter = setupCodeFormatter(ts); CppTools::QtStyleCodeFormatter codeFormatter(ts);
codeFormatter.updateStateUntil(block); codeFormatter.updateStateUntil(block);
QTextCursor tc = textCursor(); QTextCursor tc = textCursor();
......
...@@ -104,7 +104,7 @@ void CppRefactoringChanges::indentSelection(const QTextCursor &selection) const ...@@ -104,7 +104,7 @@ void CppRefactoringChanges::indentSelection(const QTextCursor &selection) const
const QTextBlock end = doc->findBlock(selection.selectionEnd()).next(); const QTextBlock end = doc->findBlock(selection.selectionEnd()).next();
const TextEditor::TabSettings &tabSettings(TextEditor::TextEditorSettings::instance()->tabSettings()); const TextEditor::TabSettings &tabSettings(TextEditor::TextEditorSettings::instance()->tabSettings());
CppTools::QtStyleCodeFormatter codeFormatter; CppTools::QtStyleCodeFormatter codeFormatter(tabSettings);
codeFormatter.updateStateUntil(block); codeFormatter.updateStateUntil(block);
do { do {
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include <Lexer.h> #include <Lexer.h>
#include <texteditor/basetextdocumentlayout.h> #include <texteditor/basetextdocumentlayout.h>
#include <texteditor/tabsettings.h>
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include <QtGui/QTextDocument> #include <QtGui/QTextDocument>
...@@ -914,6 +915,32 @@ QtStyleCodeFormatter::QtStyleCodeFormatter() ...@@ -914,6 +915,32 @@ QtStyleCodeFormatter::QtStyleCodeFormatter()
{ {
} }
QtStyleCodeFormatter::QtStyleCodeFormatter(const TextEditor::TabSettings &tabSettings)
: m_indentSize(tabSettings.m_indentSize)
, m_indentSubstatementBraces(false)
, m_indentSubstatementStatements(true)
, m_indentDeclarationBraces(false)
, m_indentDeclarationMembers(true)
{
setTabSize(tabSettings.m_tabSize);
if (tabSettings.m_indentBraces && tabSettings.m_doubleIndentBlocks) { // gnu style
setIndentSubstatementBraces(true);
setIndentSubstatementStatements(true);
setIndentDeclarationBraces(false);
setIndentDeclarationMembers(true);
} else if (tabSettings.m_indentBraces) { // whitesmiths style
setIndentSubstatementBraces(true);
setIndentSubstatementStatements(false);
setIndentDeclarationBraces(true);
setIndentDeclarationMembers(false);
} else { // default Qt style
setIndentSubstatementBraces(false);
setIndentSubstatementStatements(true);
setIndentDeclarationBraces(false);
setIndentDeclarationMembers(true);
}
}
void QtStyleCodeFormatter::setIndentSize(int size) void QtStyleCodeFormatter::setIndentSize(int size)
{ {
m_indentSize = size; m_indentSize = size;
......
...@@ -46,6 +46,10 @@ class QTextDocument; ...@@ -46,6 +46,10 @@ class QTextDocument;
class QTextBlock; class QTextBlock;
QT_END_NAMESPACE QT_END_NAMESPACE
namespace TextEditor {
class TabSettings;
}
namespace CppTools { namespace CppTools {
namespace Internal { namespace Internal {
class CppCodeFormatterData; class CppCodeFormatterData;
...@@ -241,6 +245,7 @@ class CPPTOOLS_EXPORT QtStyleCodeFormatter : public CodeFormatter ...@@ -241,6 +245,7 @@ class CPPTOOLS_EXPORT QtStyleCodeFormatter : public CodeFormatter
{ {
public: public:
QtStyleCodeFormatter(); QtStyleCodeFormatter();
explicit QtStyleCodeFormatter(const TextEditor::TabSettings &tabSettings);
void setIndentSize(int size); void setIndentSize(int size);
......
...@@ -118,7 +118,6 @@ public: ...@@ -118,7 +118,6 @@ public:
changes.replace(start, end, m_componentName + QLatin1String(" {\n")); changes.replace(start, end, m_componentName + QLatin1String(" {\n"));
currentFile->change(changes); currentFile->change(changes);
currentFile->indent(range(start, end + 1)); currentFile->indent(range(start, end + 1));
} }
}; };
......
...@@ -1276,21 +1276,13 @@ bool QmlJSTextEditor::isClosingBrace(const QList<Token> &tokens) const ...@@ -1276,21 +1276,13 @@ bool QmlJSTextEditor::isClosingBrace(const QList<Token> &tokens) const
return false; return false;
} }
static QmlJSEditor::QtStyleCodeFormatter setupCodeFormatter(const TextEditor::TabSettings &ts)
{
QmlJSEditor::QtStyleCodeFormatter codeFormatter;
codeFormatter.setIndentSize(ts.m_indentSize);
codeFormatter.setTabSize(ts.m_tabSize);
return codeFormatter;
}
void QmlJSTextEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar) void QmlJSTextEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar)
{ {
Q_UNUSED(doc) Q_UNUSED(doc)
Q_UNUSED(typedChar) Q_UNUSED(typedChar)
const TextEditor::TabSettings &ts = tabSettings(); const TextEditor::TabSettings &ts = tabSettings();
QmlJSEditor::QtStyleCodeFormatter codeFormatter = setupCodeFormatter(ts); QmlJSEditor::QtStyleCodeFormatter codeFormatter(ts);
codeFormatter.updateStateUntil(block); codeFormatter.updateStateUntil(block);
const int depth = codeFormatter.indentFor(block); const int depth = codeFormatter.indentFor(block);
......
...@@ -29,6 +29,8 @@ ...@@ -29,6 +29,8 @@
#include "qmljseditorcodeformatter.h" #include "qmljseditorcodeformatter.h"
#include <texteditor/tabsettings.h>
#include <QtCore/QDebug> #include <QtCore/QDebug>
using namespace QmlJS; using namespace QmlJS;
...@@ -40,6 +42,12 @@ QtStyleCodeFormatter::QtStyleCodeFormatter() ...@@ -40,6 +42,12 @@ QtStyleCodeFormatter::QtStyleCodeFormatter()
{ {
} }
QtStyleCodeFormatter::QtStyleCodeFormatter(const TextEditor::TabSettings &tabSettings)
: m_indentSize(tabSettings.m_indentSize)
{
setTabSize(tabSettings.m_tabSize);
}
void QtStyleCodeFormatter::setIndentSize(int size) void QtStyleCodeFormatter::setIndentSize(int size)
{ {
m_indentSize = size; m_indentSize = size;
......
...@@ -35,12 +35,17 @@ ...@@ -35,12 +35,17 @@
#include <texteditor/basetextdocumentlayout.h> #include <texteditor/basetextdocumentlayout.h>
#include <qmljs/qmljscodeformatter.h> #include <qmljs/qmljscodeformatter.h>
namespace TextEditor {
class TabSettings;
}
namespace QmlJSEditor { namespace QmlJSEditor {
class QMLJSEDITOR_EXPORT QtStyleCodeFormatter : public QmlJS::CodeFormatter class QMLJSEDITOR_EXPORT QtStyleCodeFormatter : public QmlJS::CodeFormatter
{ {
public: public:
QtStyleCodeFormatter(); QtStyleCodeFormatter();
explicit QtStyleCodeFormatter(const TextEditor::TabSettings &tabSettings);
void setIndentSize(int size); void setIndentSize(int size);
......
...@@ -54,7 +54,7 @@ void QmlJSRefactoringChanges::indentSelection(const QTextCursor &selection) cons ...@@ -54,7 +54,7 @@ void QmlJSRefactoringChanges::indentSelection(const QTextCursor &selection) cons
const QTextBlock end = doc->findBlock(selection.selectionEnd()).next(); const QTextBlock end = doc->findBlock(selection.selectionEnd()).next();
const TextEditor::TabSettings &tabSettings(TextEditor::TextEditorSettings::instance()->tabSettings()); const TextEditor::TabSettings &tabSettings(TextEditor::TextEditorSettings::instance()->tabSettings());
QtStyleCodeFormatter codeFormatter; QtStyleCodeFormatter codeFormatter(tabSettings);
codeFormatter.updateStateUntil(block); codeFormatter.updateStateUntil(block);
do { do {
......
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