From 5e84af54a07e36f5be9dba1f4ba9bb15e701a354 Mon Sep 17 00:00:00 2001 From: Ivan Donchevskii Date: Wed, 17 May 2017 11:47:38 +0200 Subject: [PATCH] Clang: fix incomplete include text underlining Fix invalid include text selection. Task-number: QTCREATORBUG-15471 Change-Id: Ifbe00a7215f2307648e815cb283691496d02c4d0 Reviewed-by: Nikolai Kosjar --- .../clangcodemodel/clangdiagnosticmanager.cpp | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/plugins/clangcodemodel/clangdiagnosticmanager.cpp b/src/plugins/clangcodemodel/clangdiagnosticmanager.cpp index 07f84298f7e..e10793055d3 100644 --- a/src/plugins/clangcodemodel/clangdiagnosticmanager.cpp +++ b/src/plugins/clangcodemodel/clangdiagnosticmanager.cpp @@ -81,12 +81,44 @@ void addRangeSelections(const ClangBackEnd::DiagnosticContainer &diagnostic, } } +QChar selectionEndChar(const QChar startSymbol) +{ + if (startSymbol == '"') + return QLatin1Char('"'); + if (startSymbol == '<') + return QLatin1Char('>'); + return QChar(); +} + +void selectToLocationEnd(QTextCursor &cursor) +{ + const QTextBlock textBlock = cursor.document()->findBlock(cursor.position()); + const QString simplifiedStr = textBlock.text().simplified(); + if (!simplifiedStr.startsWith("#include") && !simplifiedStr.startsWith("# include")) { + // General case, not the line with #include + cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); + return; + } + + const QChar endChar = selectionEndChar(cursor.document()->characterAt(cursor.position())); + if (endChar.isNull()) { + cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); + } else { + const int endPosition = textBlock.text().indexOf(endChar, cursor.position() + - textBlock.position() + 1); + if (endPosition >= 0) + cursor.setPosition(textBlock.position() + endPosition + 1, QTextCursor::KeepAnchor); + else + cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); + } +} + QTextCursor createSelectionCursor(QTextDocument *textDocument, const ClangBackEnd::SourceLocationContainer &sourceLocationContainer) { QTextCursor cursor(textDocument); cursor.setPosition(positionInText(textDocument, sourceLocationContainer)); - cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); + selectToLocationEnd(cursor); if (!cursor.hasSelection()) { cursor.setPosition(positionInText(textDocument, sourceLocationContainer) - 1);