diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp index c52f7a66283e5ea59997f4f6c6ab77a3f0dcea9b..fc7b9f3a668a91b5e7b136f8a0c9d21cf1794718 100644 --- a/src/plugins/cppeditor/cppeditor.cpp +++ b/src/plugins/cppeditor/cppeditor.cpp @@ -58,6 +58,7 @@ #include <cpptools/cppmodelmanagerinterface.h> #include <cpptools/cpptoolsconstants.h> +#include <cpptools/cppcodeformatter.h> #include <coreplugin/icore.h> #include <coreplugin/actionmanager/actionmanager.h> @@ -1427,90 +1428,35 @@ bool CPPEditor::isInComment(const QTextCursor &cursor) const return false; } -// Indent a code line based on previous -static void indentCPPBlock(const CPPEditor::TabSettings &ts, - const QTextBlock &block, - const TextEditor::TextBlockIterator &programBegin, - const TextEditor::TextBlockIterator &programEnd, - QChar typedChar) -{ - typedef SharedTools::Indenter Indenter; - Indenter &indenter = Indenter::instance(); - indenter.setIndentSize(ts.m_indentSize); - indenter.setTabSize(ts.m_tabSize); - indenter.setIndentBraces(ts.m_indentBraces); - indenter.setDoubleIndentBlocks(ts.m_doubleIndentBlocks); - - const TextEditor::TextBlockIterator current(block); - const int indent = indenter.indentForBottomLine(current, programBegin, programEnd, typedChar); - ts.indentLine(block, indent); -} - -static int indentationColumn(const TextEditor::TabSettings &tabSettings, - const BackwardsScanner &scanner, - int index) -{ - return tabSettings.indentationColumn(scanner.indentationString(index)); -} - void CPPEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar) { - QTextCursor tc(block); - tc.movePosition(QTextCursor::EndOfBlock); + Q_UNUSED(doc) + Q_UNUSED(typedChar) const TabSettings &ts = tabSettings(); - - BackwardsScanner tk(tc, 400); - const int tokenCount = tk.startToken(); - - if (tokenCount != 0) { - const Token firstToken = tk[0]; - - if (firstToken.is(T_COLON)) { - const int previousLineIndent = indentationColumn(ts, tk, -1); - ts.indentLine(block, previousLineIndent + ts.m_indentSize); - return; - } else if ((firstToken.is(T_PUBLIC) || firstToken.is(T_PROTECTED) || firstToken.is(T_PRIVATE) || - firstToken.is(T_Q_SIGNALS) || firstToken.is(T_Q_SLOTS)) && - tk.size() > 1 && tk[1].is(T_COLON)) { - const int startOfBlock = tk.startOfBlock(0); - if (startOfBlock != 0) { - const int indent = indentationColumn(ts, tk, startOfBlock); - ts.indentLine(block, indent); - return; - } - } else if (firstToken.is(T_CASE) || firstToken.is(T_DEFAULT)) { - const int startOfBlock = tk.startOfBlock(0); - if (startOfBlock != 0) { - const int indent = indentationColumn(ts, tk, startOfBlock); - ts.indentLine(block, indent); - return; - } - return; - } - } - - if ((tokenCount == 0 || tk[0].isNot(T_POUND)) && typedChar.isNull() && (tk[-1].is(T_IDENTIFIER) || tk[-1].is(T_RPAREN))) { - int tokenIndex = -1; - if (tk[-1].is(T_RPAREN)) { - const int matchingBrace = tk.startOfMatchingBrace(0); - if (matchingBrace != 0 && tk[matchingBrace - 1].is(T_IDENTIFIER)) { - tokenIndex = matchingBrace - 1; - } - } - - const QString spell = tk.text(tokenIndex); - if (tk[tokenIndex].newline() && (spell.startsWith(QLatin1String("QT_")) || - spell.startsWith(QLatin1String("Q_")))) { - const int indent = indentationColumn(ts, tk, tokenIndex); - ts.indentLine(block, indent); - return; - } - } - - const TextEditor::TextBlockIterator begin(doc->begin()); - const TextEditor::TextBlockIterator end(block.next()); - indentCPPBlock(ts, block, begin, end, typedChar); + 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); + } + + const int depth = codeFormatter.indentFor(block); + ts.indentLine(block, depth); } bool CPPEditor::event(QEvent *e) @@ -1726,6 +1672,13 @@ void CPPEditor::setFontSettings(const TextEditor::FontSettings &fs) m_occurrenceRenameFormat.clearForeground(); } +void CPPEditor::setTabSettings(const TextEditor::TabSettings &ts) +{ + CppTools::CodeFormatter::invalidateCache(document()); + + TextEditor::BaseTextEditor::setTabSettings(ts); +} + void CPPEditor::unCommentSelection() { Utils::unCommentSelection(this); diff --git a/src/plugins/cppeditor/cppeditor.h b/src/plugins/cppeditor/cppeditor.h index f623f78040cb244624945d4eac1ec8f2065ae5f3..48747bbcfc53ba9d37bc65b4af7dfb781a6c46ae 100644 --- a/src/plugins/cppeditor/cppeditor.h +++ b/src/plugins/cppeditor/cppeditor.h @@ -208,6 +208,7 @@ public: public Q_SLOTS: virtual void setFontSettings(const TextEditor::FontSettings &); + virtual void setTabSettings(const TextEditor::TabSettings &); void setSortedMethodOverview(bool sort); void switchDeclarationDefinition(); void jumpToDefinition(); diff --git a/src/plugins/cpptools/cppcodeformatter.h b/src/plugins/cpptools/cppcodeformatter.h index ce43461a773fe52cb550a3306ecb646feb8d404d..16375892af972caed66e679e0740903a7d4877ea 100644 --- a/src/plugins/cpptools/cppcodeformatter.h +++ b/src/plugins/cpptools/cppcodeformatter.h @@ -29,10 +29,11 @@ public: virtual ~CodeFormatter(); int indentFor(const QTextBlock &block); - void invalidateCache(QTextDocument *document); void setTabSize(int tabSize); + static void invalidateCache(QTextDocument *document); + protected: virtual void onEnter(int newState, int *indentDepth, int *savedIndentDepth) const = 0; virtual void adjustIndent(const QList<CPlusPlus::Token> &tokens, int lexerState, int *indentDepth) const = 0;