forked from qt-creator/qt-creator
Clang: Don't replace text if the original text is equal
A replace is changing the document revision which is triggering a reparse of the translation unit. Change-Id: I73863af650dd8e6d3fb3e5ab4112609ced201614 Reviewed-by: Nikolai Kosjar <nikolai.kosjar@theqtcompany.com>
This commit is contained in:
@@ -85,7 +85,7 @@ void ClangAssistProposalItem::applyContextualContent(TextEditor::TextEditorWidge
|
|||||||
{
|
{
|
||||||
const CodeCompletion ccr = codeCompletion();
|
const CodeCompletion ccr = codeCompletion();
|
||||||
|
|
||||||
QString toInsert = text();
|
QString textToBeInserted = text();
|
||||||
QString extraChars;
|
QString extraChars;
|
||||||
int extraLength = 0;
|
int extraLength = 0;
|
||||||
int cursorOffset = 0;
|
int cursorOffset = 0;
|
||||||
@@ -96,7 +96,7 @@ void ClangAssistProposalItem::applyContextualContent(TextEditor::TextEditorWidge
|
|||||||
if (m_typedChar == QLatin1Char('(')) // Eat the opening parenthesis
|
if (m_typedChar == QLatin1Char('(')) // Eat the opening parenthesis
|
||||||
m_typedChar = QChar();
|
m_typedChar = QChar();
|
||||||
} else if (m_completionOperator == T_STRING_LITERAL || m_completionOperator == T_ANGLE_STRING_LITERAL) {
|
} else if (m_completionOperator == T_STRING_LITERAL || m_completionOperator == T_ANGLE_STRING_LITERAL) {
|
||||||
if (!toInsert.endsWith(QLatin1Char('/'))) {
|
if (!textToBeInserted.endsWith(QLatin1Char('/'))) {
|
||||||
extraChars += QLatin1Char((m_completionOperator == T_ANGLE_STRING_LITERAL) ? '>' : '"');
|
extraChars += QLatin1Char((m_completionOperator == T_ANGLE_STRING_LITERAL) ? '>' : '"');
|
||||||
} else {
|
} else {
|
||||||
if (m_typedChar == QLatin1Char('/')) // Eat the slash
|
if (m_typedChar == QLatin1Char('/')) // Eat the slash
|
||||||
@@ -110,7 +110,7 @@ void ClangAssistProposalItem::applyContextualContent(TextEditor::TextEditorWidge
|
|||||||
|
|
||||||
converter.parseChunks(ccr.chunks());
|
converter.parseChunks(ccr.chunks());
|
||||||
|
|
||||||
toInsert = converter.text();
|
textToBeInserted = converter.text();
|
||||||
if (converter.hasPlaceholderPositions())
|
if (converter.hasPlaceholderPositions())
|
||||||
cursorOffset = converter.placeholderPositions().at(0) - converter.text().size();
|
cursorOffset = converter.placeholderPositions().at(0) - converter.text().size();
|
||||||
} else if (!ccr.text().isEmpty()) {
|
} else if (!ccr.text().isEmpty()) {
|
||||||
@@ -194,8 +194,8 @@ void ClangAssistProposalItem::applyContextualContent(TextEditor::TextEditorWidge
|
|||||||
int existLength = 0;
|
int existLength = 0;
|
||||||
if (!existingText.isEmpty() && ccr.completionKind() != CodeCompletion::KeywordCompletionKind) {
|
if (!existingText.isEmpty() && ccr.completionKind() != CodeCompletion::KeywordCompletionKind) {
|
||||||
// Calculate the exist length in front of the extra chars
|
// Calculate the exist length in front of the extra chars
|
||||||
existLength = toInsert.length() - (editorWidget->position() - basePosition);
|
existLength = textToBeInserted.length() - (editorWidget->position() - basePosition);
|
||||||
while (!existingText.startsWith(toInsert.right(existLength))) {
|
while (!existingText.startsWith(textToBeInserted.right(existLength))) {
|
||||||
if (--existLength == 0)
|
if (--existLength == 0)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -208,25 +208,30 @@ void ClangAssistProposalItem::applyContextualContent(TextEditor::TextEditorWidge
|
|||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
toInsert += extraChars;
|
|
||||||
|
textToBeInserted += extraChars;
|
||||||
|
|
||||||
// Insert the remainder of the name
|
// Insert the remainder of the name
|
||||||
const int length = editorWidget->position() - basePosition + existLength + extraLength;
|
const int length = editorWidget->position() - basePosition + existLength + extraLength;
|
||||||
editorWidget->setCursorPosition(basePosition);
|
const auto textToBeReplaced = editorWidget->textAt(basePosition, length);
|
||||||
editorWidget->replace(length, toInsert);
|
|
||||||
if (cursorOffset)
|
|
||||||
editorWidget->setCursorPosition(editorWidget->position() + cursorOffset);
|
|
||||||
|
|
||||||
// indent the statement
|
if (textToBeReplaced != textToBeInserted) {
|
||||||
if (ccr.completionKind() == CodeCompletion::KeywordCompletionKind) {
|
editorWidget->setCursorPosition(basePosition);
|
||||||
auto selectionCursor = editorWidget->textCursor();
|
editorWidget->replace(length, textToBeInserted);
|
||||||
selectionCursor.setPosition(basePosition);
|
if (cursorOffset)
|
||||||
selectionCursor.setPosition(basePosition + toInsert.size(), QTextCursor::KeepAnchor);
|
editorWidget->setCursorPosition(editorWidget->position() + cursorOffset);
|
||||||
|
|
||||||
auto basePositionCursor = editorWidget->textCursor();
|
// indent the statement
|
||||||
basePositionCursor.setPosition(basePosition);
|
if (ccr.completionKind() == CodeCompletion::KeywordCompletionKind) {
|
||||||
if (hasOnlyBlanksBeforeCursorInLine(basePositionCursor))
|
auto selectionCursor = editorWidget->textCursor();
|
||||||
editorWidget->textDocument()->autoIndent(selectionCursor);
|
selectionCursor.setPosition(basePosition);
|
||||||
|
selectionCursor.setPosition(basePosition + textToBeInserted.size(), QTextCursor::KeepAnchor);
|
||||||
|
|
||||||
|
auto basePositionCursor = editorWidget->textCursor();
|
||||||
|
basePositionCursor.setPosition(basePosition);
|
||||||
|
if (hasOnlyBlanksBeforeCursorInLine(basePositionCursor))
|
||||||
|
editorWidget->textDocument()->autoIndent(selectionCursor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user