Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtextsynchronization.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include "qqmllsutils_p.h"
6#include "qtextdocument_p.h"
7
8using namespace QLspSpecification;
9using namespace Qt::StringLiterals;
10
12
14 : QLanguageServerModule(parent), m_codeModel(codeModel)
15{
16}
17
18void TextSynchronization::didCloseTextDocument(const DidCloseTextDocumentParams &params)
19{
20 m_codeModel->closeOpenFile(QQmlLSUtils::lspUriToQmlUrl(params.textDocument.uri));
21}
22
23void TextSynchronization::didOpenTextDocument(const DidOpenTextDocumentParams &params)
24{
25 const TextDocumentItem &item = params.textDocument;
26 const QString fileName = m_codeModel->url2Path(QQmlLSUtils::lspUriToQmlUrl(item.uri));
27 m_codeModel->newOpenFile(QQmlLSUtils::lspUriToQmlUrl(item.uri), item.version,
29}
30
31void TextSynchronization::didDidChangeTextDocument(const DidChangeTextDocumentParams &params)
32{
34 const QString fileName = m_codeModel->url2Path(url);
35 auto openDoc = m_codeModel->openDocumentByUrl(url);
36 std::shared_ptr<Utils::TextDocument> document = openDoc.textDocument;
37 if (!document) {
38 qCWarning(lspServerLog) << "Ingnoring changes to non open or closed document"
40 return;
41 }
42 const auto &changes = params.contentChanges;
43 {
44 QMutexLocker l(document->mutex());
45 for (const auto &change : changes) {
46 if (!change.range) {
47 document->setPlainText(QString::fromUtf8(change.text));
48 continue;
49 }
50
51 const auto &range = *change.range;
52 const auto &rangeStart = range.start;
53 const int start =
54 document->findBlockByNumber(rangeStart.line).position() + rangeStart.character;
55 const auto &rangeEnd = range.end;
56 const int end =
57 document->findBlockByNumber(rangeEnd.line).position() + rangeEnd.character;
58
59 document->setPlainText(document->toPlainText().replace(start, end - start,
60 QString::fromUtf8(change.text)));
61 }
62 document->setVersion(params.textDocument.version);
63 qCDebug(lspServerLog).noquote()
64 << "text is\n:----------" << document->toPlainText() << "\n_________";
65 }
66 m_codeModel->addOpenToUpdate(url);
67 m_codeModel->openNeedUpdate();
68}
69
71{
72 QObject::connect(server->notifySignals(),
73 &QLspNotifySignals::receivedDidOpenTextDocumentNotification, this,
75
76 QObject::connect(server->notifySignals(),
77 &QLspNotifySignals::receivedDidChangeTextDocumentNotification, this,
79
80 QObject::connect(server->notifySignals(),
81 &QLspNotifySignals::receivedDidCloseTextDocumentNotification, this,
83}
84
86{
87 return u"TextSynchonization"_s;
88}
89
90void TextSynchronization::setupCapabilities(const QLspSpecification::InitializeParams &,
91 QLspSpecification::InitializeResult &serverInfo)
92{
93 TextDocumentSyncOptions syncOptions;
94 syncOptions.openClose = true;
95 syncOptions.change = TextDocumentSyncKind::Incremental;
96 serverInfo.capabilities.textDocumentSync = syncOptions;
97}
98
\inmodule QtCore
Definition qbytearray.h:57
Implements a server for the language server protocol.
\inmodule QtCore
Definition qmutex.h:313
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
static QByteArray lspUriToQmlUrl(const QByteArray &uri)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
void newOpenFile(const QByteArray &url, int version, const QString &docText)
void addOpenToUpdate(const QByteArray &)
QString url2Path(const QByteArray &url, UrlLookup options=UrlLookup::Caching)
OpenDocument openDocumentByUrl(const QByteArray &url)
void closeOpenFile(const QByteArray &url)
void didDidChangeTextDocument(const QLspSpecification::DidChangeTextDocumentParams &params)
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
TextSynchronization(QmlLsp::QQmlCodeModel *codeModel, QObject *parent=nullptr)
QString name() const override
void setupCapabilities(const QLspSpecification::InitializeParams &clientInfo, QLspSpecification::InitializeResult &) override
void didOpenTextDocument(const QLspSpecification::DidOpenTextDocumentParams &params)
void didCloseTextDocument(const QLspSpecification::DidCloseTextDocumentParams &params)
Combined button and popup list for selecting options.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage return DBusPendingCall DBusPendingCall return DBusPendingCall return dbus_int32_t return DBusServer * server
#define qCWarning(category,...)
#define qCDebug(category,...)
GLuint GLuint end
GLsizei range
GLuint start
void ** params
QUrl url("example.com")
[constructor-url-reference]
QGraphicsItem * item