aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlls/qtextcursor.cpp
diff options
context:
space:
mode:
authorSami Shalayel <sami.shalayel@qt.io>2023-01-24 10:45:21 +0100
committerSami Shalayel <sami.shalayel@qt.io>2023-02-07 19:23:13 +0100
commit3a985babf928a7c270c2f6516112d2299c670201 (patch)
tree084c3165a3e601ac901b2cc8c32603c2e17f9ffe /src/qmlls/qtextcursor.cpp
parent36f0cb435ecc7af326c745f793d104a3d5c65304 (diff)
qmlls: move into own private static library
Qmlls was completely implemented in the ./tools directory, which made its code complicated to test and try out. Also, it required some "dirty" hacks in the actual tests (including files from others targets to be able to use them) and made testing new features for qmlls more complicated. To remedy this, the qmlls code was split into a tool (qmlls) and a static library (QmlLSPrivate). The tool only contains tools/qmlls/qmllanguageservertool.cpp (which has the qmlls main method) and links to QmlLSPrivate, that contains all the other qmlls-related code. This way, the tests can also link to QmlLSPrivate and test out individual functions there without needing to include files from other targets. Also rename all the files to make syncqt happy (adding "_p" to headers and prepending "q" to headers and files and includeguards), and use QString::fromUtf8() to silence the QString()-constructor deprecation warnings. On the way, move tools/shared/qqmltoolingsettings.* into its own private static library, instead of recompiling it for each tool that requires it. Move the qqmltoolingsettings stuff into the qt namespace to be usable. Also, add qmlls as a dependency to the qmlls tests to avoid testing an outdated qmlls-binary. This commit prepares qmlls's code to implement the go-to and find-usages features. Task-number: QTBUG-109006 Change-Id: I91eed689c68a0c53fb88006de335b0f852cc1a83 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qmlls/qtextcursor.cpp')
-rw-r--r--src/qmlls/qtextcursor.cpp122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/qmlls/qtextcursor.cpp b/src/qmlls/qtextcursor.cpp
new file mode 100644
index 0000000000..1b0c72909f
--- /dev/null
+++ b/src/qmlls/qtextcursor.cpp
@@ -0,0 +1,122 @@
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+
+#include "qtextcursor_p.h"
+#include "qtextdocument_p.h"
+#include "qtextblock_p.h"
+
+namespace Utils {
+
+class TextFrame;
+class TextTable;
+class TextTableCell;
+
+TextCursor::TextCursor(TextDocument *document) : m_document(document) { }
+
+bool TextCursor::movePosition(TextCursor::MoveOperation op, TextCursor::MoveMode mode, int n)
+{
+ Q_UNUSED(n);
+ switch (op) {
+ case NoMove:
+ return true;
+ case Start:
+ m_position = 0;
+ break;
+ case PreviousCharacter:
+ while (--n >= 0) {
+ if (m_position == 0)
+ return false;
+ --m_position;
+ }
+ break;
+ case End:
+ m_position = m_document->characterCount();
+ break;
+ case NextCharacter:
+ while (--n >= 0) {
+ if (m_position == m_document->characterCount())
+ return false;
+ ++m_position;
+ }
+ break;
+ }
+
+ if (mode == MoveAnchor)
+ m_anchor = m_position;
+
+ return false;
+}
+
+int TextCursor::position() const
+{
+ return m_position;
+}
+
+void TextCursor::setPosition(int pos, Utils::TextCursor::MoveMode mode)
+{
+ m_position = pos;
+ if (mode == MoveAnchor)
+ m_anchor = pos;
+}
+
+QString TextCursor::selectedText() const
+{
+ return m_document->toPlainText().mid(qMin(m_position, m_anchor), qAbs(m_position - m_anchor));
+}
+
+void TextCursor::clearSelection()
+{
+ m_anchor = m_position;
+}
+
+TextDocument *TextCursor::document() const
+{
+ return m_document;
+}
+
+void TextCursor::insertText(const QString &text)
+{
+ const QString orig = m_document->toPlainText();
+ const QString left = orig.left(qMin(m_position, m_anchor));
+ const QString right = orig.mid(qMax(m_position, m_anchor));
+ m_document->setPlainText(left + text + right);
+}
+
+TextBlock TextCursor::block() const
+{
+ TextBlock current = m_document->firstBlock();
+ while (current.isValid()) {
+ if (current.position() <= position()
+ && current.position() + current.length() > current.position())
+ break;
+ current = current.next();
+ }
+ return current;
+}
+
+int TextCursor::positionInBlock() const
+{
+ return m_position - block().position();
+}
+
+int TextCursor::blockNumber() const
+{
+ return block().blockNumber();
+}
+
+void TextCursor::removeSelectedText()
+{
+ insertText(QString());
+}
+
+int TextCursor::selectionEnd() const
+{
+ return qMax(m_position, m_anchor);
+}
+
+bool TextCursor::isNull() const
+{
+ return m_document == nullptr;
+}
+
+} // namespace Utils