Commit 27578a6c authored by Christian Kamm's avatar Christian Kamm

C++: Use the new indenter.

Reviewed-by: Roberto Raggi
parent e4886468
...@@ -58,6 +58,7 @@ ...@@ -58,6 +58,7 @@
#include <cpptools/cppmodelmanagerinterface.h> #include <cpptools/cppmodelmanagerinterface.h>
#include <cpptools/cpptoolsconstants.h> #include <cpptools/cpptoolsconstants.h>
#include <cpptools/cppcodeformatter.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/actionmanager.h>
...@@ -1427,90 +1428,35 @@ bool CPPEditor::isInComment(const QTextCursor &cursor) const ...@@ -1427,90 +1428,35 @@ bool CPPEditor::isInComment(const QTextCursor &cursor) const
return false; 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) void CPPEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar)
{ {
QTextCursor tc(block); Q_UNUSED(doc)
tc.movePosition(QTextCursor::EndOfBlock); Q_UNUSED(typedChar)
const TabSettings &ts = tabSettings(); const TabSettings &ts = tabSettings();
CppTools::QtStyleCodeFormatter codeFormatter;
BackwardsScanner tk(tc, 400);
const int tokenCount = tk.startToken(); codeFormatter.setIndentSize(ts.m_indentSize);
codeFormatter.setTabSize(ts.m_tabSize);
if (tokenCount != 0) { if (ts.m_indentBraces && ts.m_doubleIndentBlocks) { // gnu style
const Token firstToken = tk[0]; codeFormatter.setIndentSubstatementBraces(true);
codeFormatter.setIndentSubstatementStatements(true);
if (firstToken.is(T_COLON)) { codeFormatter.setIndentDeclarationBraces(false);
const int previousLineIndent = indentationColumn(ts, tk, -1); codeFormatter.setIndentDeclarationMembers(true);
ts.indentLine(block, previousLineIndent + ts.m_indentSize); } else if (ts.m_indentBraces) { // whitesmiths style
return; codeFormatter.setIndentSubstatementBraces(true);
} else if ((firstToken.is(T_PUBLIC) || firstToken.is(T_PROTECTED) || firstToken.is(T_PRIVATE) || codeFormatter.setIndentSubstatementStatements(false);
firstToken.is(T_Q_SIGNALS) || firstToken.is(T_Q_SLOTS)) && codeFormatter.setIndentDeclarationBraces(true);
tk.size() > 1 && tk[1].is(T_COLON)) { codeFormatter.setIndentDeclarationMembers(false);
const int startOfBlock = tk.startOfBlock(0); } else { // default Qt style
if (startOfBlock != 0) { codeFormatter.setIndentSubstatementBraces(false);
const int indent = indentationColumn(ts, tk, startOfBlock); codeFormatter.setIndentSubstatementStatements(true);
ts.indentLine(block, indent); codeFormatter.setIndentDeclarationBraces(false);
return; codeFormatter.setIndentDeclarationMembers(true);
} }
} else if (firstToken.is(T_CASE) || firstToken.is(T_DEFAULT)) {
const int startOfBlock = tk.startOfBlock(0); const int depth = codeFormatter.indentFor(block);
if (startOfBlock != 0) { ts.indentLine(block, depth);
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);
} }
bool CPPEditor::event(QEvent *e) bool CPPEditor::event(QEvent *e)
...@@ -1726,6 +1672,13 @@ void CPPEditor::setFontSettings(const TextEditor::FontSettings &fs) ...@@ -1726,6 +1672,13 @@ void CPPEditor::setFontSettings(const TextEditor::FontSettings &fs)
m_occurrenceRenameFormat.clearForeground(); m_occurrenceRenameFormat.clearForeground();
} }
void CPPEditor::setTabSettings(const TextEditor::TabSettings &ts)
{
CppTools::CodeFormatter::invalidateCache(document());
TextEditor::BaseTextEditor::setTabSettings(ts);
}
void CPPEditor::unCommentSelection() void CPPEditor::unCommentSelection()
{ {
Utils::unCommentSelection(this); Utils::unCommentSelection(this);
......
...@@ -208,6 +208,7 @@ public: ...@@ -208,6 +208,7 @@ public:
public Q_SLOTS: public Q_SLOTS:
virtual void setFontSettings(const TextEditor::FontSettings &); virtual void setFontSettings(const TextEditor::FontSettings &);
virtual void setTabSettings(const TextEditor::TabSettings &);
void setSortedMethodOverview(bool sort); void setSortedMethodOverview(bool sort);
void switchDeclarationDefinition(); void switchDeclarationDefinition();
void jumpToDefinition(); void jumpToDefinition();
......
...@@ -29,10 +29,11 @@ public: ...@@ -29,10 +29,11 @@ public:
virtual ~CodeFormatter(); virtual ~CodeFormatter();
int indentFor(const QTextBlock &block); int indentFor(const QTextBlock &block);
void invalidateCache(QTextDocument *document);
void setTabSize(int tabSize); void setTabSize(int tabSize);
static void invalidateCache(QTextDocument *document);
protected: protected:
virtual void onEnter(int newState, int *indentDepth, int *savedIndentDepth) const = 0; 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; virtual void adjustIndent(const QList<CPlusPlus::Token> &tokens, int lexerState, int *indentDepth) const = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment