diff options
author | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-01-26 17:04:38 +0100 |
---|---|---|
committer | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-02-01 12:22:45 +0000 |
commit | fbbdfd24446c12eff7761d16c6acbf8e24985a5c (patch) | |
tree | 0a3354b819c16b29390f4911db576b30b31bf0e3 /src/plugins/clangcodemodel | |
parent | 0c50d54da7439fd4c2a56abab9781ceeff0e28b6 (diff) | |
download | qt-creator-fbbdfd24446c12eff7761d16c6acbf8e24985a5c.tar.gz |
Clang: Add commentary about column convertion
... to/from utf8 byte offset used by Clang.
Change-Id: I294d6cd61b416e5f2d64206ee2f3f1b4a91fb1d3
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
Diffstat (limited to 'src/plugins/clangcodemodel')
5 files changed, 17 insertions, 4 deletions
diff --git a/src/plugins/clangcodemodel/clangcompletionassistprocessor.cpp b/src/plugins/clangcodemodel/clangcompletionassistprocessor.cpp index 2a4673f213..467aca1bf5 100644 --- a/src/plugins/clangcodemodel/clangcompletionassistprocessor.cpp +++ b/src/plugins/clangcodemodel/clangcompletionassistprocessor.cpp @@ -558,9 +558,9 @@ ClangCompletionAssistProcessor::extractLineColumn(int position) int line = -1, column = -1; ::Utils::Text::convertPosition(m_interface->textDocument(), position, &line, &column); + const QTextBlock block = m_interface->textDocument()->findBlock(position); - const QString stringOnTheLeft = block.text().left(column); - column = stringOnTheLeft.toUtf8().size() + 1; // '+ 1' is for 1-based columns + column = Utils::clangColumn(block.text(), column); return {line, column}; } diff --git a/src/plugins/clangcodemodel/clangdiagnosticmanager.cpp b/src/plugins/clangcodemodel/clangdiagnosticmanager.cpp index 261d196b0b..44b8db7334 100644 --- a/src/plugins/clangcodemodel/clangdiagnosticmanager.cpp +++ b/src/plugins/clangcodemodel/clangdiagnosticmanager.cpp @@ -64,6 +64,9 @@ int positionInText(QTextDocument *textDocument, { auto textBlock = textDocument->findBlockByNumber( static_cast<int>(sourceLocationContainer.line()) - 1); + // 'sourceLocationContainer' already has the CppEditor column converted from + // the utf8 byte offset from the beginning of the line provided by clang. + // - 1 is required for 0-based columns. const int column = static_cast<int>(sourceLocationContainer.column()) - 1; return textBlock.position() + column; } diff --git a/src/plugins/clangcodemodel/clangeditordocumentprocessor.cpp b/src/plugins/clangcodemodel/clangeditordocumentprocessor.cpp index afee72f3e8..4cc107b0b4 100644 --- a/src/plugins/clangcodemodel/clangeditordocumentprocessor.cpp +++ b/src/plugins/clangcodemodel/clangeditordocumentprocessor.cpp @@ -348,8 +348,7 @@ ClangEditorDocumentProcessor::cursorInfo(const CppTools::CursorInfoParams ¶m return defaultCursorInfoFuture(); const QTextBlock block = params.textCursor.document()->findBlockByNumber(line - 1); - const QString stringOnTheLeft = block.text().left(column); - column = stringOnTheLeft.toUtf8().size() + 1; // '+ 1' is for 1-based columns + column = Utils::clangColumn(block.text(), column); const CppTools::SemanticInfo::LocalUseMap localUses = CppTools::BuiltinCursorInfo::findLocalUses(params.semanticInfo.doc, line, column); diff --git a/src/plugins/clangcodemodel/clangutils.cpp b/src/plugins/clangcodemodel/clangutils.cpp index cf659ff404..30dbbe7b10 100644 --- a/src/plugins/clangcodemodel/clangutils.cpp +++ b/src/plugins/clangcodemodel/clangutils.cpp @@ -190,5 +190,15 @@ void setLastSentDocumentRevision(const QString &filePath, uint revision) document->sendTracker().setLastSentRevision(int(revision)); } +int clangColumn(const QString &lineText, int cppEditorColumn) +{ + // (1) cppEditorColumn is the actual column shown by CppEditor. + // (2) The return value is the column in Clang which is the utf8 byte offset from the beginning + // of the line. + // Here we convert column from (1) to (2). + // '+ 1' is for 1-based columns + return lineText.left(cppEditorColumn).toUtf8().size() + 1; +} + } // namespace Utils } // namespace Clang diff --git a/src/plugins/clangcodemodel/clangutils.h b/src/plugins/clangcodemodel/clangutils.h index edd26ba755..c1bf34ddf1 100644 --- a/src/plugins/clangcodemodel/clangutils.h +++ b/src/plugins/clangcodemodel/clangutils.h @@ -46,6 +46,7 @@ CppTools::ProjectPart::Ptr projectPartForFile(const QString &filePath); CppTools::ProjectPart::Ptr projectPartForFileBasedOnProcessor(const QString &filePath); bool isProjectPartLoaded(const CppTools::ProjectPart::Ptr projectPart); QString projectPartIdForFile(const QString &filePath); +int clangColumn(const QString &lineText, int cppEditorColumn); } // namespace Utils } // namespace Clang |