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

@@ -58,6 +58,13 @@ Utils::OptionalLineColumn convertPosition(const QTextDocument *document, int pos
return optional; return optional;
} }
int positionInText(QTextDocument *textDocument, int line, int column)
{
// Deduct 1 from line and column since they are 1-based.
// Column should already be converted from UTF-8 byte offset to the TextEditor column.
return textDocument->findBlockByNumber(line - 1).position() + column - 1;
}
QString textAt(QTextCursor tc, int pos, int length) QString textAt(QTextCursor tc, int pos, int length)
{ {
if (pos < 0) if (pos < 0)

View File

@@ -43,6 +43,9 @@ QTCREATOR_UTILS_EXPORT bool convertPosition(const QTextDocument *document,
QTCREATOR_UTILS_EXPORT QTCREATOR_UTILS_EXPORT
Utils::OptionalLineColumn convertPosition(const QTextDocument *document, int pos); Utils::OptionalLineColumn convertPosition(const QTextDocument *document, int pos);
// line and column are 1-based
QTCREATOR_UTILS_EXPORT int positionInText(QTextDocument *textDocument, int line, int column);
QTCREATOR_UTILS_EXPORT QString textAt(QTextCursor tc, int pos, int length); QTCREATOR_UTILS_EXPORT QString textAt(QTextCursor tc, int pos, int length);
QTCREATOR_UTILS_EXPORT QTextCursor selectAt(QTextCursor textCursor, uint line, uint column, uint length); QTCREATOR_UTILS_EXPORT QTextCursor selectAt(QTextCursor textCursor, uint line, uint column, uint length);

View File

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

View File

@@ -205,20 +205,16 @@ void ClangEditorDocumentProcessor::updateCodeWarnings(
} }
namespace { 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 TextEditor::BlockRange
toTextEditorBlock(QTextDocument *textDocument, toTextEditorBlock(QTextDocument *textDocument,
const ClangBackEnd::SourceRangeContainer &sourceRangeContainer) const ClangBackEnd::SourceRangeContainer &sourceRangeContainer)
{ {
return TextEditor::BlockRange(positionInText(textDocument, sourceRangeContainer.start), return TextEditor::BlockRange(::Utils::Text::positionInText(textDocument,
positionInText(textDocument, sourceRangeContainer.end)); sourceRangeContainer.start.line,
sourceRangeContainer.start.column),
::Utils::Text::positionInText(textDocument,
sourceRangeContainer.end.line,
sourceRangeContainer.end.column));
} }
QList<TextEditor::BlockRange> QList<TextEditor::BlockRange>