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 llvm::unique_function<void(
79 BeginWorkDoneProgress;
80
81 void beginWorkDoneProgress(
83 &Params) {
84 if (ClientCaps.WorkDoneProgress)
85 BeginWorkDoneProgress(Params);
86 }
87
88 llvm::unique_function<void(
90 ReportWorkDoneProgress;
91
92 void reportWorkDoneProgress(
94 &Params) {
95 if (ClientCaps.WorkDoneProgress)
96 ReportWorkDoneProgress(Params);
97 }
98
99 llvm::unique_function<void(
101 EndWorkDoneProgress;
102
103 void endWorkDoneProgress(
105 if (ClientCaps.WorkDoneProgress)
106 EndWorkDoneProgress(Params);
107 }
108
109 std::mutex TUsLock;
110 llvm::StringMap<std::shared_ptr<NixTU>> TUs;
111
112 template <class T>
113 std::shared_ptr<NixTU> getTU(std::string File,
114 lspserver::Callback<T> &Reply) {
115 using lspserver::error;
116 std::lock_guard G(TUsLock);
117 if (!TUs.count(File)) [[unlikely]] {
118 Reply(T{}); // Reply a default constructed response.
119 lspserver::elog("cannot get translation unit: {0}", File);
120 return nullptr;
121 }
122 return TUs[File];
123 }
124
125 template <class T>
126 std::shared_ptr<nixf::Node> getAST(const NixTU &TU,
127 lspserver::Callback<T> &Reply) {
128 using lspserver::error;
129 if (!TU.ast()) {
130 Reply(T{});
131 lspserver::elog("AST is null on this unit");
132 return nullptr;
133 }
134 return TU.ast();
135 }
136
137 boost::asio::thread_pool Pool;
138
139 /// Action right after a document is added (including updates).
140 void actOnDocumentAdd(lspserver::PathRef File,
141 std::optional<int64_t> Version);
142
143 void removeDocument(lspserver::PathRef File);
144
145 void onInitialize(const lspserver::InitializeParams &Params,
147
148 void onInitialized(const lspserver::InitializedParams &Params);
149
150 bool ReceivedShutdown = false;
151
152 void onShutdown(const lspserver::NoParams &,
154
155 void onDocumentDidOpen(const lspserver::DidOpenTextDocumentParams &Params);
156
157 void
158 onDocumentDidChange(const lspserver::DidChangeTextDocumentParams &Params);
159
160 void onDocumentDidClose(const lspserver::DidCloseTextDocumentParams &Params);
161
162 void
163 onCodeAction(const lspserver::CodeActionParams &Params,
164 lspserver::Callback<std::vector<lspserver::CodeAction>> Reply);
165
166 void onHover(const lspserver::TextDocumentPositionParams &Params,
167 lspserver::Callback<std::optional<lspserver::Hover>> Reply);
168
169 void onDocumentSymbol(
171 lspserver::Callback<std::vector<lspserver::DocumentSymbol>> Reply);
172
173 void onSemanticTokens(const lspserver::SemanticTokensParams &Params,
175
176 void
177 onInlayHint(const lspserver::InlayHintsParams &Params,
178 lspserver::Callback<std::vector<lspserver::InlayHint>> Reply);
179
180 void onCompletion(const lspserver::CompletionParams &Params,
182
183 void
184 onCompletionItemResolve(const lspserver::CompletionItem &Params,
186
187 void onDefinition(const lspserver::TextDocumentPositionParams &Params,
189
190 void
191 onReferences(const lspserver::TextDocumentPositionParams &Params,
192 lspserver::Callback<std::vector<lspserver::Location>> Reply);
193
194 void onDocumentHighlight(
196 lspserver::Callback<std::vector<lspserver::DocumentHighlight>> Reply);
197
198 void onDocumentLink(
199 const lspserver::DocumentLinkParams &Params,
200 lspserver::Callback<std::vector<lspserver::DocumentLink>> Reply);
201
202 std::set<nixf::Diagnostic::DiagnosticKind>
203 SuppressedDiagnostics; // GUARDED_BY(SuppressedDiagnosticsLock)
204
205 std::mutex SuppressedDiagnosticsLock;
206
207 /// Update the suppressing set. There might be some invalid names, should be
208 /// logged then.
209 void updateSuppressed(const std::vector<std::string> &Sup);
210
211 /// Determine whether or not this diagnostic is suppressed.
212 bool isSuppressed(nixf::Diagnostic::DiagnosticKind Kind);
213 void publishDiagnostics(lspserver::PathRef File,
214 std::optional<int64_t> Version, std::string_view Src,
215 const std::vector<nixf::Diagnostic> &Diagnostics);
216
217 void onRename(const lspserver::RenameParams &Params,
219
220 void onPrepareRename(const lspserver::TextDocumentPositionParams &Params,
222
223 void onFormat(const lspserver::DocumentFormattingParams &Params,
224 lspserver::Callback<std::vector<lspserver::TextEdit>> Reply);
225
226 //---------------------------------------------------------------------------/
227 // Workspace features
228 //---------------------------------------------------------------------------/
229 void onDidChangeConfiguration(
231
232public:
233 Controller(std::unique_ptr<lspserver::InboundPort> In,
234 std::unique_ptr<lspserver::OutboundPort> Out);
235
236 ~Controller() override { Pool.join(); }
237
238 bool isReadyToEval() { return Eval && Eval->ready(); }
239};
240
241} // namespace nixd
Declares workspace configuration schema.
AttrSetClient * client()
Check if the process is still alive.
~Controller() override
Definition Controller.h:236
Controller(std::unique_ptr< lspserver::InboundPort > In, std::unique_ptr< lspserver::OutboundPort > Out)
Definition Support.cpp:68
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:38
Parameters for the document link request.
A parameter literal used in inlay hint requests.
Body of textDocument/semanticTokens/full request.