aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlls/qtextdocument.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/qtextdocument.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/qtextdocument.cpp')
-rw-r--r--src/qmlls/qtextdocument.cpp116
1 files changed, 116 insertions, 0 deletions
diff --git a/src/qmlls/qtextdocument.cpp b/src/qmlls/qtextdocument.cpp
new file mode 100644
index 0000000000..bff8c32f9a
--- /dev/null
+++ b/src/qmlls/qtextdocument.cpp
@@ -0,0 +1,116 @@
+// 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 "qtextdocument_p.h"
+#include "qtextblock_p.h"
+
+namespace Utils {
+
+TextDocument::TextDocument(const QString &text)
+{
+ setPlainText(text);
+}
+
+TextBlock TextDocument::findBlockByNumber(int blockNumber) const
+{
+ return (blockNumber >= 0 && blockNumber < m_blocks.size())
+ ? m_blocks.at(blockNumber).textBlock
+ : TextBlock();
+}
+
+TextBlock TextDocument::findBlockByLineNumber(int lineNumber) const
+{
+ return findBlockByNumber(lineNumber);
+}
+
+QChar TextDocument::characterAt(int pos) const
+{
+ return m_content.at(pos);
+}
+
+int TextDocument::characterCount() const
+{
+ return m_content.size();
+}
+
+TextBlock TextDocument::begin() const
+{
+ return m_blocks.isEmpty() ? TextBlock() : m_blocks.at(0).textBlock;
+}
+
+TextBlock TextDocument::firstBlock() const
+{
+ return begin();
+}
+
+TextBlock TextDocument::lastBlock() const
+{
+ return m_blocks.isEmpty() ? TextBlock() : m_blocks.last().textBlock;
+}
+
+std::optional<int> TextDocument::version() const
+{
+ return m_version;
+}
+
+void TextDocument::setVersion(std::optional<int> v)
+{
+ m_version = v;
+}
+
+QString TextDocument::toPlainText() const
+{
+ return m_content;
+}
+
+void TextDocument::setPlainText(const QString &text)
+{
+ m_content = text;
+ m_blocks.clear();
+
+ int blockStart = 0;
+ int blockNumber = 0;
+ while (blockStart < text.size()) {
+ Block block;
+ block.textBlock.setBlockNumber(blockNumber++);
+ block.textBlock.setPosition(blockStart);
+ block.textBlock.setDocument(this);
+
+ int blockEnd = text.indexOf(u'\n', blockStart) + 1;
+ if (blockEnd == 0)
+ blockEnd = text.size();
+
+ block.textBlock.setLength(blockEnd - blockStart);
+ m_blocks.append(block);
+ blockStart = blockEnd;
+ }
+}
+
+bool TextDocument::isModified() const
+{
+ return m_modified;
+}
+
+void TextDocument::setModified(bool modified)
+{
+ m_modified = modified;
+}
+
+void TextDocument::setUserState(int blockNumber, int state)
+{
+ if (blockNumber >= 0 && blockNumber < m_blocks.size())
+ m_blocks[blockNumber].userState = state;
+}
+
+int TextDocument::userState(int blockNumber) const
+{
+ return (blockNumber >= 0 && blockNumber < m_blocks.size()) ? m_blocks[blockNumber].userState
+ : -1;
+}
+
+QMutex *TextDocument::mutex() const
+{
+ return &m_mutex;
+}
+
+} // namespace Utils