Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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// Qt-Security score:significant reason:default
4
9
10using namespace QLspSpecification;
11using namespace Qt::StringLiterals;
12
13QT_BEGIN_NAMESPACE
14
15TextSynchronization::TextSynchronization(QmlLsp::QQmlCodeModelManager *codeModelManager,
16 QObject *parent)
17 : QLanguageServerModule(parent), m_codeModelManager(codeModelManager)
18{
19}
20
21void TextSynchronization::didCloseTextDocument(const DidCloseTextDocumentParams &params)
22{
23 m_codeModelManager->closeOpenFile(QQmlLSUtils::lspUriToQmlUrl(params.textDocument.uri));
24}
25
26void TextSynchronization::didOpenTextDocument(const DidOpenTextDocumentParams &params)
27{
28 const TextDocumentItem &item = params.textDocument;
29 m_codeModelManager->newOpenFile(QQmlLSUtils::lspUriToQmlUrl(item.uri), item.version,
30 QString::fromUtf8(item.text));
31}
32
33void TextSynchronization::didDidChangeTextDocument(const DidChangeTextDocumentParams &params)
34{
35 QByteArray url = QQmlLSUtils::lspUriToQmlUrl(params.textDocument.uri);
36 auto openDoc = m_codeModelManager->openDocumentByUrl(url);
37 std::shared_ptr<Utils::TextDocument> document = openDoc.textDocument;
38 if (!document) {
39 qCWarning(lspServerLog) << "Ignoring changes to non open or closed document"
40 << QString::fromUtf8(url);
41 return;
42 }
43 const auto &changes = params.contentChanges;
44 {
45 QMutexLocker l(document->mutex());
46 for (const auto &change : changes) {
47 if (!change.range) {
48 document->setPlainText(QString::fromUtf8(change.text));
49 continue;
50 }
51
52 const auto &range = *change.range;
53 const auto &rangeStart = range.start;
54 const int start =
55 document->findBlockByNumber(rangeStart.line).position() + rangeStart.character;
56 const auto &rangeEnd = range.end;
57 const int end =
58 document->findBlockByNumber(rangeEnd.line).position() + rangeEnd.character;
59
60 document->setPlainText(document->toPlainText().replace(start, end - start,
61 QString::fromUtf8(change.text)));
62 }
63 document->setVersion(params.textDocument.version);
64 qCDebug(lspServerLog).noquote()
65 << "text is\n:----------" << document->toPlainText() << "\n_________";
66 }
67 m_codeModelManager->addOpenToUpdate(url);
68}
69
70void TextSynchronization::registerHandlers(QLanguageServer *server, QLanguageServerProtocol *)
71{
72 QObject::connect(server->notifySignals(),
73 &QLspNotifySignals::receivedDidOpenTextDocumentNotification, this,
74 &TextSynchronization::didOpenTextDocument);
75
76 QObject::connect(server->notifySignals(),
77 &QLspNotifySignals::receivedDidChangeTextDocumentNotification, this,
78 &TextSynchronization::didDidChangeTextDocument);
79
80 QObject::connect(server->notifySignals(),
81 &QLspNotifySignals::receivedDidCloseTextDocumentNotification, this,
82 &TextSynchronization::didCloseTextDocument);
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
99QT_END_NAMESPACE
Implements a server for the language server protocol.
void didDidChangeTextDocument(const QLspSpecification::DidChangeTextDocumentParams &params)
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
QString name() const override
void setupCapabilities(const QLspSpecification::InitializeParams &clientInfo, QLspSpecification::InitializeResult &) override
void didCloseTextDocument(const QLspSpecification::DidCloseTextDocumentParams &params)