Make C++ code style configurable.

Change-Id: Iaf08edb2361146e6b5e1cbafdb716a23c938875b
Done-with: Jarek Kobus
Task-number: QTCREATORBUG-2670
Task-number: QTCREATORBUG-4310
Task-number: QTCREATORBUG-2763
Task-number: QTCREATORBUG-3623
Task-number: QTCREATORBUG-567
Reviewed-on: http://codereview.qt.nokia.com/74
Reviewed-by: Leandro T. C. Melo <leandro.melo@nokia.com>
Reviewed-by: Jarek Kobus <jaroslaw.kobus@nokia.com>
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
This commit is contained in:
Christian Kamm
2011-02-03 15:48:14 +01:00
parent f69eb52944
commit 779fafcbfe
87 changed files with 5036 additions and 979 deletions

View File

@@ -44,6 +44,8 @@
#include <QtGui/QTextCursor>
#include <QtGui/QTextBlock>
#include "cppcodestylesettingspage.h"
using namespace CPlusPlus;
using namespace CppTools;
using namespace TextEditor;
@@ -177,26 +179,25 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
} break;
case declaration_start:
if (tryExpression(true))
break;
switch (kind) {
case T_RBRACE: leave(true); continue;
case T_SEMICOLON: leave(true); break;
case T_EQUAL: enter(initializer); break;
case T_EQUAL: enter(assign_open_or_initializer); break;
case T_LBRACE: enter(defun_open); break;
case T_COLON: enter(member_init_open); enter(member_init); break;
case T_OPERATOR: enter(operator_declaration); break;
default: tryExpression(true); break;
} break;
case initializer:
case assign_open_or_initializer:
switch (kind) {
case T_LBRACE: enter(brace_list_open); break;
default: turnInto(expression); continue;
case T_RBRACE: leave(true); continue;
case T_SEMICOLON: leave(); continue;
default: enter(assign_open); continue;
} break;
case expression:
if (tryExpression())
break;
switch (kind) {
case T_RBRACE: leave(true); continue;
case T_SEMICOLON: leave(); continue;
@@ -208,30 +209,34 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
continue;
}
break;
default: tryExpression(); break;
} break;
case assign_open:
switch (kind) {
case T_RBRACE: leave(true); continue;
case T_SEMICOLON: leave(); continue;
default: tryExpression(); break;
} break;
case arglist_open:
if (tryExpression())
break;
switch (kind) {
case T_SEMICOLON: leave(true); break;
case T_RBRACE: leave(true); continue;
case T_RPAREN: leave(); break;
default: tryExpression(); break;
} break;
case ternary_op:
if (tryExpression())
break;
switch (kind) {
case T_RPAREN:
case T_COMMA:
case T_SEMICOLON: leave(); continue; // always nested, propagate
default: tryExpression(); break;
} break;
case stream_op:
case stream_op_cont:
if (kind != T_LESS_LESS && kind != T_GREATER_GREATER && tryExpression())
break;
switch (kind) {
case T_LESS_LESS:
case T_GREATER_GREATER:
@@ -243,6 +248,7 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
case T_RPAREN:
case T_COMMA:
case T_SEMICOLON: leave(); continue; // always nested, propagate
default: tryExpression(); break;
} break;
case member_init_open:
@@ -261,11 +267,10 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
} break;
case member_init_paren_open:
if (tryExpression())
break;
switch (kind) {
case T_RPAREN: leave(); continue;
case T_SEMICOLON: leave(); continue; // try to recover
default: tryExpression(); break;
} break;
case defun_open:
@@ -681,6 +686,22 @@ bool CodeFormatter::tryExpression(bool alsoExpression)
case T_LPAREN: newState = arglist_open; break;
case T_QUESTION: newState = ternary_op; break;
case T_EQUAL:
case T_AMPER_EQUAL:
case T_CARET_EQUAL:
case T_SLASH_EQUAL:
case T_EXCLAIM_EQUAL:
case T_GREATER_GREATER_EQUAL:
case T_LESS_LESS_EQUAL:
case T_MINUS_EQUAL:
case T_PERCENT_EQUAL:
case T_PIPE_EQUAL:
case T_PLUS_EQUAL:
case T_STAR_EQUAL:
case T_TILDE_EQUAL:
newState = assign_open;
break;
case T_LESS_LESS:
case T_GREATER_GREATER:
newState = stream_op;
@@ -977,63 +998,26 @@ namespace Internal {
}
QtStyleCodeFormatter::QtStyleCodeFormatter()
: m_indentSize(4)
, m_indentSubstatementBraces(false)
, m_indentSubstatementStatements(true)
, m_indentDeclarationBraces(false)
, m_indentDeclarationMembers(true)
{
}
QtStyleCodeFormatter::QtStyleCodeFormatter(const TextEditor::TabSettings &tabSettings)
: m_indentSize(tabSettings.m_indentSize)
, m_indentSubstatementBraces(false)
, m_indentSubstatementStatements(true)
, m_indentDeclarationBraces(false)
, m_indentDeclarationMembers(true)
QtStyleCodeFormatter::QtStyleCodeFormatter(const TextEditor::TabSettings &tabSettings,
const CppCodeStyleSettings &settings)
: m_tabSettings(tabSettings)
, m_styleSettings(settings)
{
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::setTabSettings(const TextEditor::TabSettings &tabSettings)
{
m_indentSize = size;
m_tabSettings = tabSettings;
setTabSize(tabSettings.m_tabSize);
}
void QtStyleCodeFormatter::setIndentSubstatementBraces(bool onOff)
void QtStyleCodeFormatter::setCodeStyleSettings(const CppCodeStyleSettings &settings)
{
m_indentSubstatementBraces = onOff;
}
void QtStyleCodeFormatter::setIndentSubstatementStatements(bool onOff)
{
m_indentSubstatementStatements = onOff;
}
void QtStyleCodeFormatter::setIndentDeclarationBraces(bool onOff)
{
m_indentDeclarationBraces = onOff;
}
void QtStyleCodeFormatter::setIndentDeclarationMembers(bool onOff)
{
m_indentDeclarationMembers = onOff;
m_styleSettings = settings;
}
void QtStyleCodeFormatter::saveBlockData(QTextBlock *block, const BlockData &data) const
@@ -1100,7 +1084,7 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
*savedIndentDepth = tokenPosition;
*indentDepth = tokenPosition;
}
*paddingDepth = 2*m_indentSize;
*paddingDepth = 2*m_tabSettings.m_indentSize;
break;
case template_param:
@@ -1108,9 +1092,9 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
*paddingDepth = nextTokenPosition-*indentDepth;
else {
if (*paddingDepth == 0)
*paddingDepth = 2*m_indentSize;
*paddingDepth = 2*m_tabSettings.m_indentSize;
else
*paddingDepth += m_indentSize;
*paddingDepth += m_tabSettings.m_indentSize;
}
break;
@@ -1121,7 +1105,7 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
case return_statement:
if (firstToken)
*indentDepth = *savedIndentDepth = tokenPosition;
*paddingDepth = 2*m_indentSize;
*paddingDepth = 2*m_tabSettings.m_indentSize;
break;
case declaration_start:
@@ -1133,25 +1117,36 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
// after the return type in "void\nfoo() {}"
for (int i = 0; state(i).type != topmost_intro; ++i) {
if (state(i).type == defun_open) {
*paddingDepth = 2*m_indentSize;
*paddingDepth = 2*m_tabSettings.m_indentSize;
break;
}
}
break;
case assign_open:
if (parentState.type == assign_open_or_initializer)
break;
// fallthrough
case assign_open_or_initializer:
if (!lastToken && m_styleSettings.alignAssignments)
*paddingDepth = nextTokenPosition-*indentDepth;
else
*paddingDepth = 2*m_tabSettings.m_indentSize;
break;
case arglist_open:
case condition_paren_open:
if (!lastToken)
*paddingDepth = nextTokenPosition-*indentDepth;
else
*paddingDepth += m_indentSize;
*paddingDepth += m_tabSettings.m_indentSize;
break;
case ternary_op:
if (!lastToken)
*paddingDepth = spaceOrNextTokenPosition-*indentDepth;
else
*paddingDepth += m_indentSize;
*paddingDepth += m_tabSettings.m_indentSize;
break;
case stream_op:
@@ -1169,7 +1164,7 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
if (firstToken)
*paddingDepth = tokenPosition-*indentDepth;
else
*paddingDepth = m_indentSize - 2; // they'll get another 2 from member_init
*paddingDepth = m_tabSettings.m_indentSize - 2; // they'll get another 2 from member_init
break;
case member_init:
@@ -1177,24 +1172,45 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
break;
case member_init_paren_open:
*paddingDepth += m_indentSize;
*paddingDepth += m_tabSettings.m_indentSize;
break;
case case_cont:
*indentDepth += m_indentSize;
if (m_styleSettings.indentStatementsRelativeToSwitchLabels)
*indentDepth += m_tabSettings.m_indentSize;
break;
case namespace_open:
case class_open:
case enum_open:
case defun_open: {
// undo the continuation indent of the parent
*savedPaddingDepth = 0;
// whether the { is followed by a non-comment token
bool followedByData = (!lastToken && !tokenAt(tokenIndex() + 1).isComment());
if (followedByData)
*savedPaddingDepth = tokenPosition-*indentDepth;
*savedPaddingDepth = tokenPosition-*indentDepth; // pad the } to align with the {
*indentDepth += m_indentSize;
if (newState == class_open) {
if (m_styleSettings.indentAccessSpecifiers
|| m_styleSettings.indentDeclarationsRelativeToAccessSpecifiers)
*indentDepth += m_tabSettings.m_indentSize;
if (m_styleSettings.indentAccessSpecifiers && m_styleSettings.indentDeclarationsRelativeToAccessSpecifiers)
*indentDepth += m_tabSettings.m_indentSize;
} else if (newState == defun_open) {
if (m_styleSettings.indentFunctionBody || m_styleSettings.indentFunctionBraces)
*indentDepth += m_tabSettings.m_indentSize;
if (m_styleSettings.indentFunctionBody && m_styleSettings.indentFunctionBraces)
*indentDepth += m_tabSettings.m_indentSize;
} else if (newState == namespace_open) {
if (m_styleSettings.indentNamespaceBody || m_styleSettings.indentNamespaceBraces)
*indentDepth += m_tabSettings.m_indentSize;
if (m_styleSettings.indentNamespaceBody && m_styleSettings.indentNamespaceBraces)
*indentDepth += m_tabSettings.m_indentSize;
} else {
*indentDepth += m_tabSettings.m_indentSize;
}
if (followedByData) {
*paddingDepth = nextTokenPosition-*indentDepth;
@@ -1206,42 +1222,40 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
// undo parent continuation indent
*savedPaddingDepth = 0;
if (firstToken) {
*savedIndentDepth = tokenPosition;
*indentDepth = *savedIndentDepth;
} else if (m_indentSubstatementBraces && !m_indentSubstatementStatements) {
// ### The preceding check is quite arbitrary.
// It actually needs another flag to determine whether the closing curly
// should be indented or not
*indentDepth = *savedIndentDepth += m_indentSize;
}
if (m_indentSubstatementStatements) {
if (parentState.type != switch_statement)
*indentDepth += m_indentSize;
if (parentState.type == switch_statement) {
if (m_styleSettings.indentSwitchLabels)
*indentDepth += m_tabSettings.m_indentSize;
} else {
if (m_styleSettings.indentBlockBody || m_styleSettings.indentBlockBraces)
*indentDepth += m_tabSettings.m_indentSize;
if (m_styleSettings.indentBlockBody && m_styleSettings.indentBlockBraces)
*indentDepth += m_tabSettings.m_indentSize;
}
break;
case brace_list_open:
if (!lastToken) {
if (parentState.type == initializer)
if (parentState.type == assign_open_or_initializer)
*savedPaddingDepth = tokenPosition-*indentDepth;
*paddingDepth = nextTokenPosition-*indentDepth;
} else {
// avoid existing continuation indents
if (parentState.type == initializer)
if (parentState.type == assign_open_or_initializer)
*savedPaddingDepth = state(1).savedPaddingDepth;
*paddingDepth = *savedPaddingDepth + m_indentSize;
*paddingDepth = *savedPaddingDepth + m_tabSettings.m_indentSize;
}
break;
case block_open:
// case_cont already adds some indent, revert it for a block
if (parentState.type == case_cont && !m_indentSubstatementBraces)
*indentDepth = *savedIndentDepth = parentState.savedIndentDepth;
if (parentState.type == case_cont) {
*indentDepth = parentState.savedIndentDepth;
if (m_styleSettings.indentBlocksRelativeToSwitchLabels)
*indentDepth += m_tabSettings.m_indentSize;
}
if (m_indentSubstatementStatements)
*indentDepth += m_indentSize;
if (m_styleSettings.indentBlockBody)
*indentDepth += m_tabSettings.m_indentSize;
break;
case condition_open:
@@ -1250,8 +1264,9 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
*savedPaddingDepth = *paddingDepth;
// fixed extra indent when continuing 'if (', but not for 'else if ('
if (nextTokenPosition-*indentDepth <= m_indentSize)
*paddingDepth = 2*m_indentSize;
if (m_styleSettings.extraPaddingForConditionsIfConfusingAlign
&& nextTokenPosition-*indentDepth <= m_tabSettings.m_indentSize)
*paddingDepth = 2*m_tabSettings.m_indentSize;
else
*paddingDepth = nextTokenPosition-*indentDepth;
break;
@@ -1286,7 +1301,7 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
case cpp_macro:
case cpp_macro_cont:
*indentDepth = m_indentSize;
*indentDepth = m_tabSettings.m_indentSize;
break;
}
@@ -1316,7 +1331,7 @@ void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, i
// adjusting the indentDepth here instead of in enter() gives 'else if' the correct indentation
// ### could be moved?
if (topState.type == substatement)
*indentDepth += m_indentSize;
*indentDepth += m_tabSettings.m_indentSize;
// keep user-adjusted indent in multiline comments
if (topState.type == multiline_comment_start
@@ -1333,7 +1348,7 @@ void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, i
case T_COLON:
// ### ok for constructor initializer lists - what about ? and bitfields?
if (topState.type == expression && previousState.type == declaration_start) {
*paddingDepth = m_indentSize;
*paddingDepth = m_tabSettings.m_indentSize;
} else if (topState.type == ternary_op) {
if (*paddingDepth >= 2)
*paddingDepth -= 2;
@@ -1344,24 +1359,41 @@ void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, i
case T_LBRACE: {
if (topState.type == case_cont) {
*indentDepth = topState.savedIndentDepth;
if (m_indentSubstatementBraces)
*indentDepth += m_indentSize;
if (m_styleSettings.indentBlocksRelativeToSwitchLabels)
*indentDepth += m_tabSettings.m_indentSize;
*paddingDepth = 0;
// function definition - argument list is expression state
} else if (topState.type == expression && previousState.type == declaration_start) {
*indentDepth = previousState.savedIndentDepth;
if (m_indentDeclarationBraces)
*indentDepth += m_indentSize;
// or constructor
} else if ((topState.type == expression && previousState.type == declaration_start)
|| topState.type == member_init || topState.type == member_init_open) {
// the declaration_start indent is the base
if (topState.type == member_init) {
*indentDepth = state(2).savedIndentDepth;
} else {
*indentDepth = previousState.savedIndentDepth;
}
if (m_styleSettings.indentFunctionBraces)
*indentDepth += m_tabSettings.m_indentSize;
*paddingDepth = 0;
} else if (topState.type == class_start) {
*indentDepth = topState.savedIndentDepth;
if (m_indentDeclarationBraces)
*indentDepth += m_indentSize;
if (m_styleSettings.indentClassBraces)
*indentDepth += m_tabSettings.m_indentSize;
*paddingDepth = 0;
} else if (topState.type == enum_start) {
*indentDepth = topState.savedIndentDepth;
if (m_styleSettings.indentEnumBraces)
*indentDepth += m_tabSettings.m_indentSize;
*paddingDepth = 0;
} else if (topState.type == namespace_start) {
*indentDepth = topState.savedIndentDepth;
if (m_styleSettings.indentNamespaceBraces)
*indentDepth += m_tabSettings.m_indentSize;
*paddingDepth = 0;
} else if (topState.type == substatement) {
*indentDepth = topState.savedIndentDepth;
if (m_indentSubstatementBraces)
*indentDepth += m_indentSize;
if (m_styleSettings.indentBlockBraces)
*indentDepth += m_tabSettings.m_indentSize;
*paddingDepth = 0;
} else if (topState.type != defun_open
&& topState.type != block_open
@@ -1376,8 +1408,10 @@ void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, i
}
case T_RBRACE: {
if (topState.type == block_open && previousState.type == case_cont) {
*indentDepth = topState.savedIndentDepth;
*paddingDepth = topState.savedPaddingDepth;
*indentDepth = previousState.savedIndentDepth;
*paddingDepth = previousState.savedPaddingDepth;
if (m_styleSettings.indentBlocksRelativeToSwitchLabels)
*indentDepth += m_tabSettings.m_indentSize;
break;
}
for (int i = 0; state(i).type != topmost_intro; ++i) {
@@ -1386,17 +1420,18 @@ void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, i
|| type == namespace_open
|| type == extern_open
|| type == enum_open
|| type == defun_open) {
*indentDepth = state(i).savedIndentDepth;
if (m_indentDeclarationBraces)
*indentDepth += m_indentSize;
*paddingDepth = state(i).savedPaddingDepth;
break;
} else if (type == substatement_open
|| type == brace_list_open
|| type == block_open) {
|| type == defun_open
|| type == substatement_open
|| type == brace_list_open
|| type == block_open) {
*indentDepth = state(i).savedIndentDepth;
*paddingDepth = state(i).savedPaddingDepth;
if ((type == defun_open && m_styleSettings.indentFunctionBraces)
|| (type == class_open && m_styleSettings.indentClassBraces)
|| (type == namespace_open && m_styleSettings.indentNamespaceBraces)
|| (type == enum_open && m_styleSettings.indentEnumBraces)
|| (type == substatement_open && m_styleSettings.indentBlockBraces))
*indentDepth += m_tabSettings.m_indentSize;
break;
}
}
@@ -1411,13 +1446,12 @@ void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, i
// break;
case T_DEFAULT:
case T_CASE: {
int lastSubstatementIndent = 0;
for (int i = 0; state(i).type != topmost_intro; ++i) {
const int type = state(i).type;
if (type == substatement_open) {
lastSubstatementIndent = state(i).savedIndentDepth;
} else if (type == switch_statement) {
*indentDepth = lastSubstatementIndent;
if (type == switch_statement) {
*indentDepth = state(i).savedIndentDepth;
if (m_styleSettings.indentSwitchLabels)
*indentDepth += m_tabSettings.m_indentSize;
break;
} else if (type == case_cont) {
*indentDepth = state(i).savedIndentDepth;
@@ -1432,9 +1466,13 @@ void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, i
case T_PRIVATE:
case T_PROTECTED:
case T_Q_SIGNALS:
if (topState.type == class_open) {
if (tokenAt(1).is(T_COLON) || tokenAt(2).is(T_COLON))
if (m_styleSettings.indentDeclarationsRelativeToAccessSpecifiers
&& topState.type == class_open) {
if (tokenAt(1).is(T_COLON) || tokenAt(2).is(T_COLON)) {
*indentDepth = topState.savedIndentDepth;
if (m_styleSettings.indentAccessSpecifiers)
*indentDepth += m_tabSettings.m_indentSize;
}
}
break;
case T_ELSE:
@@ -1460,8 +1498,8 @@ void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, i
&& (kind == T_COMMENT || kind == T_DOXY_COMMENT)
&& (lexerState == Lexer::State_Default
|| tokens.size() != 1)) {
if (*indentDepth >= m_indentSize)
*indentDepth -= m_indentSize;
if (*indentDepth >= m_tabSettings.m_indentSize)
*indentDepth -= m_tabSettings.m_indentSize;
else
*indentDepth = 0;
}
@@ -1476,7 +1514,18 @@ void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, i
*indentDepth = 0;
}
break;
case T_BREAK:
case T_CONTINUE:
case T_RETURN:
if (topState.type == case_cont) {
*indentDepth = topState.savedIndentDepth;
if (m_styleSettings.indentControlFlowRelativeToSwitchLabels)
*indentDepth += m_tabSettings.m_indentSize;
}
}
// ensure padding and indent are >= 0
*indentDepth = qMax(0, *indentDepth);
*paddingDepth = qMax(0, *paddingDepth);
}
bool QtStyleCodeFormatter::shouldClearPaddingOnEnter(int state)

View File

@@ -35,8 +35,10 @@
#include "cpptools_global.h"
#include <cplusplus/SimpleLexer.h>
#include <Token.h>
#include <cplusplus/SimpleLexer.h>
#include <texteditor/tabsettings.h>
#include <cpptools/cppcodestylesettings.h>
#include <QtCore/QChar>
#include <QtCore/QStack>
@@ -49,10 +51,6 @@ class QTextDocument;
class QTextBlock;
QT_END_NAMESPACE
namespace TextEditor {
class TabSettings;
}
namespace CppTools {
namespace Internal {
class CppCodeFormatterData;
@@ -176,7 +174,7 @@ public: // must be public to make Q_GADGET introspection work
assign_open, // after an assignment token
expression, // after a '=' in a declaration_start once we're sure it's not '= {'
initializer // after a '=' in a declaration start
assign_open_or_initializer // after a '=' in a declaration start
};
Q_ENUMS(StateType)
@@ -261,14 +259,11 @@ class CPPTOOLS_EXPORT QtStyleCodeFormatter : public CodeFormatter
{
public:
QtStyleCodeFormatter();
explicit QtStyleCodeFormatter(const TextEditor::TabSettings &tabSettings);
QtStyleCodeFormatter(const TextEditor::TabSettings &tabSettings,
const CppCodeStyleSettings &settings);
void setIndentSize(int size);
void setIndentSubstatementBraces(bool onOff);
void setIndentSubstatementStatements(bool onOff);
void setIndentDeclarationBraces(bool onOff);
void setIndentDeclarationMembers(bool onOff);
void setTabSettings(const TextEditor::TabSettings &tabSettings);
void setCodeStyleSettings(const CppCodeStyleSettings &settings);
protected:
virtual void onEnter(int newState, int *indentDepth, int *savedIndentDepth, int *paddingDepth, int *savedPaddingDepth) const;
@@ -283,11 +278,8 @@ protected:
static bool shouldClearPaddingOnEnter(int state);
private:
int m_indentSize;
bool m_indentSubstatementBraces;
bool m_indentSubstatementStatements;
bool m_indentDeclarationBraces;
bool m_indentDeclarationMembers;
TextEditor::TabSettings m_tabSettings;
CppCodeStyleSettings m_styleSettings;
};
} // namespace CppTools

View File

@@ -0,0 +1,86 @@
#include "cppcodestylepreferences.h"
using namespace CppTools;
static const char *settingsSuffixKey = "CodeStyleSettings";
static const char *currentFallbackKey = "CurrentFallback";
CppCodeStylePreferences::CppCodeStylePreferences(const QList<TextEditor::IFallbackPreferences *> &fallbacks, QObject *parent) :
IFallbackPreferences(fallbacks, parent)
{
connect(this, SIGNAL(currentValueChanged(QVariant)),
this, SLOT(slotCurrentValueChanged(QVariant)));
}
QVariant CppCodeStylePreferences::value() const
{
QVariant v;
v.setValue(settings());
return v;
}
void CppCodeStylePreferences::setValue(const QVariant &data)
{
if (!data.canConvert<CppCodeStyleSettings>())
return;
setSettings(data.value<CppCodeStyleSettings>());
}
CppCodeStyleSettings CppCodeStylePreferences::settings() const
{
return m_data;
}
void CppCodeStylePreferences::setSettings(const CppCodeStyleSettings &data)
{
if (m_data == data)
return;
m_data = data;
QVariant v;
v.setValue(data);
emit valueChanged(v);
emit settingsChanged(m_data);
if (!currentFallback()) {
emit currentValueChanged(v);
}
}
CppCodeStyleSettings CppCodeStylePreferences::currentSettings() const
{
QVariant v = currentValue();
if (!v.canConvert<CppCodeStyleSettings>()) {
// warning
return CppCodeStyleSettings();
}
return v.value<CppCodeStyleSettings>();
}
void CppCodeStylePreferences::slotCurrentValueChanged(const QVariant &value)
{
if (!value.canConvert<CppCodeStyleSettings>())
return;
emit currentSettingsChanged(value.value<CppCodeStyleSettings>());
}
QString CppCodeStylePreferences::settingsSuffix() const
{
return settingsSuffixKey;
}
void CppCodeStylePreferences::toMap(const QString &prefix, QVariantMap *map) const
{
m_data.toMap(prefix, map);
map->insert(prefix + QLatin1String(currentFallbackKey), currentFallbackId());
}
void CppCodeStylePreferences::fromMap(const QString &prefix, const QVariantMap &map)
{
m_data.fromMap(prefix, map);
setCurrentFallback(map.value(prefix + QLatin1String(currentFallbackKey), QLatin1String("Global")).toString());
}

View File

@@ -0,0 +1,48 @@
#ifndef CPPCODESTYLEPREFERENCES_H
#define CPPCODESTYLEPREFERENCES_H
#include "cpptools_global.h"
#include "cppcodestylesettings.h"
#include <texteditor/ifallbackpreferences.h>
namespace CppTools {
class CPPTOOLS_EXPORT CppCodeStylePreferences : public TextEditor::IFallbackPreferences
{
Q_OBJECT
public:
explicit CppCodeStylePreferences(
const QList<TextEditor::IFallbackPreferences *> &fallbacks,
QObject *parent = 0);
virtual QVariant value() const;
virtual void setValue(const QVariant &);
CppCodeStyleSettings settings() const;
// tracks parent hierarchy until currentParentSettings is null
CppCodeStyleSettings currentSettings() const;
virtual void toMap(const QString &prefix, QVariantMap *map) const;
virtual void fromMap(const QString &prefix, const QVariantMap &map);
public slots:
void setSettings(const CppTools::CppCodeStyleSettings &data);
signals:
void settingsChanged(const CppTools::CppCodeStyleSettings &);
void currentSettingsChanged(const CppTools::CppCodeStyleSettings &);
protected:
virtual QString settingsSuffix() const;
private slots:
void slotCurrentValueChanged(const QVariant &);
private:
CppCodeStyleSettings m_data;
};
} // namespace CppTools
#endif // CPPCODESTYLEPREFERENCES_H

View File

@@ -0,0 +1,133 @@
#include "cppcodestylesettings.h"
#include <utils/settingsutils.h>
static const char *groupPostfix = "IndentSettings";
static const char *indentBlockBracesKey = "IndentBlockBraces";
static const char *indentBlockBodyKey = "IndentBlockBody";
static const char *indentClassBracesKey = "IndentClassBraces";
static const char *indentEnumBracesKey = "IndentEnumBraces";
static const char *indentNamespaceBracesKey = "IndentNamespaceBraces";
static const char *indentNamespaceBodyKey = "IndentNamespaceBody";
static const char *indentAccessSpecifiersKey = "IndentAccessSpecifiers";
static const char *indentDeclarationsRelativeToAccessSpecifiersKey = "IndentDeclarationsRelativeToAccessSpecifiers";
static const char *indentFunctionBodyKey = "IndentFunctionBody";
static const char *indentFunctionBracesKey = "IndentFunctionBraces";
static const char *indentSwitchLabelsKey = "IndentSwitchLabels";
static const char *indentStatementsRelativeToSwitchLabelsKey = "IndentStatementsRelativeToSwitchLabels";
static const char *indentBlocksRelativeToSwitchLabelsKey = "IndentBlocksRelativeToSwitchLabels";
static const char *indentControlFlowRelativeToSwitchLabelsKey = "IndentControlFlowRelativeToSwitchLabels";
static const char *extraPaddingForConditionsIfConfusingAlignKey = "ExtraPaddingForConditionsIfConfusingAlign";
static const char *alignAssignmentsKey = "AlignAssignments";
using namespace CppTools;
// ------------------ CppCodeStyleSettingsWidget
CppCodeStyleSettings::CppCodeStyleSettings() :
indentBlockBraces(false)
, indentBlockBody(true)
, indentClassBraces(false)
, indentEnumBraces(false)
, indentNamespaceBraces(false)
, indentNamespaceBody(false)
, indentAccessSpecifiers(false)
, indentDeclarationsRelativeToAccessSpecifiers(true)
, indentFunctionBody(true)
, indentFunctionBraces(false)
, indentSwitchLabels(false)
, indentStatementsRelativeToSwitchLabels(true)
, indentBlocksRelativeToSwitchLabels(false)
, indentControlFlowRelativeToSwitchLabels(true)
, extraPaddingForConditionsIfConfusingAlign(true)
, alignAssignments(false)
{
}
void CppCodeStyleSettings::toSettings(const QString &category, QSettings *s) const
{
Utils::toSettings(QLatin1String(groupPostfix), category, s, this);
}
void CppCodeStyleSettings::fromSettings(const QString &category, const QSettings *s)
{
*this = CppCodeStyleSettings(); // Assign defaults
Utils::fromSettings(QLatin1String(groupPostfix), category, s, this);
}
void CppCodeStyleSettings::toMap(const QString &prefix, QVariantMap *map) const
{
map->insert(prefix + QLatin1String(indentBlockBracesKey), indentBlockBraces);
map->insert(prefix + QLatin1String(indentBlockBodyKey), indentBlockBody);
map->insert(prefix + QLatin1String(indentClassBracesKey), indentClassBraces);
map->insert(prefix + QLatin1String(indentEnumBracesKey), indentEnumBraces);
map->insert(prefix + QLatin1String(indentNamespaceBracesKey), indentNamespaceBraces);
map->insert(prefix + QLatin1String(indentNamespaceBodyKey), indentNamespaceBody);
map->insert(prefix + QLatin1String(indentAccessSpecifiersKey), indentAccessSpecifiers);
map->insert(prefix + QLatin1String(indentDeclarationsRelativeToAccessSpecifiersKey), indentDeclarationsRelativeToAccessSpecifiers);
map->insert(prefix + QLatin1String(indentFunctionBodyKey), indentFunctionBody);
map->insert(prefix + QLatin1String(indentFunctionBracesKey), indentFunctionBraces);
map->insert(prefix + QLatin1String(indentSwitchLabelsKey), indentSwitchLabels);
map->insert(prefix + QLatin1String(indentStatementsRelativeToSwitchLabelsKey), indentStatementsRelativeToSwitchLabels);
map->insert(prefix + QLatin1String(indentBlocksRelativeToSwitchLabelsKey), indentBlocksRelativeToSwitchLabels);
map->insert(prefix + QLatin1String(indentControlFlowRelativeToSwitchLabelsKey), indentControlFlowRelativeToSwitchLabels);
map->insert(prefix + QLatin1String(extraPaddingForConditionsIfConfusingAlignKey), extraPaddingForConditionsIfConfusingAlign);
map->insert(prefix + QLatin1String(alignAssignmentsKey), alignAssignments);
}
void CppCodeStyleSettings::fromMap(const QString &prefix, const QVariantMap &map)
{
indentBlockBraces = map.value(prefix + QLatin1String(indentBlockBracesKey),
indentBlockBraces).toBool();
indentBlockBody = map.value(prefix + QLatin1String(indentBlockBodyKey),
indentBlockBody).toBool();
indentClassBraces = map.value(prefix + QLatin1String(indentClassBracesKey),
indentClassBraces).toBool();
indentEnumBraces = map.value(prefix + QLatin1String(indentEnumBracesKey),
indentEnumBraces).toBool();
indentNamespaceBraces = map.value(prefix + QLatin1String(indentNamespaceBracesKey),
indentNamespaceBraces).toBool();
indentNamespaceBody = map.value(prefix + QLatin1String(indentNamespaceBodyKey),
indentNamespaceBody).toBool();
indentAccessSpecifiers = map.value(prefix + QLatin1String(indentAccessSpecifiersKey),
indentAccessSpecifiers).toBool();
indentDeclarationsRelativeToAccessSpecifiers = map.value(prefix + QLatin1String(indentDeclarationsRelativeToAccessSpecifiersKey),
indentDeclarationsRelativeToAccessSpecifiers).toBool();
indentFunctionBody = map.value(prefix + QLatin1String(indentFunctionBodyKey),
indentFunctionBody).toBool();
indentFunctionBraces = map.value(prefix + QLatin1String(indentFunctionBracesKey),
indentFunctionBraces).toBool();
indentSwitchLabels = map.value(prefix + QLatin1String(indentSwitchLabelsKey),
indentSwitchLabels).toBool();
indentStatementsRelativeToSwitchLabels = map.value(prefix + QLatin1String(indentStatementsRelativeToSwitchLabelsKey),
indentStatementsRelativeToSwitchLabels).toBool();
indentBlocksRelativeToSwitchLabels = map.value(prefix + QLatin1String(indentBlocksRelativeToSwitchLabelsKey),
indentBlocksRelativeToSwitchLabels).toBool();
indentControlFlowRelativeToSwitchLabels = map.value(prefix + QLatin1String(indentControlFlowRelativeToSwitchLabelsKey),
indentControlFlowRelativeToSwitchLabels).toBool();
extraPaddingForConditionsIfConfusingAlign = map.value(prefix + QLatin1String(extraPaddingForConditionsIfConfusingAlignKey),
extraPaddingForConditionsIfConfusingAlign).toBool();
alignAssignments = map.value(prefix + QLatin1String(alignAssignmentsKey),
alignAssignments).toBool();
}
bool CppCodeStyleSettings::equals(const CppCodeStyleSettings &rhs) const
{
return indentBlockBraces == rhs.indentBlockBraces
&& indentBlockBody == rhs.indentBlockBody
&& indentClassBraces == rhs.indentClassBraces
&& indentEnumBraces == rhs.indentEnumBraces
&& indentNamespaceBraces == rhs.indentNamespaceBraces
&& indentNamespaceBody == rhs.indentNamespaceBody
&& indentAccessSpecifiers == rhs.indentAccessSpecifiers
&& indentDeclarationsRelativeToAccessSpecifiers == rhs.indentDeclarationsRelativeToAccessSpecifiers
&& indentFunctionBody == rhs.indentFunctionBody
&& indentFunctionBraces == rhs.indentFunctionBraces
&& indentSwitchLabels == rhs.indentSwitchLabels
&& indentStatementsRelativeToSwitchLabels == rhs.indentStatementsRelativeToSwitchLabels
&& indentBlocksRelativeToSwitchLabels == rhs.indentBlocksRelativeToSwitchLabels
&& indentControlFlowRelativeToSwitchLabels == rhs.indentControlFlowRelativeToSwitchLabels
&& extraPaddingForConditionsIfConfusingAlign == rhs.extraPaddingForConditionsIfConfusingAlign
&& alignAssignments == rhs.alignAssignments;
}

View File

@@ -0,0 +1,67 @@
#ifndef CPPCODESTYLESETTINGS_H
#define CPPCODESTYLESETTINGS_H
#include "cpptools_global.h"
#include <QtCore/QMetaType>
#include <QtCore/QVariant>
QT_BEGIN_NAMESPACE
class QSettings;
QT_END_NAMESPACE
namespace CppTools {
struct CPPTOOLS_EXPORT CppCodeStyleSettings
{
CppCodeStyleSettings();
bool indentBlockBraces;
bool indentBlockBody;
bool indentClassBraces;
bool indentEnumBraces;
bool indentNamespaceBraces;
bool indentNamespaceBody;
bool indentAccessSpecifiers;
bool indentDeclarationsRelativeToAccessSpecifiers;
bool indentFunctionBody;
bool indentFunctionBraces;
bool indentSwitchLabels;
bool indentStatementsRelativeToSwitchLabels;
bool indentBlocksRelativeToSwitchLabels;
bool indentControlFlowRelativeToSwitchLabels;
// false: if (a &&
// b)
// c;
// true: if (a &&
// b)
// c;
// but always: while (a &&
// b)
// foo;
bool extraPaddingForConditionsIfConfusingAlign;
// false: a = a +
// b;
// true: a = a +
// b
bool alignAssignments;
void toSettings(const QString &category, QSettings *s) const;
void fromSettings(const QString &category, const QSettings *s);
void toMap(const QString &prefix, QVariantMap *map) const;
void fromMap(const QString &prefix, const QVariantMap &map);
bool equals(const CppCodeStyleSettings &rhs) const;
};
inline bool operator==(const CppCodeStyleSettings &s1, const CppCodeStyleSettings &s2) { return s1.equals(s2); }
inline bool operator!=(const CppCodeStyleSettings &s1, const CppCodeStyleSettings &s2) { return !s1.equals(s2); }
} // namespace CppTools
Q_DECLARE_METATYPE(CppTools::CppCodeStyleSettings)
#endif // CPPCODESTYLESETTINGS_H

View File

@@ -0,0 +1,45 @@
#include "cppcodestylesettingsfactory.h"
#include "cppcodestylesettings.h"
#include "cppcodestylesettingspage.h"
#include "cppcodestylepreferences.h"
#include "cpptoolsconstants.h"
#include <texteditor/tabpreferences.h>
#include <texteditor/tabsettings.h>
#include <QtGui/QLayout>
using namespace CppTools;
CppCodeStylePreferencesFactory::CppCodeStylePreferencesFactory()
{
}
QString CppCodeStylePreferencesFactory::languageId()
{
return Constants::CPP_SETTINGS_ID;
}
QString CppCodeStylePreferencesFactory::displayName()
{
return Constants::CPP_SETTINGS_NAME;
}
TextEditor::IFallbackPreferences *CppCodeStylePreferencesFactory::createPreferences(
const QList<TextEditor::IFallbackPreferences *> &fallbacks) const
{
return new CppCodeStylePreferences(fallbacks);
}
QWidget *CppCodeStylePreferencesFactory::createEditor(TextEditor::IFallbackPreferences *preferences,
TextEditor::TabPreferences *tabPreferences,
QWidget *parent) const
{
CppCodeStylePreferences *cppPreferences = qobject_cast<CppCodeStylePreferences *>(preferences);
if (!cppPreferences)
return 0;
Internal::CppCodeStylePreferencesWidget *widget = new Internal::CppCodeStylePreferencesWidget(parent);
widget->layout()->setMargin(0);
widget->setCppCodeStylePreferences(cppPreferences);
widget->setTabPreferences(tabPreferences);
return widget;
}

View File

@@ -0,0 +1,24 @@
#ifndef CPPCODESTYLESETTINGSFACTORY_H
#define CPPCODESTYLESETTINGSFACTORY_H
#include <texteditor/icodestylepreferencesfactory.h>
namespace CppTools {
class CppCodeStylePreferencesFactory : public TextEditor::ICodeStylePreferencesFactory
{
public:
CppCodeStylePreferencesFactory();
virtual QString languageId();
virtual QString displayName();
virtual TextEditor::IFallbackPreferences *createPreferences(const QList<TextEditor::IFallbackPreferences *> &fallbacks) const;
virtual QWidget *createEditor(TextEditor::IFallbackPreferences *settings,
TextEditor::TabPreferences *tabSettings,
QWidget *parent) const;
};
} // namespace CppTools
#endif // CPPCODESTYLESETTINGSFACTORY_H

View File

@@ -0,0 +1,536 @@
#include "cppcodestylesettingspage.h"
#include "cppcodestylepreferences.h"
#include "ui_cppcodestylesettingspage.h"
#include "cpptoolsconstants.h"
#include "cpptoolssettings.h"
#include "cppqtstyleindenter.h"
#include <texteditor/snippets/isnippetprovider.h>
#include <texteditor/fontsettings.h>
#include <texteditor/displaysettings.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/tabsettings.h>
#include <texteditor/tabpreferences.h>
#include <extensionsystem/pluginmanager.h>
#include <cppeditor/cppeditorconstants.h>
#include <coreplugin/icore.h>
#include <QtGui/QTextBlock>
#include <QtCore/QTextStream>
static const char *defaultCodeStyleSnippets[] = {
"#include <math.h>\n"
"\n"
"class Complex\n"
" {\n"
"public:\n"
" Complex(double re, double im)\n"
" : _re(re), _im(im)\n"
" {}\n"
" double modulus() const\n"
" {\n"
" return sqrt(_re * _re + _im * _im);\n"
" }\n"
"private:\n"
" double _re;\n"
" double _im;\n"
" };\n"
"\n"
"void bar(int i)\n"
" {\n"
" static int counter = 0;\n"
" counter += i;\n"
" }\n"
"\n"
"namespace Foo\n"
" {\n"
" namespace Bar\n"
" {\n"
" void foo(int a, int b)\n"
" {\n"
" for (int i = 0; i < a; i++)\n"
" {\n"
" if (i < b)\n"
" bar(i);\n"
" else\n"
" {\n"
" bar(i);\n"
" bar(b);\n"
" }\n"
" }\n"
" }\n"
" } // namespace Bar\n"
" } // namespace Foo\n"
,
"#include <math.h>\n"
"\n"
"class Complex\n"
" {\n"
"public:\n"
" Complex(double re, double im)\n"
" : _re(re), _im(im)\n"
" {}\n"
" double modulus() const\n"
" {\n"
" return sqrt(_re * _re + _im * _im);\n"
" }\n"
"private:\n"
" double _re;\n"
" double _im;\n"
" };\n"
"\n"
"void bar(int i)\n"
" {\n"
" static int counter = 0;\n"
" counter += i;\n"
" }\n"
"\n"
"namespace Foo\n"
" {\n"
" namespace Bar\n"
" {\n"
" void foo(int a, int b)\n"
" {\n"
" for (int i = 0; i < a; i++)\n"
" {\n"
" if (i < b)\n"
" bar(i);\n"
" else\n"
" {\n"
" bar(i);\n"
" bar(b);\n"
" }\n"
" }\n"
" }\n"
" } // namespace Bar\n"
" } // namespace Foo\n"
,
"namespace Foo\n"
"{\n"
"namespace Bar\n"
"{\n"
"class FooBar\n"
" {\n"
"public:\n"
" FooBar(int a)\n"
" : _a(a)\n"
" {}\n"
" int calculate() const\n"
" {\n"
" if (a > 10)\n"
" {\n"
" int b = 2 * a;\n"
" return a * b;\n"
" }\n"
" return -a;\n"
" }\n"
"private:\n"
" int _a;\n"
" };\n"
"}\n"
"}\n"
,
"#include \"bar.h\"\n"
"\n"
"int foo(int a)\n"
" {\n"
" switch (a)\n"
" {\n"
" case 1:\n"
" bar(1);\n"
" break;\n"
" case 2:\n"
" {\n"
" bar(2);\n"
" break;\n"
" }\n"
" case 3:\n"
" default:\n"
" bar(3);\n"
" break;\n"
" }\n"
" return 0;\n"
" }\n"
,
"void foo() {\n"
" if (a &&\n"
" b)\n"
" c;\n"
"\n"
" while (a ||\n"
" b)\n"
" break;\n"
" a = b +\n"
" c;\n"
" myInstance.longMemberName +=\n"
" foo;\n"
" myInstance.longMemberName += bar +\n"
" foo;\n"
"}\n"
};
using namespace TextEditor;
namespace CppTools {
namespace Internal {
// ------------------ CppCodeStyleSettingsWidget
CppCodeStylePreferencesWidget::CppCodeStylePreferencesWidget(QWidget *parent)
: QWidget(parent),
m_tabPreferences(0),
m_cppCodeStylePreferences(0),
m_ui(new ::Ui::CppCodeStyleSettingsPage)
{
m_ui->setupUi(this);
m_previews << m_ui->previewTextEditGeneral << m_ui->previewTextEditContent
<< m_ui->previewTextEditBraces << m_ui->previewTextEditSwitch
<< m_ui->previewTextEditPadding;
for (int i = 0; i < m_previews.size(); ++i) {
m_previews[i]->setPlainText(defaultCodeStyleSnippets[i]);
}
const QList<ISnippetProvider *> &providers =
ExtensionSystem::PluginManager::instance()->getObjects<ISnippetProvider>();
foreach (ISnippetProvider *provider, providers) {
if (provider->groupId() == QLatin1String(CppEditor::Constants::CPP_SNIPPETS_GROUP_ID)) {
foreach (TextEditor::SnippetEditorWidget *preview, m_previews)
provider->decorateEditor(preview);
break;
}
}
TextEditor::TextEditorSettings *settings = TextEditorSettings::instance();
setFontSettings(settings->fontSettings());
connect(settings, SIGNAL(fontSettingsChanged(TextEditor::FontSettings)),
this, SLOT(setFontSettings(TextEditor::FontSettings)));
setVisualizeWhitespace(true);
connect(m_ui->indentBlockBraces, SIGNAL(toggled(bool)),
this, SLOT(slotCppCodeStyleSettingsChanged()));
connect(m_ui->indentBlockBody, SIGNAL(toggled(bool)),
this, SLOT(slotCppCodeStyleSettingsChanged()));
connect(m_ui->indentClassBraces, SIGNAL(toggled(bool)),
this, SLOT(slotCppCodeStyleSettingsChanged()));
connect(m_ui->indentNamespaceBraces, SIGNAL(toggled(bool)),
this, SLOT(slotCppCodeStyleSettingsChanged()));
connect(m_ui->indentEnumBraces, SIGNAL(toggled(bool)),
this, SLOT(slotCppCodeStyleSettingsChanged()));
connect(m_ui->indentNamespaceBody, SIGNAL(toggled(bool)),
this, SLOT(slotCppCodeStyleSettingsChanged()));
connect(m_ui->indentSwitchLabels, SIGNAL(toggled(bool)),
this, SLOT(slotCppCodeStyleSettingsChanged()));
connect(m_ui->indentCaseStatements, SIGNAL(toggled(bool)),
this, SLOT(slotCppCodeStyleSettingsChanged()));
connect(m_ui->indentCaseBlocks, SIGNAL(toggled(bool)),
this, SLOT(slotCppCodeStyleSettingsChanged()));
connect(m_ui->indentCaseBreak, SIGNAL(toggled(bool)),
this, SLOT(slotCppCodeStyleSettingsChanged()));
connect(m_ui->indentAccessSpecifiers, SIGNAL(toggled(bool)),
this, SLOT(slotCppCodeStyleSettingsChanged()));
connect(m_ui->indentDeclarationsRelativeToAccessSpecifiers, SIGNAL(toggled(bool)),
this, SLOT(slotCppCodeStyleSettingsChanged()));
connect(m_ui->indentFunctionBody, SIGNAL(toggled(bool)),
this, SLOT(slotCppCodeStyleSettingsChanged()));
connect(m_ui->indentFunctionBraces, SIGNAL(toggled(bool)),
this, SLOT(slotCppCodeStyleSettingsChanged()));
connect(m_ui->extraPaddingConditions, SIGNAL(toggled(bool)),
this, SLOT(slotCppCodeStyleSettingsChanged()));
connect(m_ui->alignAssignments, SIGNAL(toggled(bool)),
this, SLOT(slotCppCodeStyleSettingsChanged()));
m_ui->categoryTab->setCurrentIndex(0);
m_ui->tabPreferencesWidget->setFlat(true);
}
CppCodeStylePreferencesWidget::~CppCodeStylePreferencesWidget()
{
delete m_ui;
}
void CppCodeStylePreferencesWidget::setTabPreferences(TextEditor::TabPreferences *preferences)
{
m_tabPreferences = preferences;
m_ui->tabPreferencesWidget->setTabPreferences(preferences);
connect(m_tabPreferences, SIGNAL(currentSettingsChanged(TextEditor::TabSettings)),
this, SLOT(slotSettingsChanged()));
updatePreview();
}
void CppCodeStylePreferencesWidget::setCppCodeStylePreferences(CppCodeStylePreferences *preferences)
{
m_cppCodeStylePreferences = preferences;
m_ui->fallbackWidget->setFallbackPreferences(preferences);
m_ui->fallbackContainer->setVisible(!m_ui->fallbackWidget->isHidden());
connect(m_cppCodeStylePreferences, SIGNAL(settingsChanged(CppTools::CppCodeStyleSettings)),
this, SLOT(setCppCodeStyleSettings(CppTools::CppCodeStyleSettings)));
connect(m_cppCodeStylePreferences, SIGNAL(currentFallbackChanged(TextEditor::IFallbackPreferences*)),
this, SLOT(slotCurrentFallbackChanged(TextEditor::IFallbackPreferences*)));
connect(this, SIGNAL(cppCodeStyleSettingsChanged(CppTools::CppCodeStyleSettings)),
m_cppCodeStylePreferences, SLOT(setSettings(CppTools::CppCodeStyleSettings)));
setCppCodeStyleSettings(m_cppCodeStylePreferences->settings());
slotCurrentFallbackChanged(m_cppCodeStylePreferences->currentFallback());
connect(m_cppCodeStylePreferences, SIGNAL(currentSettingsChanged(CppTools::CppCodeStyleSettings)),
this, SLOT(slotSettingsChanged()));
updatePreview();
}
CppCodeStyleSettings CppCodeStylePreferencesWidget::cppCodeStyleSettings() const
{
CppCodeStyleSettings set;
set.indentBlockBraces = m_ui->indentBlockBraces->isChecked();
set.indentBlockBody = m_ui->indentBlockBody->isChecked();
set.indentClassBraces = m_ui->indentClassBraces->isChecked();
set.indentEnumBraces = m_ui->indentEnumBraces->isChecked();
set.indentNamespaceBraces = m_ui->indentNamespaceBraces->isChecked();
set.indentNamespaceBody = m_ui->indentNamespaceBody->isChecked();
set.indentAccessSpecifiers = m_ui->indentAccessSpecifiers->isChecked();
set.indentDeclarationsRelativeToAccessSpecifiers = m_ui->indentDeclarationsRelativeToAccessSpecifiers->isChecked();
set.indentFunctionBody = m_ui->indentFunctionBody->isChecked();
set.indentFunctionBraces = m_ui->indentFunctionBraces->isChecked();
set.indentSwitchLabels = m_ui->indentSwitchLabels->isChecked();
set.indentStatementsRelativeToSwitchLabels = m_ui->indentCaseStatements->isChecked();
set.indentBlocksRelativeToSwitchLabels = m_ui->indentCaseBlocks->isChecked();
set.indentControlFlowRelativeToSwitchLabels = m_ui->indentCaseBreak->isChecked();
set.extraPaddingForConditionsIfConfusingAlign = m_ui->extraPaddingConditions->isChecked();
set.alignAssignments = m_ui->alignAssignments->isChecked();
return set;
}
void CppCodeStylePreferencesWidget::setCppCodeStyleSettings(const CppCodeStyleSettings &s)
{
const bool wasBlocked = blockSignals(true);
m_ui->indentBlockBraces->setChecked(s.indentBlockBraces);
m_ui->indentBlockBody->setChecked(s.indentBlockBody);
m_ui->indentClassBraces->setChecked(s.indentClassBraces);
m_ui->indentEnumBraces->setChecked(s.indentEnumBraces);
m_ui->indentNamespaceBraces->setChecked(s.indentNamespaceBraces);
m_ui->indentNamespaceBody->setChecked(s.indentNamespaceBody);
m_ui->indentAccessSpecifiers->setChecked(s.indentAccessSpecifiers);
m_ui->indentDeclarationsRelativeToAccessSpecifiers->setChecked(s.indentDeclarationsRelativeToAccessSpecifiers);
m_ui->indentFunctionBody->setChecked(s.indentFunctionBody);
m_ui->indentFunctionBraces->setChecked(s.indentFunctionBraces);
m_ui->indentSwitchLabels->setChecked(s.indentSwitchLabels);
m_ui->indentCaseStatements->setChecked(s.indentStatementsRelativeToSwitchLabels);
m_ui->indentCaseBlocks->setChecked(s.indentBlocksRelativeToSwitchLabels);
m_ui->indentCaseBreak->setChecked(s.indentControlFlowRelativeToSwitchLabels);
m_ui->extraPaddingConditions->setChecked(s.extraPaddingForConditionsIfConfusingAlign);
m_ui->alignAssignments->setChecked(s.alignAssignments);
blockSignals(wasBlocked);
updatePreview();
}
void CppCodeStylePreferencesWidget::slotCurrentFallbackChanged(TextEditor::IFallbackPreferences *fallback)
{
m_ui->tabPreferencesWidget->setEnabled(!fallback);
m_ui->contentGroupBox->setEnabled(!fallback);
m_ui->bracesGroupBox->setEnabled(!fallback);
m_ui->switchGroupBox->setEnabled(!fallback);
m_ui->alignmentGroupBox->setEnabled(!fallback);
// if C++ global is used for style, use it for tab settings as well
if (fallback && m_tabPreferences && m_cppCodeStylePreferences->currentFallback())
m_tabPreferences->setCurrentFallback(m_cppCodeStylePreferences->currentFallback()->id());
updatePreview();
}
QString CppCodeStylePreferencesWidget::searchKeywords() const
{
QString rc;
QLatin1Char sep(' ');
QTextStream(&rc)
<< sep << m_ui->tabPreferencesWidget->searchKeywords()
<< sep << m_ui->fallbackWidget->searchKeywords()
<< sep << m_ui->indentBlockBraces->text()
<< sep << m_ui->indentBlockBody->text()
<< sep << m_ui->indentClassBraces->text()
<< sep << m_ui->indentEnumBraces->text()
<< sep << m_ui->indentNamespaceBraces->text()
<< sep << m_ui->indentNamespaceBody->text()
<< sep << m_ui->indentAccessSpecifiers->text()
<< sep << m_ui->indentDeclarationsRelativeToAccessSpecifiers->text()
<< sep << m_ui->indentFunctionBody->text()
<< sep << m_ui->indentFunctionBraces->text()
<< sep << m_ui->indentSwitchLabels->text()
<< sep << m_ui->indentCaseStatements->text()
<< sep << m_ui->indentCaseBlocks->text()
<< sep << m_ui->indentCaseBreak->text()
<< sep << m_ui->contentGroupBox->title()
<< sep << m_ui->bracesGroupBox->title()
<< sep << m_ui->switchGroupBox->title()
<< sep << m_ui->alignmentGroupBox->title()
<< sep << m_ui->extraPaddingConditions->text()
<< sep << m_ui->alignAssignments->text()
;
for (int i = 0; i < m_ui->categoryTab->count(); i++)
QTextStream(&rc) << sep << m_ui->categoryTab->tabText(i);
rc.remove(QLatin1Char('&'));
return rc;
}
void CppCodeStylePreferencesWidget::slotCppCodeStyleSettingsChanged()
{
emit cppCodeStyleSettingsChanged(cppCodeStyleSettings());
updatePreview();
}
void CppCodeStylePreferencesWidget::slotSettingsChanged()
{
updatePreview();
}
void CppCodeStylePreferencesWidget::updatePreview()
{
foreach (TextEditor::SnippetEditorWidget *preview, m_previews) {
QTextDocument *doc = preview->document();
const TextEditor::TabSettings ts = m_tabPreferences
? m_tabPreferences->currentSettings()
: CppToolsSettings::instance()->tabPreferences()->settings();
CppCodeStylePreferences *cppCodeStylePreferences = m_cppCodeStylePreferences
? m_cppCodeStylePreferences
: CppToolsSettings::instance()->cppCodeStylePreferences();
const CppCodeStyleSettings ccss = cppCodeStylePreferences->currentSettings();
preview->setTabSettings(ts);
preview->setCodeStylePreferences(cppCodeStylePreferences);
QtStyleCodeFormatter formatter(ts, ccss);
formatter.invalidateCache(doc);
QTextBlock block = doc->firstBlock();
QTextCursor tc = preview->textCursor();
tc.beginEditBlock();
while (block.isValid()) {
int indent;
int padding;
formatter.indentFor(block, &indent, &padding);
ts.indentLine(block, indent + padding, padding);
formatter.updateLineStateChange(block);
block = block.next();
}
tc.endEditBlock();
}
}
void CppCodeStylePreferencesWidget::setFontSettings(const TextEditor::FontSettings &fontSettings)
{
foreach (TextEditor::SnippetEditorWidget *editor, m_previews)
editor->setFont(fontSettings.font());
}
void CppCodeStylePreferencesWidget::setVisualizeWhitespace(bool on)
{
foreach (TextEditor::SnippetEditorWidget *editor, m_previews) {
DisplaySettings displaySettings = editor->displaySettings();
displaySettings.m_visualizeWhitespace = on;
editor->setDisplaySettings(displaySettings);
}
}
// ------------------ CppCodeStyleSettingsPage
CppCodeStyleSettingsPage::CppCodeStyleSettingsPage(
QWidget *parent) :
Core::IOptionsPage(parent),
m_pageTabPreferences(0)
{
}
CppCodeStyleSettingsPage::~CppCodeStyleSettingsPage()
{
}
QString CppCodeStyleSettingsPage::id() const
{
return QLatin1String(Constants::CPP_CODE_STYLE_SETTINGS_ID);
}
QString CppCodeStyleSettingsPage::displayName() const
{
return QCoreApplication::translate("CppTools", Constants::CPP_CODE_STYLE_SETTINGS_NAME);
}
QString CppCodeStyleSettingsPage::category() const
{
return QLatin1String(Constants::CPP_SETTINGS_CATEGORY);
}
QString CppCodeStyleSettingsPage::displayCategory() const
{
return QCoreApplication::translate("CppTools", Constants::CPP_SETTINGS_TR_CATEGORY);
}
QIcon CppCodeStyleSettingsPage::categoryIcon() const
{
return QIcon(QLatin1String(Constants::SETTINGS_CATEGORY_CPP_ICON));
}
QWidget *CppCodeStyleSettingsPage::createPage(QWidget *parent)
{
m_widget = new CppCodeStylePreferencesWidget(parent);
TextEditor::TabPreferences *originalTabPreferences
= CppToolsSettings::instance()->tabPreferences();
m_pageTabPreferences = new TextEditor::TabPreferences(originalTabPreferences->fallbacks(), m_widget);
m_pageTabPreferences->setSettings(originalTabPreferences->settings());
m_pageTabPreferences->setCurrentFallback(originalTabPreferences->currentFallback());
m_widget->setTabPreferences(m_pageTabPreferences);
CppCodeStylePreferences *originalCodeStylePreferences
= CppToolsSettings::instance()->cppCodeStylePreferences();
m_pageCppCodeStylePreferences = new CppCodeStylePreferences(originalCodeStylePreferences->fallbacks(), m_widget);
m_pageCppCodeStylePreferences->setSettings(originalCodeStylePreferences->settings());
m_pageCppCodeStylePreferences->setCurrentFallback(originalCodeStylePreferences->currentFallback());
m_widget->setCppCodeStylePreferences(m_pageCppCodeStylePreferences);
if (m_searchKeywords.isEmpty())
m_searchKeywords = m_widget->searchKeywords();
return m_widget;
}
void CppCodeStyleSettingsPage::apply()
{
if (m_widget) {
QSettings *s = Core::ICore::instance()->settings();
TextEditor::TabPreferences *originalTabPreferences = CppToolsSettings::instance()->tabPreferences();
if (originalTabPreferences->settings() != m_pageTabPreferences->settings()) {
originalTabPreferences->setSettings(m_pageTabPreferences->settings());
if (s)
originalTabPreferences->toSettings(CppTools::Constants::CPP_SETTINGS_ID, s);
}
if (originalTabPreferences->currentFallback() != m_pageTabPreferences->currentFallback()) {
originalTabPreferences->setCurrentFallback(m_pageTabPreferences->currentFallback());
if (s)
originalTabPreferences->toSettings(CppTools::Constants::CPP_SETTINGS_ID, s);
}
CppCodeStylePreferences *originalCppCodeStylePreferences = CppToolsSettings::instance()->cppCodeStylePreferences();
if (originalCppCodeStylePreferences->settings() != m_pageCppCodeStylePreferences->settings()) {
originalCppCodeStylePreferences->setSettings(m_pageCppCodeStylePreferences->settings());
if (s)
originalCppCodeStylePreferences->toSettings(CppTools::Constants::CPP_SETTINGS_ID, s);
}
if (originalCppCodeStylePreferences->currentFallback() != m_pageCppCodeStylePreferences->currentFallback()) {
originalCppCodeStylePreferences->setCurrentFallback(m_pageCppCodeStylePreferences->currentFallback());
if (s)
originalCppCodeStylePreferences->toSettings(CppTools::Constants::CPP_SETTINGS_ID, s);
}
}
}
bool CppCodeStyleSettingsPage::matches(const QString &s) const
{
return m_searchKeywords.contains(s, Qt::CaseInsensitive);
}
} // namespace Internal
} // namespace CppTools

View File

@@ -0,0 +1,98 @@
#ifndef CPPCODESTYLESETTINGSPAGE_H
#define CPPCODESTYLESETTINGSPAGE_H
#include "cpptools_global.h"
#include <coreplugin/dialogs/ioptionspage.h>
#include <QtGui/QWidget>
#include <QtCore/QPointer>
#include <QtCore/QSharedPointer>
#include <QtCore/QVariant>
#include <QtCore/QStringList>
#include "cppcodestylesettings.h"
#include "cppcodeformatter.h"
QT_BEGIN_NAMESPACE
namespace Ui {
class CppCodeStyleSettingsPage;
}
QT_END_NAMESPACE
namespace TextEditor {
class FontSettings;
class TabSettings;
class TabPreferences;
class IFallbackPreferences;
class SnippetEditorWidget;
}
namespace CppTools {
class CppCodeStylePreferences;
namespace Internal {
class CppCodeStylePreferencesWidget : public QWidget
{
Q_OBJECT
public:
explicit CppCodeStylePreferencesWidget(QWidget *parent = 0);
virtual ~CppCodeStylePreferencesWidget();
void setTabPreferences(TextEditor::TabPreferences *tabPreferences);
void setCppCodeStylePreferences(CppTools::CppCodeStylePreferences *codeStylePreferences);
QString searchKeywords() const;
private slots:
void setFontSettings(const TextEditor::FontSettings &fontSettings);
void setVisualizeWhitespace(bool on);
void slotCppCodeStyleSettingsChanged();
void slotSettingsChanged();
void updatePreview();
void setCppCodeStyleSettings(const CppTools::CppCodeStyleSettings &settings);
void slotCurrentFallbackChanged(TextEditor::IFallbackPreferences *);
signals:
void cppCodeStyleSettingsChanged(const CppTools::CppCodeStyleSettings &);
private:
CppCodeStyleSettings cppCodeStyleSettings() const;
TextEditor::TabPreferences *m_tabPreferences;
CppCodeStylePreferences *m_cppCodeStylePreferences;
Ui::CppCodeStyleSettingsPage *m_ui;
QList<TextEditor::SnippetEditorWidget *> m_previews;
};
class CppCodeStyleSettingsPage : public Core::IOptionsPage
{
Q_OBJECT
public:
explicit CppCodeStyleSettingsPage(QWidget *parent = 0);
~CppCodeStyleSettingsPage();
virtual QString id() const;
virtual QString displayName() const;
virtual QString category() const;
virtual QString displayCategory() const;
virtual QIcon categoryIcon() const;
virtual QWidget *createPage(QWidget *parent);
virtual void apply();
virtual void finish() { }
virtual bool matches(const QString &) const;
private:
QString m_searchKeywords;
TextEditor::TabPreferences *m_pageTabPreferences;
CppCodeStylePreferences *m_pageCppCodeStylePreferences;
QPointer<CppCodeStylePreferencesWidget> m_widget;
};
} // namespace Internal
} // namespace CppTools
#endif // CPPCODESTYLESETTINGSPAGE_H

View File

@@ -0,0 +1,436 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CppCodeStyleSettingsPage</class>
<widget class="QWidget" name="CppCodeStyleSettingsPage">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>759</width>
<height>467</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QWidget" name="fallbackContainer" native="true">
<layout class="QHBoxLayout" name="fallbackLayout">
<property name="leftMargin">
<number>0</number>
</property>
<item>
<widget class="TextEditor::FallbackSelectorWidget" name="fallbackWidget" native="true"/>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QTabWidget" name="categoryTab">
<property name="currentIndex">
<number>4</number>
</property>
<widget class="QWidget" name="generalTab">
<attribute name="title">
<string>General</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="TextEditor::TabPreferencesWidget" name="tabPreferencesWidget" native="true">
<property name="focusPolicy">
<enum>Qt::TabFocus</enum>
</property>
</widget>
</item>
<item>
<widget class="TextEditor::SnippetEditorWidget" name="previewTextEditGeneral">
<property name="plainText">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="contentTab">
<attribute name="title">
<string>Content</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QGroupBox" name="contentGroupBox">
<property name="title">
<string>Indent</string>
</property>
<property name="flat">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="indentAccessSpecifiers">
<property name="text">
<string>&quot;public&quot;, &quot;protected&quot; and
&quot;private&quot; within class body</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="indentDeclarationsRelativeToAccessSpecifiers">
<property name="text">
<string>Declarations relative to &quot;public&quot;,
&quot;protected&quot; and &quot;private&quot;</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="indentFunctionBody">
<property name="text">
<string>Statements within method body</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="indentBlockBody">
<property name="text">
<string>Statements within blocks</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="indentNamespaceBody">
<property name="text">
<string>Declarations within
&quot;namespace&quot; definition</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>17</width>
<height>114</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="TextEditor::SnippetEditorWidget" name="previewTextEditContent">
<property name="plainText">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="bracesTab">
<attribute name="title">
<string>Braces</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QGroupBox" name="bracesGroupBox">
<property name="title">
<string>Indent Braces</string>
</property>
<property name="flat">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="indentClassBraces">
<property name="text">
<string>Class declarations</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="indentNamespaceBraces">
<property name="text">
<string>Namespace declarations</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="indentEnumBraces">
<property name="text">
<string>Enum declarations</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="indentFunctionBraces">
<property name="text">
<string>Method declarations</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="indentBlockBraces">
<property name="text">
<string>Blocks</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>195</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="TextEditor::SnippetEditorWidget" name="previewTextEditBraces">
<property name="plainText">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="switchTab">
<attribute name="title">
<string>&quot;switch&quot;</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QGroupBox" name="switchGroupBox">
<property name="title">
<string>Indent within &quot;switch&quot;</string>
</property>
<property name="flat">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<property name="leftMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="indentSwitchLabels">
<property name="text">
<string>&quot;case&quot; or &quot;default&quot;</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="indentCaseStatements">
<property name="text">
<string>Statements relative to
&quot;case&quot; or &quot;default&quot;</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="indentCaseBlocks">
<property name="text">
<string>Blocks relative to
&quot;case&quot; or &quot;default&quot;</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="indentCaseBreak">
<property name="text">
<string>&quot;break&quot; statement relative to
&quot;case&quot; or &quot;default&quot;</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>143</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="TextEditor::SnippetEditorWidget" name="previewTextEditSwitch">
<property name="plainText">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="alignmentTab">
<attribute name="title">
<string>Alignment</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QGroupBox" name="alignmentGroupBox">
<property name="title">
<string>Align</string>
</property>
<property name="flat">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
<property name="leftMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="alignAssignments">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;
Enables alignment to tokens after =, += etc. When the option is disabled regular continuation line indentation will be used.&lt;br&gt;
&lt;br&gt;
With alignment:
&lt;pre&gt;
a = a +
b
&lt;/pre&gt;
Without alignment:
&lt;pre&gt;
a = a +
b
&lt;/pre&gt;
&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Align after assignments</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="extraPaddingConditions">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;
The extra padding usually only affects if statement conditions. Without extra padding:
&lt;pre&gt;
if (a &amp;&amp;
b)
c;
&lt;/pre&gt;
With extra padding:
&lt;pre&gt;
if (a &amp;&amp;
b)
c;
&lt;/pre&gt;
&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Add extra padding to conditions
if they would align to the next line</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="TextEditor::SnippetEditorWidget" name="previewTextEditPadding">
<property name="plainText">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>TextEditor::SnippetEditorWidget</class>
<extends>QPlainTextEdit</extends>
<header location="global">texteditor/snippets/snippeteditor.h</header>
</customwidget>
<customwidget>
<class>TextEditor::TabPreferencesWidget</class>
<extends>QWidget</extends>
<header location="global">texteditor/tabpreferenceswidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>TextEditor::FallbackSelectorWidget</class>
<extends>QWidget</extends>
<header location="global">texteditor/fallbackselectorwidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@@ -333,12 +333,12 @@ CppFileSettingsPage::~CppFileSettingsPage()
QString CppFileSettingsPage::id() const
{
return QLatin1String(Constants::CPP_SETTINGS_ID);
return QLatin1String(Constants::CPP_FILE_SETTINGS_ID);
}
QString CppFileSettingsPage::displayName() const
{
return QCoreApplication::translate("CppTools", Constants::CPP_SETTINGS_NAME);
return QCoreApplication::translate("CppTools", Constants::CPP_FILE_SETTINGS_NAME);
}
QString CppFileSettingsPage::category() const

View File

@@ -0,0 +1,164 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#include "cppqtstyleindenter.h"
#include "cppcodeformatter.h"
#include "cpptoolssettings.h"
#include "cppcodestylepreferences.h"
#include "cpptoolsconstants.h"
#include <texteditor/basetexteditor.h>
#include <texteditor/tabsettings.h>
#include <texteditor/texteditorsettings.h>
#include <QtCore/QChar>
#include <QtGui/QTextDocument>
#include <QtGui/QTextBlock>
#include <QtGui/QTextCursor>
using namespace CppTools;
CppQtStyleIndenter::CppQtStyleIndenter()
: m_cppCodeStylePreferences(0)
{
m_cppCodeStylePreferences = CppToolsSettings::instance()->cppCodeStylePreferences();
}
CppQtStyleIndenter::~CppQtStyleIndenter()
{}
bool CppQtStyleIndenter::isElectricCharacter(const QChar &ch) const
{
if (ch == QLatin1Char('{') ||
ch == QLatin1Char('}') ||
ch == QLatin1Char(':') ||
ch == QLatin1Char('#')) {
return true;
}
return false;
}
static bool colonIsElectric(const QString &text)
{
// switch cases and access declarations should be reindented
if (text.contains(QLatin1String("case"))
|| text.contains(QLatin1String("default"))
|| text.contains(QLatin1String("public"))
|| text.contains(QLatin1String("private"))
|| text.contains(QLatin1String("protected"))
|| text.contains(QLatin1String("signals"))) {
return true;
}
// lines that start with : might have a constructor initializer list
const QString trimmedtext = text.trimmed();
if (!trimmedtext.isEmpty() && trimmedtext.at(0) == QLatin1Char(':'))
return true;
return false;
}
void CppQtStyleIndenter::indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor)
{
Q_UNUSED(doc)
const TextEditor::TabSettings &ts = editor->tabSettings();
CppTools::QtStyleCodeFormatter codeFormatter(ts, codeStyleSettings());
codeFormatter.updateStateUntil(block);
int indent;
int padding;
codeFormatter.indentFor(block, &indent, &padding);
if (isElectricCharacter(typedChar)) {
// : should not be electric for labels
if (typedChar == QLatin1Char(':') && !colonIsElectric(block.text()))
return;
// only reindent the current line when typing electric characters if the
// indent is the same it would be if the line were empty
int newlineIndent;
int newlinePadding;
codeFormatter.indentForNewLineAfter(block.previous(), &newlineIndent, &newlinePadding);
if (ts.indentationColumn(block.text()) != newlineIndent + newlinePadding)
return;
}
ts.indentLine(block, indent + padding, padding);
}
void CppQtStyleIndenter::indent(QTextDocument *doc,
const QTextCursor &cursor,
const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor)
{
if (cursor.hasSelection()) {
QTextBlock block = doc->findBlock(cursor.selectionStart());
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
const TextEditor::TabSettings &ts = editor->tabSettings();
CppTools::QtStyleCodeFormatter codeFormatter(ts, codeStyleSettings());
codeFormatter.updateStateUntil(block);
QTextCursor tc = editor->textCursor();
tc.beginEditBlock();
do {
int indent;
int padding;
codeFormatter.indentFor(block, &indent, &padding);
ts.indentLine(block, indent + padding, padding);
codeFormatter.updateLineStateChange(block);
block = block.next();
} while (block.isValid() && block != end);
tc.endEditBlock();
} else {
indentBlock(doc, cursor.block(), typedChar, editor);
}
}
void CppQtStyleIndenter::setCodeStylePreferences(TextEditor::IFallbackPreferences *preferences)
{
CppTools::CppCodeStylePreferences *cppCodeStylePreferences
= qobject_cast<CppTools::CppCodeStylePreferences *>(preferences);
if (cppCodeStylePreferences)
m_cppCodeStylePreferences = cppCodeStylePreferences;
}
CppCodeStyleSettings CppQtStyleIndenter::codeStyleSettings() const
{
if (m_cppCodeStylePreferences)
return m_cppCodeStylePreferences->currentSettings();
return CppCodeStyleSettings();
}

View File

@@ -0,0 +1,72 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#ifndef CPPQTSTYLEINDENTER_H
#define CPPQTSTYLEINDENTER_H
#include "cpptools_global.h"
#include <texteditor/indenter.h>
#include "cppcodestylesettingspage.h"
namespace TextEditor
{
class IFallbackPreferences;
}
namespace CppTools {
class CPPTOOLS_EXPORT CppQtStyleIndenter : public TextEditor::Indenter
{
public:
CppQtStyleIndenter();
virtual ~CppQtStyleIndenter();
virtual bool isElectricCharacter(const QChar &ch) const;
virtual void indentBlock(QTextDocument *doc,
const QTextBlock &block,
const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor);
virtual void indent(QTextDocument *doc,
const QTextCursor &cursor,
const QChar &typedChar,
TextEditor::BaseTextEditorWidget *editor);
virtual void setCodeStylePreferences(TextEditor::IFallbackPreferences *preferences);
private:
CppCodeStyleSettings codeStyleSettings() const;
CppCodeStylePreferences *m_cppCodeStylePreferences;
};
} // CppTools
#endif // CPPQTSTYLEINDENTER_H

View File

@@ -31,11 +31,14 @@
**************************************************************************/
#include "cpprefactoringchanges.h"
#include "cppcodestylepreferences.h"
#include <TranslationUnit.h>
#include <AST.h>
#include <cpptools/cppcodeformatter.h>
#include <cpptools/cppmodelmanager.h>
#include <cpptools/cpptoolssettings.h>
#include <cpptools/cpptoolsconstants.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/tabsettings.h>
#include <projectexplorer/editorconfiguration.h>
@@ -76,7 +79,9 @@ void CppRefactoringChanges::indentSelection(const QTextCursor &selection,
const TextEditor::TabSettings &tabSettings =
ProjectExplorer::actualTabSettings(fileName, textEditor);
CppTools::QtStyleCodeFormatter codeFormatter(tabSettings);
// TODO: add similar method like above one
CppTools::QtStyleCodeFormatter codeFormatter(tabSettings,
CppToolsSettings::instance()->cppCodeStylePreferences()->settings());
codeFormatter.updateStateUntil(block);
do {

View File

@@ -18,6 +18,7 @@ HEADERS += completionsettingspage.h \
cpptoolsconstants.h \
cpptoolseditorsupport.h \
cpptoolsplugin.h \
cppqtstyleindenter.h \
searchsymbols.h \
cppdoxygen.h \
cppfilesettingspage.h \
@@ -28,7 +29,12 @@ HEADERS += completionsettingspage.h \
insertionpointlocator.h \
cpprefactoringchanges.h \
abstracteditorsupport.h \
cppcompletionassist.h
cppcompletionassist.h \
cppcodestylesettingspage.h \
cpptoolssettings.h \
cppcodestylesettings.h \
cppcodestylesettingsfactory.h \
cppcodestylepreferences.h
SOURCES += completionsettingspage.cpp \
cppclassesfilter.cpp \
@@ -38,6 +44,7 @@ SOURCES += completionsettingspage.cpp \
cpplocatorfilter.cpp \
cpptoolseditorsupport.cpp \
cpptoolsplugin.cpp \
cppqtstyleindenter.cpp \
searchsymbols.cpp \
cppdoxygen.cpp \
cppfilesettingspage.cpp \
@@ -48,7 +55,13 @@ SOURCES += completionsettingspage.cpp \
uicodecompletionsupport.cpp \
insertionpointlocator.cpp \
cpprefactoringchanges.cpp \
cppcompletionassist.cpp
cppcompletionassist.cpp \
cppcodestylesettingspage.cpp \
cpptoolssettings.cpp \
cppcodestylesettings.cpp \
cppcodestylesettingsfactory.cpp \
cppcodestylepreferences.cpp
FORMS += completionsettingspage.ui \
cppfilesettingspage.ui
cppfilesettingspage.ui \
cppcodestylesettingspage.ui

View File

@@ -53,12 +53,17 @@ const char * const CPPTOOLS_SETTINGSGROUP = "CppTools";
const char * const LOWERCASE_CPPFILES_KEY = "LowerCaseFiles";
enum { lowerCaseFilesDefault = 1 };
const char * const CPP_SETTINGS_ID = "File Naming";
const char * const CPP_SETTINGS_NAME = QT_TRANSLATE_NOOP("CppTools", "File Naming");
const char * const CPP_CODE_STYLE_SETTINGS_ID = "A.Code Style";
const char * const CPP_CODE_STYLE_SETTINGS_NAME = QT_TRANSLATE_NOOP("CppTools", "Code Style");
const char * const CPP_FILE_SETTINGS_ID = "B.File Naming";
const char * const CPP_FILE_SETTINGS_NAME = QT_TRANSLATE_NOOP("CppTools", "File Naming");
const char * const CPP_SETTINGS_CATEGORY = "I.C++";
const char * const CPP_SETTINGS_TR_CATEGORY = QT_TRANSLATE_NOOP("CppTools", "C++");
const char * const SETTINGS_CATEGORY_CPP_ICON = ":/core/images/category_cpp.png";
const char * const CPP_SETTINGS_ID = "Cpp";
const char * const CPP_SETTINGS_NAME = QT_TRANSLATE_NOOP("CppTools", "C++");
} // namespace Constants
} // namespace CppTools

View File

@@ -33,6 +33,7 @@
#include "cpptoolsplugin.h"
#include "completionsettingspage.h"
#include "cppfilesettingspage.h"
#include "cppcodestylesettingspage.h"
#include "cppclassesfilter.h"
#include "cppfunctionsfilter.h"
#include "cppcurrentdocumentfilter.h"
@@ -41,6 +42,8 @@
#include "cpplocatorfilter.h"
#include "symbolsfindfilter.h"
#include "cppcompletionassist.h"
#include "cpptoolssettings.h"
#include "cppcodestylesettingsfactory.h"
#include <extensionsystem/pluginmanager.h>
@@ -56,6 +59,8 @@
#include <coreplugin/vcsmanager.h>
#include <coreplugin/filemanager.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/tabsettings.h>
#include <texteditor/codestylepreferencesmanager.h>
#include <cppeditor/cppeditorconstants.h>
#include <QtCore/QtConcurrentRun>
@@ -103,6 +108,8 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error)
Core::ICore *core = Core::ICore::instance();
Core::ActionManager *am = core->actionManager();
m_settings = new CppToolsSettings(this); // force registration of cpp tools settings
// Objects
m_modelManager = new CppModelManager(this);
Core::VcsManager *vcsManager = core->vcsManager();
@@ -121,6 +128,10 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error)
addAutoReleasedObject(new CompletionSettingsPage);
addAutoReleasedObject(new CppFileSettingsPage(m_fileSettings));
addAutoReleasedObject(new SymbolsFindFilter(m_modelManager));
addAutoReleasedObject(new CppCodeStyleSettingsPage);
TextEditor::CodeStylePreferencesManager::instance()->registerFactory(
new CppTools::CppCodeStylePreferencesFactory());
// Menus
Core::ActionContainer *mtools = am->actionContainer(Core::Constants::M_TOOLS);

View File

@@ -51,6 +51,9 @@ class QDir;
QT_END_NAMESPACE
namespace CppTools {
class CppToolsSettings;
namespace Internal {
class CppModelManager;
@@ -81,6 +84,7 @@ private:
CppModelManager *m_modelManager;
QSharedPointer<CppFileSettings> m_fileSettings;
CppToolsSettings *m_settings;
static CppToolsPlugin *m_instance;
};

View File

@@ -0,0 +1,80 @@
#include "cpptoolssettings.h"
#include "cpptoolsconstants.h"
#include "cppcodestylepreferences.h"
#include <texteditor/texteditorsettings.h>
#include <texteditor/tabpreferences.h>
#include <utils/qtcassert.h>
#include <coreplugin/icore.h>
#include <QtCore/QSettings>
static const char *idKey = "CppGlobal";
using namespace CppTools;
namespace CppTools {
namespace Internal {
class CppToolsSettingsPrivate
{
public:
CppCodeStylePreferences *m_cppCodeStylePreferences;
TextEditor::TabPreferences *m_tabPreferences;
};
} // namespace Internal
} // namespace CppTools
CppToolsSettings *CppToolsSettings::m_instance = 0;
CppToolsSettings::CppToolsSettings(QObject *parent)
: QObject(parent)
, m_d(new Internal::CppToolsSettingsPrivate)
{
QTC_ASSERT(!m_instance, return);
m_instance = this;
if (const QSettings *s = Core::ICore::instance()->settings()) {
TextEditor::TextEditorSettings *textEditorSettings = TextEditor::TextEditorSettings::instance();
m_d->m_tabPreferences
= new TextEditor::TabPreferences(QList<TextEditor::IFallbackPreferences *>()
<< textEditorSettings->tabPreferences(), this);
m_d->m_tabPreferences->setCurrentFallback(textEditorSettings->tabPreferences());
m_d->m_tabPreferences->fromSettings(CppTools::Constants::CPP_SETTINGS_ID, s);
m_d->m_tabPreferences->setDisplayName(tr("global C++"));
m_d->m_tabPreferences->setId(idKey);
textEditorSettings->registerLanguageTabPreferences(CppTools::Constants::CPP_SETTINGS_ID, m_d->m_tabPreferences);
m_d->m_cppCodeStylePreferences
= new CppCodeStylePreferences(QList<TextEditor::IFallbackPreferences *>(), this);
m_d->m_cppCodeStylePreferences->fromSettings(CppTools::Constants::CPP_SETTINGS_ID, s);
m_d->m_cppCodeStylePreferences->setDisplayName(tr("global C++"));
m_d->m_cppCodeStylePreferences->setId(idKey);
textEditorSettings->registerLanguageCodeStylePreferences(CppTools::Constants::CPP_SETTINGS_ID, m_d->m_cppCodeStylePreferences);
}
}
CppToolsSettings::~CppToolsSettings()
{
delete m_d;
m_instance = 0;
}
CppToolsSettings *CppToolsSettings::instance()
{
return m_instance;
}
CppCodeStylePreferences *CppToolsSettings::cppCodeStylePreferences() const
{
return m_d->m_cppCodeStylePreferences;
}
TextEditor::TabPreferences *CppToolsSettings::tabPreferences() const
{
return m_d->m_tabPreferences;
}

View File

@@ -0,0 +1,80 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** No Commercial Usage
**
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#ifndef CPPTOOLSSETTINGS_H
#define CPPTOOLSSETTINGS_H
#include "cpptools_global.h"
#include <QtCore/QObject>
namespace TextEditor
{
class TabPreferences;
}
namespace CppTools
{
class CppCodeStylePreferences;
namespace Internal
{
class CppToolsSettingsPrivate;
}
/**
* This class provides a central place for cpp tools settings.
*/
class CPPTOOLS_EXPORT CppToolsSettings : public QObject
{
Q_OBJECT
public:
explicit CppToolsSettings(QObject *parent);
~CppToolsSettings();
static CppToolsSettings *instance();
CppCodeStylePreferences *cppCodeStylePreferences() const;
TextEditor::TabPreferences *tabPreferences() const;
private:
Internal::CppToolsSettingsPrivate *m_d;
static CppToolsSettings *m_instance;
};
} // namespace CppTools
#endif // CPPTOOLSSETTINGS_H