forked from qt-creator/qt-creator
Utils: Move common positionInText function to Utils::Text
Change-Id: I5d74a73058ca457b0fb3f13eaf945f224d5699fb Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -58,6 +58,13 @@ Utils::OptionalLineColumn convertPosition(const QTextDocument *document, int pos
|
||||
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)
|
||||
{
|
||||
if (pos < 0)
|
||||
|
||||
@@ -43,6 +43,9 @@ QTCREATOR_UTILS_EXPORT bool convertPosition(const QTextDocument *document,
|
||||
QTCREATOR_UTILS_EXPORT
|
||||
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 QTextCursor selectAt(QTextCursor textCursor, uint line, uint column, uint length);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user