Utils: Move common positionInText function to Utils::Text

Change-Id: I5d74a73058ca457b0fb3f13eaf945f224d5699fb
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Ivan Donchevskii
2018-06-15 14:33:01 +02:00
parent b6dfab804b
commit a91956f300
4 changed files with 29 additions and 26 deletions

View File

@@ -63,18 +63,6 @@ QTextEdit::ExtraSelection createExtraSelections(const QTextCharFormat &mainforma
return extraSelection;
}
int positionInText(QTextDocument *textDocument,
const ClangBackEnd::SourceLocationContainer &sourceLocationContainer)
{
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;
}
void addRangeSelections(const ClangBackEnd::DiagnosticContainer &diagnostic,
QTextDocument *textDocument,
const QTextCharFormat &contextFormat,
@@ -82,8 +70,13 @@ void addRangeSelections(const ClangBackEnd::DiagnosticContainer &diagnostic,
{
for (auto &&range : diagnostic.ranges) {
QTextCursor cursor(textDocument);
cursor.setPosition(positionInText(textDocument, range.start));
cursor.setPosition(positionInText(textDocument, range.end), QTextCursor::KeepAnchor);
cursor.setPosition(::Utils::Text::positionInText(textDocument,
range.start.line,
range.start.column));
cursor.setPosition(::Utils::Text::positionInText(textDocument,
range.end.line,
range.end.column),
QTextCursor::KeepAnchor);
auto extraSelection = createExtraSelections(contextFormat, cursor);
@@ -127,11 +120,15 @@ QTextCursor createSelectionCursor(QTextDocument *textDocument,
const ClangBackEnd::SourceLocationContainer &sourceLocationContainer)
{
QTextCursor cursor(textDocument);
cursor.setPosition(positionInText(textDocument, sourceLocationContainer));
cursor.setPosition(::Utils::Text::positionInText(textDocument,
sourceLocationContainer.line,
sourceLocationContainer.column));
selectToLocationEnd(cursor);
if (!cursor.hasSelection()) {
cursor.setPosition(positionInText(textDocument, sourceLocationContainer) - 1);
cursor.setPosition(::Utils::Text::positionInText(textDocument,
sourceLocationContainer.line,
sourceLocationContainer.column) - 1);
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2);
}

View File

@@ -205,20 +205,16 @@ void ClangEditorDocumentProcessor::updateCodeWarnings(
}
namespace {
int positionInText(QTextDocument *textDocument,
const ClangBackEnd::SourceLocationContainer &sourceLocationContainer)
{
auto textBlock = textDocument->findBlockByNumber(int(sourceLocationContainer.line) - 1);
return textBlock.position() + int(sourceLocationContainer.column) - 1;
}
TextEditor::BlockRange
toTextEditorBlock(QTextDocument *textDocument,
const ClangBackEnd::SourceRangeContainer &sourceRangeContainer)
{
return TextEditor::BlockRange(positionInText(textDocument, sourceRangeContainer.start),
positionInText(textDocument, sourceRangeContainer.end));
return TextEditor::BlockRange(::Utils::Text::positionInText(textDocument,
sourceRangeContainer.start.line,
sourceRangeContainer.start.column),
::Utils::Text::positionInText(textDocument,
sourceRangeContainer.end.line,
sourceRangeContainer.end.column));
}
QList<TextEditor::BlockRange>