Utils: move TextPosition/Range to textutils

Change-Id: Id94a7a96f3b0f978e94850d67eb4b8fba6c18fe2
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
David Schulz
2023-05-09 13:49:57 +02:00
parent fc95d7a737
commit 3f2832289d
11 changed files with 76 additions and 91 deletions

View File

@@ -5,26 +5,6 @@
namespace Utils {
int Search::TextRange::length(const QString &text) const
{
if (begin.line == end.line)
return end.column - begin.column;
const int lineCount = end.line - begin.line;
int index = text.indexOf(QChar::LineFeed);
int currentLine = 1;
while (index > 0 && currentLine < lineCount) {
++index;
index = text.indexOf(QChar::LineFeed, index);
++currentLine;
}
if (index < 0)
return 0;
return index - begin.column + end.column;
}
SearchResultColor::SearchResultColor(const QColor &textBg, const QColor &textFg,
const QColor &highlightBg, const QColor &highlightFg,
const QColor &functionBg, const QColor &functionFg)
@@ -72,16 +52,6 @@ QTCREATOR_UTILS_EXPORT size_t qHash(SearchResultColor::Style style, uint seed)
return ::qHash(a, seed);
}
bool Search::TextPosition::operator==(const Search::TextPosition &other) const
{
return line == other.line && column == other.column;
}
bool Search::TextRange::operator==(const Search::TextRange &other) const
{
return begin == other.begin && end == other.end;
}
bool SearchResultItem::operator==(const SearchResultItem &other) const
{
return m_path == other.m_path && m_lineText == other.m_lineText

View File

@@ -7,6 +7,7 @@
#include <utils/filepath.h>
#include <utils/hostosinfo.h>
#include <utils/textutils.h>
#include <QColor>
#include <QHash>
@@ -18,36 +19,6 @@
namespace Utils {
namespace Search {
class QTCREATOR_UTILS_EXPORT TextPosition
{
public:
int line = -1; // (0 or -1 for no line number)
int column = -1; // 0-based starting position for a mark (-1 for no mark)
bool operator<(const TextPosition &other) const
{ return line < other.line || (line == other.line && column < other.column); }
bool operator==(const TextPosition &other) const;
bool operator!=(const TextPosition &other) const { return !(operator==(other)); }
};
class QTCREATOR_UTILS_EXPORT TextRange
{
public:
QString mid(const QString &text) const { return text.mid(begin.column, length(text)); }
int length(const QString &text) const;
TextPosition begin;
TextPosition end;
bool operator<(const TextRange &other) const { return begin < other.begin; }
bool operator==(const TextRange &other) const;
bool operator!=(const TextRange &other) const { return !(operator==(other)); }
};
} // namespace Search
class QTCREATOR_UTILS_EXPORT SearchResultColor
{
public:
@@ -88,8 +59,8 @@ public:
QVariant userData() const { return m_userData; }
void setUserData(const QVariant &userData) { m_userData = userData; }
Search::TextRange mainRange() const { return m_mainRange; }
void setMainRange(const Search::TextRange &mainRange) { m_mainRange = mainRange; }
Text::Range mainRange() const { return m_mainRange; }
void setMainRange(const Text::Range &mainRange) { m_mainRange = mainRange; }
void setMainRange(int line, int column, int length);
bool useTextEditorFont() const { return m_useTextEditorFont; }
@@ -116,7 +87,7 @@ private:
QString m_lineText; // text to show for the item itself
QIcon m_icon; // icon to show in front of the item (by be null icon to hide)
QVariant m_userData; // user data for identification of the item
Search::TextRange m_mainRange;
Text::Range m_mainRange;
bool m_useTextEditorFont = false;
bool m_selectForReplacement = true;
SearchResultColor::Style m_style = SearchResultColor::Style::Default;
@@ -129,4 +100,3 @@ using SearchResultItems = QList<SearchResultItem>;
Q_DECLARE_METATYPE(Utils::SearchResultItem)
Q_DECLARE_METATYPE(Utils::SearchResultItems)
Q_DECLARE_METATYPE(Utils::Search::TextPosition)

View File

@@ -6,8 +6,37 @@
#include <QTextDocument>
#include <QTextBlock>
namespace Utils {
namespace Text {
namespace Utils::Text {
bool Position::operator==(const Position &other) const
{
return line == other.line && column == other.column;
}
int Range::length(const QString &text) const
{
if (begin.line == end.line)
return end.column - begin.column;
const int lineCount = end.line - begin.line;
int index = text.indexOf(QChar::LineFeed);
int currentLine = 1;
while (index > 0 && currentLine < lineCount) {
++index;
index = text.indexOf(QChar::LineFeed, index);
++currentLine;
}
if (index < 0)
return 0;
return index - begin.column + end.column;
}
bool Range::operator==(const Range &other) const
{
return begin == other.begin && end == other.end;
}
bool convertPosition(const QTextDocument *document, int pos, int *line, int *column)
{
@@ -211,5 +240,4 @@ void applyReplacements(QTextDocument *doc, const Replacements &replacements)
editCursor.endEditBlock();
}
} // Text
} // Utils
} // namespace Utils::Text

View File

@@ -17,6 +17,34 @@ QT_END_NAMESPACE
namespace Utils {
namespace Text {
class QTCREATOR_UTILS_EXPORT Position
{
public:
int line = 0; // 1-based
int column = -1; // 0-based
bool operator<(const Position &other) const
{ return line < other.line || (line == other.line && column < other.column); }
bool operator==(const Position &other) const;
bool operator!=(const Position &other) const { return !(operator==(other)); }
};
class QTCREATOR_UTILS_EXPORT Range
{
public:
QString mid(const QString &text) const { return text.mid(begin.column, length(text)); }
int length(const QString &text) const;
Position begin;
Position end;
bool operator<(const Range &other) const { return begin < other.begin; }
bool operator==(const Range &other) const;
bool operator!=(const Range &other) const { return !(operator==(other)); }
};
struct Replacement
{
Replacement() = default;
@@ -66,3 +94,5 @@ QTCREATOR_UTILS_EXPORT QString utf16LineTextInUtf8Buffer(const QByteArray &utf8B
} // Text
} // Utils
Q_DECLARE_METATYPE(Utils::Text::Position)

View File

@@ -35,8 +35,8 @@
#include <utils/mimeutils.h>
#include <utils/pathchooser.h>
#include <utils/savefile.h>
#include <utils/searchresultitem.h>
#include <utils/stringutils.h>
#include <utils/textutils.h>
#include <utils/theme/theme.h>
#include <utils/theme/theme_p.h>
@@ -72,7 +72,7 @@ void CorePlugin::setupSystemEnvironment()
CorePlugin::CorePlugin()
{
qRegisterMetaType<Id>();
qRegisterMetaType<Utils::Search::TextPosition>();
qRegisterMetaType<Utils::Text::Position>();
qRegisterMetaType<Utils::CommandLine>();
qRegisterMetaType<Utils::FilePath>();
qRegisterMetaType<Utils::Environment>();

View File

@@ -3192,7 +3192,7 @@ void EditorManager::openEditorAtSearchResult(const SearchResultItem &item,
openEditor(FilePath::fromUserInput(item.lineText()), editorId, flags, newEditor);
return;
}
const Search::TextPosition position = item.mainRange().begin;
const Text::Position position = item.mainRange().begin;
openEditorAt({FilePath::fromUserInput(path.first()), position.line, position.column},
editorId, flags, newEditor);
}

View File

@@ -40,18 +40,6 @@ namespace Core {
\internal
*/
/*!
\class Core::Search::TextPosition
\inmodule QtCreator
\internal
*/
/*!
\class Core::Search::TextRange
\inmodule QtCreator
\internal
*/
namespace Internal {
class InternalScrollArea : public QScrollArea

View File

@@ -193,7 +193,7 @@ bool SymbolSupport::supportsFindUsages(TextEditor::TextDocument *document) const
struct ItemData
{
Utils::Search::TextRange range;
Utils::Text::Range range;
QVariant userData;
};
@@ -616,10 +616,10 @@ QString SymbolSupport::derivePlaceholder(const QString &oldSymbol, const QString
return m_defaultSymbolMapper ? m_defaultSymbolMapper(oldSymbol) : oldSymbol;
}
Utils::Search::TextRange SymbolSupport::convertRange(const Range &range)
Utils::Text::Range SymbolSupport::convertRange(const Range &range)
{
const auto convertPosition = [](const Position &pos) {
return Utils::Search::TextPosition{pos.line() + 1, pos.character()};
return Utils::Text::Position{pos.line() + 1, pos.character()};
};
return {convertPosition(range.start()), convertPosition(range.end())};
}

View File

@@ -43,7 +43,7 @@ public:
const std::function<void()> &callback = {},
bool preferLowerCaseFileNames = true);
static Utils::Search::TextRange convertRange(const LanguageServerProtocol::Range &range);
static Utils::Text::Range convertRange(const LanguageServerProtocol::Range &range);
static QStringList getFileContents(const Utils::FilePath &filePath);
using SymbolMapper = std::function<QString(const QString &)>;

View File

@@ -97,11 +97,10 @@ void applyTextEdit(TextDocumentManipulatorInterface &manipulator,
const TextEdit &edit,
bool newTextIsSnippet)
{
using namespace Utils::Text;
const Range range = edit.range();
const QTextDocument *doc = manipulator.textCursorAt(manipulator.currentPosition()).document();
const int start = positionInText(doc, range.start().line() + 1, range.start().character() + 1);
const int end = positionInText(doc, range.end().line() + 1, range.end().character() + 1);
const int start = Text::positionInText(doc, range.start().line() + 1, range.start().character() + 1);
const int end = Text::positionInText(doc, range.end().line() + 1, range.end().character() + 1);
if (newTextIsSnippet) {
manipulator.replace(start, end - start, {});
manipulator.insertCodeSnippet(start, edit.newText(), &parseSnippet);

View File

@@ -850,7 +850,7 @@ private:
static QFutureWatcher<SearchResultItems> *m_selectWatcher;
};
static QTextCursor selectRange(QTextDocument *textDocument, const Search::TextRange &range,
static QTextCursor selectRange(QTextDocument *textDocument, const Text::Range &range,
TextEditorWidgetPrivate::SearchResult *searchResult = nullptr)
{
const int startLine = qMax(range.begin.line - 1, 0);