CppTools: Fix indentation of plain strings

Raw strings should not be indented, but old-style strings still should.

Amends commit ddf7f5f232.

Fixes: QTCREATORBUG-25817
Change-Id: I7836388efb2d19b8b898c7463c7fa2d2077e80b2
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Orgad Shaneh
2021-08-18 14:57:19 +03:00
committed by Orgad Shaneh
parent 57243d4dc6
commit c6ac773b9a
3 changed files with 11 additions and 8 deletions

View File

@@ -559,6 +559,7 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
break; break;
case string_open: case string_open:
case raw_string_open:
if (!m_currentToken.isStringLiteral()) { if (!m_currentToken.isStringLiteral()) {
leave(); leave();
continue; continue;
@@ -672,14 +673,14 @@ void CodeFormatter::updateLineStateChange(const QTextBlock &block)
saveBlockData(&next, BlockData()); saveBlockData(&next, BlockData());
} }
bool CodeFormatter::isInStringLiteral(const QTextBlock &block) const bool CodeFormatter::isInRawStringLiteral(const QTextBlock &block) const
{ {
if (!block.previous().isValid()) if (!block.previous().isValid())
return false; return false;
BlockData blockData; BlockData blockData;
if (!loadBlockData(block.previous(), &blockData)) if (!loadBlockData(block.previous(), &blockData))
return false; return false;
return !blockData.m_endState.isEmpty() && blockData.m_endState.top().type == string_open; return !blockData.m_endState.isEmpty() && blockData.m_endState.top().type == raw_string_open;
} }
CodeFormatter::State CodeFormatter::state(int belowTop) const CodeFormatter::State CodeFormatter::state(int belowTop) const
@@ -825,7 +826,7 @@ bool CodeFormatter::tryExpression(bool alsoExpression)
} }
if (m_currentToken.isStringLiteral()) if (m_currentToken.isStringLiteral())
newState = string_open; newState = m_currentToken.kind() == T_RAW_STRING_LITERAL ? raw_string_open : string_open;
if (newState != -1) { if (newState != -1) {
if (alsoExpression) if (alsoExpression)
@@ -1439,6 +1440,7 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
break; break;
case string_open: case string_open:
case raw_string_open:
*paddingDepth = tokenPosition - *indentDepth; *paddingDepth = tokenPosition - *indentDepth;
break; break;
} }
@@ -1481,6 +1483,7 @@ void QtStyleCodeFormatter::adjustIndent(const Tokens &tokens, int lexerState, in
} }
break; break;
case string_open: case string_open:
case raw_string_open:
if (!tokenAt(0).isStringLiteral()) { if (!tokenAt(0).isStringLiteral()) {
*paddingDepth = topState.savedPaddingDepth; *paddingDepth = topState.savedPaddingDepth;
topState = previousState; topState = previousState;

View File

@@ -57,7 +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; bool isInRawStringLiteral(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);
@@ -173,8 +173,8 @@ public: // must be public to make Q_GADGET introspection work
lambda_instroducer, // when '=', '&' or ',' occurred within '[]' lambda_instroducer, // when '=', '&' or ',' occurred within '[]'
lambda_declarator, // just after ']' when previous state is lambda_introducer lambda_declarator, // just after ']' when previous state is lambda_introducer
lambda_statement, // just after '{' when previous state is lambda_declarator or lambda_declarator_or_expression lambda_statement, // just after '{' when previous state is lambda_declarator or lambda_declarator_or_expression
string_open string_open, // after opening quote of simple string types, like ", L", u8" etc.
raw_string_open // after raw string open delimiter, like R"EOF(
}; };
Q_ENUM(StateType) Q_ENUM(StateType)

View File

@@ -99,7 +99,7 @@ void CppQtStyleIndenter::indentBlock(const QTextBlock &block,
QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings()); QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());
codeFormatter.updateStateUntil(block); codeFormatter.updateStateUntil(block);
if (codeFormatter.isInStringLiteral(block)) if (codeFormatter.isInRawStringLiteral(block))
return; return;
int indent; int indent;
int padding; int padding;
@@ -137,7 +137,7 @@ void CppQtStyleIndenter::indent(const QTextCursor &cursor,
QTextCursor tc = cursor; QTextCursor tc = cursor;
tc.beginEditBlock(); tc.beginEditBlock();
do { do {
if (!codeFormatter.isInStringLiteral(block)) { if (!codeFormatter.isInRawStringLiteral(block)) {
int indent; int indent;
int padding; int padding;
codeFormatter.indentFor(block, &indent, &padding); codeFormatter.indentFor(block, &indent, &padding);