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
qqmllanguageserver.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
10#include <QtCore/qdir.h>
11
12#include <iostream>
13#include <algorithm>
14
15QT_BEGIN_NAMESPACE
16
17namespace QmlLsp {
18
19using namespace QLspSpecification;
20using namespace Qt::StringLiterals;
21/*!
22\internal
23\class QmlLsp::QQmlLanguageServer
24\brief Sets up a QmlLanguageServer.
25
26This class sets up a QML language server.
27
28Use the following function to send replies:
29
30\code
31std::function<void(const QByteArray &)> sendData
32\endcode
33
34And, feed the data that the function receives to the \c {server()->receive()}
35method.
36
37Call this method only from a single thread, and do not block. To achieve this,
38avoid direct calls, and connect the method as a slot, while reading from another
39thread.
40
41The various tasks of the language server are divided between
42QLanguageServerModule instances. Each instance is responsible for handling a
43certain subset of client requests. For example, one instance handles completion
44requests, another one updates the code in the code model when the client sends a
45new file version, and so on. The QLanguageServerModule instances are
46constructed and registered with QLanguageServer in the constructor of
47this class.
48
49Generally, do all operations in the object thread and always call handlers from
50it. However, the operations can delegate the response to another thread, as the
51response handler is thread safe. All the methods of the \c server() object are
52also thread safe.
53
54The code model starts other threads to update its state. See its documentation
55for more information.
56*/
57QQmlLanguageServer::QQmlLanguageServer(std::function<void(const QByteArray &)> sendData,
58 QQmlToolingSharedSettings *settings)
59 : QLanguageServer(sendData),
60 m_codeModelManager(nullptr, settings),
61 m_textSynchronization(&m_codeModelManager),
62 m_workspace(&m_codeModelManager),
63 m_completionSupport(&m_codeModelManager),
64 m_navigationSupport(&m_codeModelManager),
65 m_definitionSupport(&m_codeModelManager),
66 m_referencesSupport(&m_codeModelManager),
67 m_documentFormatting(&m_codeModelManager),
68 m_renameSupport(&m_codeModelManager),
69 m_rangeFormatting(&m_codeModelManager),
70 m_hover(&m_codeModelManager),
71 m_codeActionSupport(&m_codeModelManager),
72 m_highlightSupport(&m_codeModelManager),
73 m_documentSymbolSupport(&m_codeModelManager),
74 m_progressSupport(&m_codeModelManager),
75 m_lint(this, &m_codeModelManager)
76{
77 QObject::connect(this, &QLanguageServer::lifecycleError, this, &QQmlLanguageServer::errorExit);
78 QObject::connect(this, &QLanguageServer::exit, this, &QQmlLanguageServer::exit);
79
80 registerModule(&m_textSynchronization);
81 registerModule(&m_codeActionSupport);
82 registerModule(&m_workspace);
83 registerModule(&m_completionSupport);
84 registerModule(&m_navigationSupport);
85 registerModule(&m_definitionSupport);
86 registerModule(&m_referencesSupport);
87 registerModule(&m_documentFormatting);
88 registerModule(&m_renameSupport);
89 registerModule(&m_rangeFormatting);
90 registerModule(&m_hover);
91 registerModule(&m_documentSymbolSupport);
92 registerModule(&m_progressSupport);
93 registerModule(&m_highlightSupport);
94
95 qCWarning(lspServerLog) << "Did Setup";
96}
97
99{
100 // note: the server modules might be in use by the QQmlCodeModel thread, so wait for the
101 // QQmlCodeModel threads to finish before destroying the server modules.
102 m_codeModelManager.prepareForShutdown();
103}
104
106{
107 qCWarning(lspServerLog) << "Error exit";
108 fclose(stdin);
109}
110
112{
113 m_returnValue = 0;
114 fclose(stdin);
115}
116
118{
119 return m_returnValue;
120}
121
123{
124 return &m_codeModelManager;
125}
126
127} // namespace QmlLsp
128
129QT_END_NAMESPACE
Sets up a QmlLanguageServer.
QQmlCodeModelManager * codeModelManager()