forked from qt-creator/qt-creator
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:
@@ -558,9 +558,9 @@ ClangCompletionAssistProcessor::extractLineColumn(int position)
|
|||||||
|
|
||||||
int line = -1, column = -1;
|
int line = -1, column = -1;
|
||||||
::Utils::Text::convertPosition(m_interface->textDocument(), position, &line, &column);
|
::Utils::Text::convertPosition(m_interface->textDocument(), position, &line, &column);
|
||||||
|
|
||||||
const QTextBlock block = m_interface->textDocument()->findBlock(position);
|
const QTextBlock block = m_interface->textDocument()->findBlock(position);
|
||||||
const QString stringOnTheLeft = block.text().left(column);
|
column = Utils::clangColumn(block.text(), column);
|
||||||
column = stringOnTheLeft.toUtf8().size() + 1; // '+ 1' is for 1-based columns
|
|
||||||
return {line, column};
|
return {line, column};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -64,6 +64,9 @@ int positionInText(QTextDocument *textDocument,
|
|||||||
{
|
{
|
||||||
auto textBlock = textDocument->findBlockByNumber(
|
auto textBlock = textDocument->findBlockByNumber(
|
||||||
static_cast<int>(sourceLocationContainer.line()) - 1);
|
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;
|
const int column = static_cast<int>(sourceLocationContainer.column()) - 1;
|
||||||
return textBlock.position() + column;
|
return textBlock.position() + column;
|
||||||
}
|
}
|
||||||
|
@@ -348,8 +348,7 @@ ClangEditorDocumentProcessor::cursorInfo(const CppTools::CursorInfoParams ¶m
|
|||||||
return defaultCursorInfoFuture();
|
return defaultCursorInfoFuture();
|
||||||
|
|
||||||
const QTextBlock block = params.textCursor.document()->findBlockByNumber(line - 1);
|
const QTextBlock block = params.textCursor.document()->findBlockByNumber(line - 1);
|
||||||
const QString stringOnTheLeft = block.text().left(column);
|
column = Utils::clangColumn(block.text(), column);
|
||||||
column = stringOnTheLeft.toUtf8().size() + 1; // '+ 1' is for 1-based columns
|
|
||||||
const CppTools::SemanticInfo::LocalUseMap localUses
|
const CppTools::SemanticInfo::LocalUseMap localUses
|
||||||
= CppTools::BuiltinCursorInfo::findLocalUses(params.semanticInfo.doc, line, column);
|
= CppTools::BuiltinCursorInfo::findLocalUses(params.semanticInfo.doc, line, column);
|
||||||
|
|
||||||
|
@@ -190,5 +190,15 @@ void setLastSentDocumentRevision(const QString &filePath, uint revision)
|
|||||||
document->sendTracker().setLastSentRevision(int(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 Utils
|
||||||
} // namespace Clang
|
} // namespace Clang
|
||||||
|
@@ -46,6 +46,7 @@ CppTools::ProjectPart::Ptr projectPartForFile(const QString &filePath);
|
|||||||
CppTools::ProjectPart::Ptr projectPartForFileBasedOnProcessor(const QString &filePath);
|
CppTools::ProjectPart::Ptr projectPartForFileBasedOnProcessor(const QString &filePath);
|
||||||
bool isProjectPartLoaded(const CppTools::ProjectPart::Ptr projectPart);
|
bool isProjectPartLoaded(const CppTools::ProjectPart::Ptr projectPart);
|
||||||
QString projectPartIdForFile(const QString &filePath);
|
QString projectPartIdForFile(const QString &filePath);
|
||||||
|
int clangColumn(const QString &lineText, int cppEditorColumn);
|
||||||
|
|
||||||
} // namespace Utils
|
} // namespace Utils
|
||||||
} // namespace Clang
|
} // namespace Clang
|
||||||
|
@@ -100,6 +100,9 @@ SourceLocation::SourceLocation(CXTranslationUnit cxTranslationUnit,
|
|||||||
const char *contents = clang_getFileContents(cxTranslationUnit, cxFile, nullptr);
|
const char *contents = clang_getFileContents(cxTranslationUnit, cxFile, nullptr);
|
||||||
if (!contents)
|
if (!contents)
|
||||||
return;
|
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],
|
column_ = static_cast<uint>(QString::fromUtf8(&contents[lineStart],
|
||||||
static_cast<int>(column_)).size());
|
static_cast<int>(column_)).size());
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user