summaryrefslogtreecommitdiff
path: root/src/plugins/cpptools/cppcodeformatter.cpp
diff options
context:
space:
mode:
authorChristian Kamm <christian.d.kamm@nokia.com>2010-07-05 13:39:23 +0200
committerChristian Kamm <christian.d.kamm@nokia.com>2010-07-05 13:49:12 +0200
commite48864684e2b8462508c1c2518d2bd6d1123fc13 (patch)
treed53a476b5a58b3c3624abddee2bc35d6eb3ed90a /src/plugins/cpptools/cppcodeformatter.cpp
parent19db6c98267d6afd4e27d727d5582541947ec43b (diff)
downloadqt-creator-e48864684e2b8462508c1c2518d2bd6d1123fc13.tar.gz
C++ indenter: Store tab size and use it to calculate the column position
Diffstat (limited to 'src/plugins/cpptools/cppcodeformatter.cpp')
-rw-r--r--src/plugins/cpptools/cppcodeformatter.cpp26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/plugins/cpptools/cppcodeformatter.cpp b/src/plugins/cpptools/cppcodeformatter.cpp
index 7028157c1b..4eb3a3ac84 100644
--- a/src/plugins/cpptools/cppcodeformatter.cpp
+++ b/src/plugins/cpptools/cppcodeformatter.cpp
@@ -39,6 +39,7 @@ using namespace CppTools::Internal;
CodeFormatter::CodeFormatter()
: m_indentDepth(0)
+ , m_tabSize(4)
{
}
@@ -46,6 +47,11 @@ CodeFormatter::~CodeFormatter()
{
}
+void CodeFormatter::setTabSize(int tabSize)
+{
+ m_tabSize = tabSize;
+}
+
void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
{
restoreBlockState(block.previous());
@@ -723,6 +729,24 @@ const Token &CodeFormatter::tokenAt(int idx) const
return m_tokens.at(idx);
}
+int CodeFormatter::column(int index) const
+{
+ int col = 0;
+ if (index > m_currentLine.length())
+ index = m_currentLine.length();
+
+ const QChar tab = QLatin1Char('\t');
+
+ for (int i = 0; i < index; i++) {
+ if (m_currentLine[i] == tab) {
+ col = ((col / m_tabSize) + 1) * m_tabSize;
+ } else {
+ col++;
+ }
+ }
+ return col;
+}
+
QStringRef CodeFormatter::currentTokenText() const
{
return m_currentLine.midRef(m_currentToken.begin(), m_currentToken.length());
@@ -843,7 +867,7 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
{
const State &parentState = state();
const Token &tk = currentToken();
- const int tokenPosition = tk.begin();
+ const int tokenPosition = column(tk.begin());
const bool firstToken = (tokenIndex() == 0);
const bool lastToken = (tokenIndex() == tokenCount() - 1);