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_highlightSupport(&m_codeModelManager),
72 m_documentSymbolSupport(&m_codeModelManager),
73 m_progressSupport(&m_codeModelManager),
74 m_lint(this, &m_codeModelManager)
75{
76 QObject::connect(this, &QLanguageServer::lifecycleError, this, &QQmlLanguageServer::errorExit);
77 QObject::connect(this, &QLanguageServer::exit, this, &QQmlLanguageServer::exit);
78
79 registerModule(&m_textSynchronization);
80 registerModule(&m_lint);
81 registerModule(&m_workspace);
82 registerModule(&m_completionSupport);
83 registerModule(&m_navigationSupport);
84 registerModule(&m_definitionSupport);
85 registerModule(&m_referencesSupport);
86 registerModule(&m_documentFormatting);
87 registerModule(&m_renameSupport);
88 registerModule(&m_rangeFormatting);
89 registerModule(&m_hover);
90 registerModule(&m_documentSymbolSupport);
91 registerModule(&m_progressSupport);
92 registerModule(&m_highlightSupport);
93
94 qCWarning(lspServerLog) << "Did Setup";
95}
96
98{
99 // note: the server modules might be in use by the QQmlCodeModel thread, so wait for the
100 // QQmlCodeModel threads to finish before destroying the server modules.
101 m_codeModelManager.prepareForShutdown();
102}
103
105{
106 qCWarning(lspServerLog) << "Error exit";
107 fclose(stdin);
108}
109
111{
112 m_returnValue = 0;
113 fclose(stdin);
114}
115
117{
118 return m_returnValue;
119}
120
122{
123 return &m_codeModelManager;
124}
125
126} // namespace QmlLsp
127
128QT_END_NAMESPACE
Sets up a QmlLanguageServer.
QQmlCodeModelManager * codeModelManager()