forked from qt-creator/qt-creator
CppTools: Do not indent within string literals
Multi-line strings must be kept as the user wrote them. Fixes: QTCREATORBUG-20180 Change-Id: I141eff52b55d31215e6f5c6c5a0e026689db877a Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -635,7 +635,6 @@ void CodeFormatter::updateStateUntil(const QTextBlock &endBlock)
|
|||||||
break;
|
break;
|
||||||
if (loadLexerState(it) == -1)
|
if (loadLexerState(it) == -1)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
previousState = blockData.m_endState;
|
previousState = blockData.m_endState;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -673,6 +672,16 @@ void CodeFormatter::updateLineStateChange(const QTextBlock &block)
|
|||||||
saveBlockData(&next, BlockData());
|
saveBlockData(&next, BlockData());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CodeFormatter::isInStringLiteral(const QTextBlock &block) const
|
||||||
|
{
|
||||||
|
if (!block.previous().isValid())
|
||||||
|
return false;
|
||||||
|
BlockData blockData;
|
||||||
|
if (!loadBlockData(block.previous(), &blockData))
|
||||||
|
return false;
|
||||||
|
return !blockData.m_endState.isEmpty() && blockData.m_endState.top().type == string_open;
|
||||||
|
}
|
||||||
|
|
||||||
CodeFormatter::State CodeFormatter::state(int belowTop) const
|
CodeFormatter::State CodeFormatter::state(int belowTop) const
|
||||||
{
|
{
|
||||||
if (belowTop < m_currentState.size())
|
if (belowTop < m_currentState.size())
|
||||||
|
@@ -57,6 +57,7 @@ public:
|
|||||||
// calculates the state change introduced by changing a single line
|
// calculates the state change introduced by changing a single line
|
||||||
void updateLineStateChange(const QTextBlock &block);
|
void updateLineStateChange(const QTextBlock &block);
|
||||||
|
|
||||||
|
bool isInStringLiteral(const QTextBlock &block) const;
|
||||||
void indentFor(const QTextBlock &block, int *indent, int *padding);
|
void indentFor(const QTextBlock &block, int *indent, int *padding);
|
||||||
void indentForNewLineAfter(const QTextBlock &block, int *indent, int *padding);
|
void indentForNewLineAfter(const QTextBlock &block, int *indent, int *padding);
|
||||||
|
|
||||||
|
@@ -99,6 +99,8 @@ void CppQtStyleIndenter::indentBlock(const QTextBlock &block,
|
|||||||
QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());
|
QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());
|
||||||
|
|
||||||
codeFormatter.updateStateUntil(block);
|
codeFormatter.updateStateUntil(block);
|
||||||
|
if (codeFormatter.isInStringLiteral(block))
|
||||||
|
return;
|
||||||
int indent;
|
int indent;
|
||||||
int padding;
|
int padding;
|
||||||
codeFormatter.indentFor(block, &indent, &padding);
|
codeFormatter.indentFor(block, &indent, &padding);
|
||||||
@@ -135,10 +137,12 @@ void CppQtStyleIndenter::indent(const QTextCursor &cursor,
|
|||||||
QTextCursor tc = cursor;
|
QTextCursor tc = cursor;
|
||||||
tc.beginEditBlock();
|
tc.beginEditBlock();
|
||||||
do {
|
do {
|
||||||
|
if (!codeFormatter.isInStringLiteral(block)) {
|
||||||
int indent;
|
int indent;
|
||||||
int padding;
|
int padding;
|
||||||
codeFormatter.indentFor(block, &indent, &padding);
|
codeFormatter.indentFor(block, &indent, &padding);
|
||||||
tabSettings.indentLine(block, indent + padding, padding);
|
tabSettings.indentLine(block, indent + padding, padding);
|
||||||
|
}
|
||||||
codeFormatter.updateLineStateChange(block);
|
codeFormatter.updateLineStateChange(block);
|
||||||
block = block.next();
|
block = block.next();
|
||||||
} while (block.isValid() && block != end);
|
} while (block.isValid() && block != end);
|
||||||
|
Reference in New Issue
Block a user