nixd
Loading...
Searching...
No Matches
Controller.h
Go to the documentation of this file.
1#pragma once
2
3#include "Configuration.h"
4#include "EvalClient.h"
5#include "NixTU.h"
6
12
13#include <boost/asio/thread_pool.hpp>
14
15#include <set>
16
17namespace nixd {
18
20public:
21 using OptionMapTy = std::map<std::string, std::unique_ptr<AttrSetClientProc>>;
22
23private:
24 std::unique_ptr<OwnedEvalClient> Eval;
25
26 // Use this worker for evaluating nixpkgs.
27 std::unique_ptr<AttrSetClientProc> NixpkgsEval;
28
29 std::mutex OptionsLock;
30 // Map of option providers.
31 //
32 // e.g. "nixos" -> nixos worker
33 // "home-manager" -> home-manager worker
34 OptionMapTy Options; // GUARDED_BY(OptionsLock)
35
36 AttrSetClientProc &nixpkgsEval() {
37 assert(NixpkgsEval);
38 return *NixpkgsEval;
39 }
40
41 AttrSetClient *nixpkgsClient() { return nixpkgsEval().client(); }
42
43 void evalExprWithProgress(AttrSetClient &Client, const EvalExprParams &Params,
44 std::string_view Description);
45
47
49
50 std::mutex ConfigLock;
51 Configuration Config; // GUARDED_BY(ConfigLock)
52
53 llvm::unique_function<void(const lspserver::ConfigurationParams &,
55 WorkspaceConfiguration;
56
57 void workspaceConfiguration(const lspserver::ConfigurationParams &Params,
59
60 /// \brief Update the configuration, do necessary adjusting for updates.
61 ///
62 /// \example If asked to change eval settings, send eval requests to workers.
63 void updateConfig(Configuration NewConfig);
64
65 /// \brief Get configuration from LSP client. Update the config.
66 void fetchConfig();
67
68 llvm::unique_function<void(const lspserver::PublishDiagnosticsParams &)>
69 PublishDiagnostic;
70 llvm::unique_function<void(const lspserver::WorkDoneProgressCreateParams &,
72 CreateWorkDoneProgress;
73
74 void
75 createWorkDoneProgress(const lspserver::WorkDoneProgressCreateParams &Params);
76
77 /// Request the client to show a document (file or URL).
78 /// @since LSP 3.16.0
79 llvm::unique_function<void(
82 ShowDocument;
83
84 llvm::unique_function<void(
86 BeginWorkDoneProgress;
87
88 void beginWorkDoneProgress(
90 &Params) {
91 if (ClientCaps.WorkDoneProgress)
92 BeginWorkDoneProgress(Params);
93 }
94
95 llvm::unique_function<void(
96 const lspserver::ProgressParams<lspserver::WorkDoneProgressReport> &)>
97 ReportWorkDoneProgress;
98
99 void reportWorkDoneProgress(
100 const lspserver::ProgressParams<lspserver::WorkDoneProgressReport>
101 &Params) {
102 if (ClientCaps.WorkDoneProgress)
103 ReportWorkDoneProgress(Params);
104 }
105
106 llvm::unique_function<void(
107 lspserver::ProgressParams<lspserver::WorkDoneProgressEnd>)>
108 EndWorkDoneProgress;
109
110 void endWorkDoneProgress(
111 const lspserver::ProgressParams<lspserver::WorkDoneProgressEnd> &Params) {
112 if (ClientCaps.WorkDoneProgress)
113 EndWorkDoneProgress(Params);
114 }
115
116 mutable std::mutex TUsLock;
117 llvm::StringMap<std::shared_ptr<NixTU>> TUs;
118
119 std::shared_ptr<const NixTU> getTU(std::string_view File) const {
120 using lspserver::error;
121 std::lock_guard G(TUsLock);
122 if (!TUs.count(File)) [[unlikely]] {
123 lspserver::elog("cannot get translation unit: {0}", File);
124 return nullptr;
125 }
126 return TUs.lookup(File);
127 }
128
129 static std::shared_ptr<nixf::Node> getAST(const NixTU &TU) {
130 using lspserver::error;
131 if (!TU.ast()) {
132 lspserver::elog("AST is null on this unit");
133 return nullptr;
134 }
135 return TU.ast();
136 }
137
138 std::shared_ptr<const nixf::Node> getAST(std::string_view File) const {
139 auto TU = getTU(File);
140 return TU ? getAST(*TU) : nullptr;
141 }
142
143 // Default constructor is broken in Boost 1.87:
144 // https://github.com/boostorg/asio/commit/30b5974ed34bfa321d268b3135ffaffcb261461a
145 boost::asio::thread_pool Pool{
146 static_cast<size_t>(boost::asio::detail::default_thread_pool_size())};
147
148 /// Action right after a document is added (including updates).
149 void actOnDocumentAdd(lspserver::PathRef File,
150 std::optional<int64_t> Version);
151
152 void removeDocument(lspserver::PathRef File);
153
154 void onInitialize(const lspserver::InitializeParams &Params,
156
157 void onInitialized(const lspserver::InitializedParams &Params);
158
159 bool ReceivedShutdown = false;
160
161 void onShutdown(const lspserver::NoParams &,
163
164 void onDocumentDidOpen(const lspserver::DidOpenTextDocumentParams &Params);
165
166 void
167 onDocumentDidChange(const lspserver::DidChangeTextDocumentParams &Params);
168
169 void onDocumentDidClose(const lspserver::DidCloseTextDocumentParams &Params);
170
171 void
172 onCodeAction(const lspserver::CodeActionParams &Params,
173 lspserver::Callback<std::vector<lspserver::CodeAction>> Reply);
174
175 void onCodeActionResolve(const lspserver::CodeAction &Params,
177
178 void onHover(const lspserver::TextDocumentPositionParams &Params,
179 lspserver::Callback<std::optional<lspserver::Hover>> Reply);
180
181 void onDocumentSymbol(
182 const lspserver::DocumentSymbolParams &Params,
183 lspserver::Callback<std::vector<lspserver::DocumentSymbol>> Reply);
184
185 void onFoldingRange(
186 const lspserver::FoldingRangeParams &Params,
187 lspserver::Callback<std::vector<lspserver::FoldingRange>> Reply);
188
189 void onSemanticTokens(const lspserver::SemanticTokensParams &Params,
191
192 void
193 onInlayHint(const lspserver::InlayHintsParams &Params,
194 lspserver::Callback<std::vector<lspserver::InlayHint>> Reply);
195
196 void onCompletion(const lspserver::CompletionParams &Params,
198
199 void
200 onCompletionItemResolve(const lspserver::CompletionItem &Params,
202
203 void onDefinition(const lspserver::TextDocumentPositionParams &Params,
205
206 void
207 onReferences(const lspserver::TextDocumentPositionParams &Params,
208 lspserver::Callback<std::vector<lspserver::Location>> Reply);
209
210 void onDocumentHighlight(
211 const lspserver::TextDocumentPositionParams &Params,
212 lspserver::Callback<std::vector<lspserver::DocumentHighlight>> Reply);
213
214 void onDocumentLink(
215 const lspserver::DocumentLinkParams &Params,
216 lspserver::Callback<std::vector<lspserver::DocumentLink>> Reply);
217
218 std::set<nixf::Diagnostic::DiagnosticKind>
219 SuppressedDiagnostics; // GUARDED_BY(SuppressedDiagnosticsLock)
220
221 std::mutex SuppressedDiagnosticsLock;
222
223 /// Update the suppressing set. There might be some invalid names, should be
224 /// logged then.
225 void updateSuppressed(const std::vector<std::string> &Sup);
226
227 /// Determine whether or not this diagnostic is suppressed.
228 bool isSuppressed(nixf::Diagnostic::DiagnosticKind Kind);
229 void publishDiagnostics(lspserver::PathRef File,
230 std::optional<int64_t> Version, std::string_view Src,
231 const std::vector<nixf::Diagnostic> &Diagnostics);
232
233 void onRename(const lspserver::RenameParams &Params,
235
236 void
237 onPrepareRename(const lspserver::TextDocumentPositionParams &Params,
238 lspserver::Callback<std::optional<lspserver::Range>> Reply);
239
240 void onFormat(const lspserver::DocumentFormattingParams &Params,
241 lspserver::Callback<std::vector<lspserver::TextEdit>> Reply);
242
243 //---------------------------------------------------------------------------/
244 // Workspace features
245 //---------------------------------------------------------------------------/
246 void onDidChangeConfiguration(
247 const lspserver::DidChangeConfigurationParams &Params);
248
249public:
250 Controller(std::unique_ptr<lspserver::InboundPort> In,
251 std::unique_ptr<lspserver::OutboundPort> Out);
252
253 ~Controller() override { Pool.join(); }
254
255 bool isReadyToEval() { return Eval && Eval->ready(); }
256};
257
258} // namespace nixd
Declares workspace configuration schema.
AttrSetClient * client()
Check if the process is still alive.
~Controller() override
Definition Controller.h:253
Controller(std::unique_ptr< lspserver::InboundPort > In, std::unique_ptr< lspserver::OutboundPort > Out)
Definition Support.cpp:69
std::map< std::string, std::unique_ptr< AttrSetClientProc > > OptionMapTy
Definition Controller.h:21
llvm::unique_function< void(llvm::Expected< T >)> Callback
Definition Function.h:14
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&...Vals)
Definition Logger.h:70
void elog(const char *Fmt, Ts &&...Vals)
Definition Logger.h:52
llvm::StringRef PathRef
Definition Path.h:27
std::string EvalExprParams
Definition AttrSet.h:39