forked from qt-creator/qt-creator
Utils: use Text::Position instead of LineColumn in textutils
Change-Id: I606b0b4f8106bdb2f97383d6c81ac065e7e61858 Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
@@ -47,6 +47,15 @@ Position Position::fromFileName(QStringView fileName, int &postfixPos)
|
|||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Position Position::fromPositionInDocument(const QTextDocument *document, int pos)
|
||||||
|
{
|
||||||
|
const QTextBlock block = document->findBlock(pos);
|
||||||
|
if (block.isValid())
|
||||||
|
return {block.blockNumber() + 1, pos - block.position()};
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
int Range::length(const QString &text) const
|
int Range::length(const QString &text) const
|
||||||
{
|
{
|
||||||
if (begin.line == end.line)
|
if (begin.line == end.line)
|
||||||
@@ -87,15 +96,6 @@ bool convertPosition(const QTextDocument *document, int pos, int *line, int *col
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LineColumn convertPosition(const QTextDocument *document, int pos)
|
|
||||||
{
|
|
||||||
const QTextBlock block = document->findBlock(pos);
|
|
||||||
if (block.isValid())
|
|
||||||
return {block.blockNumber() + 1, pos - block.position()};
|
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
int positionInText(const QTextDocument *textDocument, int line, int column)
|
int positionInText(const QTextDocument *textDocument, int line, int column)
|
||||||
{
|
{
|
||||||
// Deduct 1 from line and column since they are 1-based.
|
// Deduct 1 from line and column since they are 1-based.
|
||||||
@@ -201,20 +201,6 @@ int utf8NthLineOffset(const QTextDocument *textDocument, const QByteArray &buffe
|
|||||||
return utf8Offset;
|
return utf8Offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
LineColumn utf16LineColumn(const QByteArray &utf8Buffer, int utf8Offset)
|
|
||||||
{
|
|
||||||
LineColumn lineColumn;
|
|
||||||
lineColumn.line = static_cast<int>(
|
|
||||||
std::count(utf8Buffer.begin(), utf8Buffer.begin() + utf8Offset, '\n'))
|
|
||||||
+ 1;
|
|
||||||
const int startOfLineOffset = utf8Offset ? (utf8Buffer.lastIndexOf('\n', utf8Offset - 1) + 1)
|
|
||||||
: 0;
|
|
||||||
lineColumn.column = QString::fromUtf8(
|
|
||||||
utf8Buffer.mid(startOfLineOffset, utf8Offset - startOfLineOffset))
|
|
||||||
.length();
|
|
||||||
return lineColumn;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString utf16LineTextInUtf8Buffer(const QByteArray &utf8Buffer, int currentUtf8Offset)
|
QString utf16LineTextInUtf8Buffer(const QByteArray &utf8Buffer, int currentUtf8Offset)
|
||||||
{
|
{
|
||||||
const int lineStartUtf8Offset = currentUtf8Offset
|
const int lineStartUtf8Offset = currentUtf8Offset
|
||||||
|
|||||||
@@ -5,8 +5,6 @@
|
|||||||
|
|
||||||
#include "utils_global.h"
|
#include "utils_global.h"
|
||||||
|
|
||||||
#include "linecolumn.h"
|
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
@@ -29,7 +27,10 @@ public:
|
|||||||
|
|
||||||
bool operator!=(const Position &other) const { return !(operator==(other)); }
|
bool operator!=(const Position &other) const { return !(operator==(other)); }
|
||||||
|
|
||||||
|
bool isValid() const { return line > 0 && column >= 0; }
|
||||||
|
|
||||||
static Position fromFileName(QStringView fileName, int &postfixPos);
|
static Position fromFileName(QStringView fileName, int &postfixPos);
|
||||||
|
static Position fromPositionInDocument(const QTextDocument *document, int pos);
|
||||||
};
|
};
|
||||||
|
|
||||||
class QTCREATOR_UTILS_EXPORT Range
|
class QTCREATOR_UTILS_EXPORT Range
|
||||||
@@ -70,8 +71,6 @@ QTCREATOR_UTILS_EXPORT void applyReplacements(QTextDocument *doc, const Replacem
|
|||||||
QTCREATOR_UTILS_EXPORT bool convertPosition(const QTextDocument *document,
|
QTCREATOR_UTILS_EXPORT bool convertPosition(const QTextDocument *document,
|
||||||
int pos,
|
int pos,
|
||||||
int *line, int *column);
|
int *line, int *column);
|
||||||
QTCREATOR_UTILS_EXPORT
|
|
||||||
LineColumn convertPosition(const QTextDocument *document, int pos);
|
|
||||||
|
|
||||||
// line and column are 1-based
|
// line and column are 1-based
|
||||||
QTCREATOR_UTILS_EXPORT int positionInText(const QTextDocument *textDocument, int line, int column);
|
QTCREATOR_UTILS_EXPORT int positionInText(const QTextDocument *textDocument, int line, int column);
|
||||||
@@ -90,7 +89,6 @@ QTCREATOR_UTILS_EXPORT int utf8NthLineOffset(const QTextDocument *textDocument,
|
|||||||
const QByteArray &buffer,
|
const QByteArray &buffer,
|
||||||
int line);
|
int line);
|
||||||
|
|
||||||
QTCREATOR_UTILS_EXPORT LineColumn utf16LineColumn(const QByteArray &utf8Buffer, int utf8Offset);
|
|
||||||
QTCREATOR_UTILS_EXPORT QString utf16LineTextInUtf8Buffer(const QByteArray &utf8Buffer,
|
QTCREATOR_UTILS_EXPORT QString utf16LineTextInUtf8Buffer(const QByteArray &utf8Buffer,
|
||||||
int currentUtf8Offset);
|
int currentUtf8Offset);
|
||||||
|
|
||||||
|
|||||||
@@ -1030,6 +1030,7 @@ bool LanguageFilter::operator!=(const LanguageFilter &other) const
|
|||||||
TextEditor::BaseTextEditor *jsonEditor()
|
TextEditor::BaseTextEditor *jsonEditor()
|
||||||
{
|
{
|
||||||
using namespace TextEditor;
|
using namespace TextEditor;
|
||||||
|
using namespace Utils::Text;
|
||||||
BaseTextEditor *editor = PlainTextEditorFactory::createPlainTextEditor();
|
BaseTextEditor *editor = PlainTextEditorFactory::createPlainTextEditor();
|
||||||
TextDocument *document = editor->textDocument();
|
TextDocument *document = editor->textDocument();
|
||||||
TextEditorWidget *widget = editor->editorWidget();
|
TextEditorWidget *widget = editor->editorWidget();
|
||||||
@@ -1052,12 +1053,11 @@ TextEditor::BaseTextEditor *jsonEditor()
|
|||||||
QJsonDocument::fromJson(content.toUtf8(), &error);
|
QJsonDocument::fromJson(content.toUtf8(), &error);
|
||||||
if (error.error == QJsonParseError::NoError)
|
if (error.error == QJsonParseError::NoError)
|
||||||
return;
|
return;
|
||||||
const Utils::LineColumn lineColumn
|
const Position pos = Position::fromPositionInDocument(document->document(), error.offset);
|
||||||
= Utils::Text::convertPosition(document->document(), error.offset);
|
if (!pos.isValid())
|
||||||
if (!lineColumn.isValid())
|
|
||||||
return;
|
return;
|
||||||
auto mark = new TextMark(Utils::FilePath(),
|
auto mark = new TextMark(Utils::FilePath(),
|
||||||
lineColumn.line,
|
pos.line,
|
||||||
{::LanguageClient::Tr::tr("JSON Error"), jsonMarkId});
|
{::LanguageClient::Tr::tr("JSON Error"), jsonMarkId});
|
||||||
mark->setLineAnnotation(error.errorString());
|
mark->setLineAnnotation(error.errorString());
|
||||||
mark->setColor(Utils::Theme::CodeModel_Error_TextMarkColor);
|
mark->setColor(Utils::Theme::CodeModel_Error_TextMarkColor);
|
||||||
|
|||||||
Reference in New Issue
Block a user