diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp index 775b2ee48833e413ca69e887350e9e412ba7899b..a25d67c60d01e100163231f6eef7eeef7012b2ab 100644 --- a/src/plugins/cppeditor/cppeditor.cpp +++ b/src/plugins/cppeditor/cppeditor.cpp @@ -1474,37 +1474,13 @@ bool CPPEditor::isInComment(const QTextCursor &cursor) const 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) { Q_UNUSED(doc) Q_UNUSED(typedChar) const TabSettings &ts = tabSettings(); - CppTools::QtStyleCodeFormatter codeFormatter = setupCodeFormatter(ts); + CppTools::QtStyleCodeFormatter codeFormatter(ts); codeFormatter.updateStateUntil(block); const int depth = codeFormatter.indentFor(block); @@ -1522,7 +1498,7 @@ void CPPEditor::indent(QTextDocument *doc, const QTextCursor &cursor, QChar type const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next(); const TabSettings &ts = tabSettings(); - CppTools::QtStyleCodeFormatter codeFormatter = setupCodeFormatter(ts); + CppTools::QtStyleCodeFormatter codeFormatter(ts); codeFormatter.updateStateUntil(block); QTextCursor tc = textCursor(); diff --git a/src/plugins/cppeditor/cpprefactoringchanges.cpp b/src/plugins/cppeditor/cpprefactoringchanges.cpp index 3aaebc74b068579137a26a2a73e5faf25934b012..6b500a73f04517517b06339b97318156bb155f40 100644 --- a/src/plugins/cppeditor/cpprefactoringchanges.cpp +++ b/src/plugins/cppeditor/cpprefactoringchanges.cpp @@ -104,7 +104,7 @@ void CppRefactoringChanges::indentSelection(const QTextCursor &selection) const const QTextBlock end = doc->findBlock(selection.selectionEnd()).next(); const TextEditor::TabSettings &tabSettings(TextEditor::TextEditorSettings::instance()->tabSettings()); - CppTools::QtStyleCodeFormatter codeFormatter; + CppTools::QtStyleCodeFormatter codeFormatter(tabSettings); codeFormatter.updateStateUntil(block); do { diff --git a/src/plugins/cpptools/cppcodeformatter.cpp b/src/plugins/cpptools/cppcodeformatter.cpp index 481df9b6acc6057e11000cc8bccafa7bf1880214..4164010fb9f26c5947320f203b7a17bd0426a09b 100644 --- a/src/plugins/cpptools/cppcodeformatter.cpp +++ b/src/plugins/cpptools/cppcodeformatter.cpp @@ -33,6 +33,7 @@ #include <Lexer.h> #include <texteditor/basetextdocumentlayout.h> +#include <texteditor/tabsettings.h> #include <QtCore/QDebug> #include <QtGui/QTextDocument> @@ -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) { m_indentSize = size; diff --git a/src/plugins/cpptools/cppcodeformatter.h b/src/plugins/cpptools/cppcodeformatter.h index 68671c9f9c93767a641a0d1bfb54cbaac7bdab2e..a9a9aa7e06beeca66f6b5c3db6f9a17d86978b6b 100644 --- a/src/plugins/cpptools/cppcodeformatter.h +++ b/src/plugins/cpptools/cppcodeformatter.h @@ -46,6 +46,10 @@ class QTextDocument; class QTextBlock; QT_END_NAMESPACE +namespace TextEditor { + class TabSettings; +} + namespace CppTools { namespace Internal { class CppCodeFormatterData; @@ -241,6 +245,7 @@ class CPPTOOLS_EXPORT QtStyleCodeFormatter : public CodeFormatter { public: QtStyleCodeFormatter(); + explicit QtStyleCodeFormatter(const TextEditor::TabSettings &tabSettings); void setIndentSize(int size); diff --git a/src/plugins/qmljseditor/qmljscomponentfromobjectdef.cpp b/src/plugins/qmljseditor/qmljscomponentfromobjectdef.cpp index 9a831acfe409f797905a5a518e0a4327a4e0484f..3b49624be488af88db86e9f6b8f81df959420d49 100644 --- a/src/plugins/qmljseditor/qmljscomponentfromobjectdef.cpp +++ b/src/plugins/qmljseditor/qmljscomponentfromobjectdef.cpp @@ -118,7 +118,6 @@ public: changes.replace(start, end, m_componentName + QLatin1String(" {\n")); currentFile->change(changes); currentFile->indent(range(start, end + 1)); - } }; diff --git a/src/plugins/qmljseditor/qmljseditor.cpp b/src/plugins/qmljseditor/qmljseditor.cpp index 6119e11999f3190d3e1160ed25107c274a0a802a..d3b41cb087d35b2fe5ee55f283b3724784acdbae 100644 --- a/src/plugins/qmljseditor/qmljseditor.cpp +++ b/src/plugins/qmljseditor/qmljseditor.cpp @@ -1276,21 +1276,13 @@ bool QmlJSTextEditor::isClosingBrace(const QList<Token> &tokens) const 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) { Q_UNUSED(doc) Q_UNUSED(typedChar) const TextEditor::TabSettings &ts = tabSettings(); - QmlJSEditor::QtStyleCodeFormatter codeFormatter = setupCodeFormatter(ts); + QmlJSEditor::QtStyleCodeFormatter codeFormatter(ts); codeFormatter.updateStateUntil(block); const int depth = codeFormatter.indentFor(block); diff --git a/src/plugins/qmljseditor/qmljseditorcodeformatter.cpp b/src/plugins/qmljseditor/qmljseditorcodeformatter.cpp index 638b4fdd7303fccfae9d5da07710528e6b31ac21..0547253553108a0188f7466e402a35286dea6bd1 100644 --- a/src/plugins/qmljseditor/qmljseditorcodeformatter.cpp +++ b/src/plugins/qmljseditor/qmljseditorcodeformatter.cpp @@ -29,6 +29,8 @@ #include "qmljseditorcodeformatter.h" +#include <texteditor/tabsettings.h> + #include <QtCore/QDebug> using namespace QmlJS; @@ -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) { m_indentSize = size; diff --git a/src/plugins/qmljseditor/qmljseditorcodeformatter.h b/src/plugins/qmljseditor/qmljseditorcodeformatter.h index a9aed6d05db743b5eda378738b046946ffaf1fc2..baf663c723b2a26702c9749200aacabeefc98fbb 100644 --- a/src/plugins/qmljseditor/qmljseditorcodeformatter.h +++ b/src/plugins/qmljseditor/qmljseditorcodeformatter.h @@ -35,12 +35,17 @@ #include <texteditor/basetextdocumentlayout.h> #include <qmljs/qmljscodeformatter.h> +namespace TextEditor { + class TabSettings; +} + namespace QmlJSEditor { class QMLJSEDITOR_EXPORT QtStyleCodeFormatter : public QmlJS::CodeFormatter { public: QtStyleCodeFormatter(); + explicit QtStyleCodeFormatter(const TextEditor::TabSettings &tabSettings); void setIndentSize(int size); diff --git a/src/plugins/qmljseditor/qmljsrefactoringchanges.cpp b/src/plugins/qmljseditor/qmljsrefactoringchanges.cpp index d3f420fc6933414225425376c44bf0ec5f89e940..4f99c3a7f5891560b3e46651e5fa40949e7764cc 100644 --- a/src/plugins/qmljseditor/qmljsrefactoringchanges.cpp +++ b/src/plugins/qmljseditor/qmljsrefactoringchanges.cpp @@ -54,7 +54,7 @@ void QmlJSRefactoringChanges::indentSelection(const QTextCursor &selection) cons const QTextBlock end = doc->findBlock(selection.selectionEnd()).next(); const TextEditor::TabSettings &tabSettings(TextEditor::TextEditorSettings::instance()->tabSettings()); - QtStyleCodeFormatter codeFormatter; + QtStyleCodeFormatter codeFormatter(tabSettings); codeFormatter.updateStateUntil(block); do {