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 (auto plainText = std::get_if<TextDocumentContentChangeEventVariant2>(&change)) {
48 document->setPlainText(QString::fromUtf8(plainText->text));
49 continue;
50 }
51
52 const auto &withRange = std::get<TextDocumentContentChangeEventVariant1>(change);
53 const auto &range = withRange.range;
54 const auto &rangeStart = range.start;
55 const int start =
56 document->findBlockByNumber(rangeStart.line).position() + rangeStart.character;
57 const auto &rangeEnd = range.end;
58 const int end =
59 document->findBlockByNumber(rangeEnd.line).position() + rangeEnd.character;
60
61 document->setPlainText(document->toPlainText().replace(
62 start, end - start, QString::fromUtf8(withRange.text)));
63 }
64 document->setVersion(params.textDocument.version);
65 qCDebug(lspServerLog).noquote()
66 << "text is\n:----------" << document->toPlainText() << "\n_________";
67 }
68 m_codeModelManager->addOpenToUpdate(url);
69}
70
71void TextSynchronization::registerHandlers(QLanguageServer *server, QLanguageServerProtocol *)
72{
73 QObject::connect(server->notifySignals(),
74 &QLspNotifySignals::receivedDidOpenTextDocumentNotification, this,
75 &TextSynchronization::didOpenTextDocument);
76
77 QObject::connect(server->notifySignals(),
78 &QLspNotifySignals::receivedDidChangeTextDocumentNotification, this,
79 &TextSynchronization::didDidChangeTextDocument);
80
81 QObject::connect(server->notifySignals(),
82 &QLspNotifySignals::receivedDidCloseTextDocumentNotification, this,
83 &TextSynchronization::didCloseTextDocument);
84}
85
86void TextSynchronization::setupCapabilities(QLspSpecification::ServerCapabilities &caps)
87{
88 TextDocumentSyncOptions syncOptions;
89 syncOptions.openClose = true;
90 syncOptions.change = TextDocumentSyncKind::Incremental;
91 caps.textDocumentSync = syncOptions;
92}
93
94QT_END_NAMESPACE
void setupCapabilities(QLspSpecification::ServerCapabilities &caps) override
void didDidChangeTextDocument(const QLspSpecification::DidChangeTextDocumentParams &params)
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
void didCloseTextDocument(const QLspSpecification::DidCloseTextDocumentParams &params)