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>
This commit is contained in:
Ivan Donchevskii
2018-01-26 17:04:38 +01:00
parent 0c50d54da7
commit fbbdfd2444
6 changed files with 20 additions and 4 deletions

View File

@@ -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};
}

View File

@@ -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;
}

View File

@@ -348,8 +348,7 @@ ClangEditorDocumentProcessor::cursorInfo(const CppTools::CursorInfoParams &param
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);

View File

@@ -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

View File

@@ -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

View File

@@ -100,6 +100,9 @@ SourceLocation::SourceLocation(CXTranslationUnit cxTranslationUnit,
const char *contents = clang_getFileContents(cxTranslationUnit, cxFile, nullptr);
if (!contents)
return;
// (1) column in SourceLocation is the actual column shown by CppEditor.
// (2) column in Clang is the utf8 byte offset from the beginning of the line.
// Here we convert column from (2) to (1).
column_ = static_cast<uint>(QString::fromUtf8(&contents[lineStart],
static_cast<int>(column_)).size());
}