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 mutable std::mutex TUsLock;
110 llvm::StringMap<std::shared_ptr<NixTU>> TUs;
111
112 std::shared_ptr<const NixTU> getTU(std::string_view File) const {
113 using lspserver::error;
114 std::lock_guard G(TUsLock);
115 if (!TUs.count(File)) [[unlikely]] {
116 lspserver::elog("cannot get translation unit: {0}", File);
117 return nullptr;
118 }
119 return TUs.lookup(File);
120 }
121
122 static std::shared_ptr<nixf::Node> getAST(const NixTU &TU) {
123 using lspserver::error;
124 if (!TU.ast()) {
125 lspserver::elog("AST is null on this unit");
126 return nullptr;
127 }
128 return TU.ast();
129 }
130
131 std::shared_ptr<const nixf::Node> getAST(std::string_view File) const {
132 auto TU = getTU(File);
133 return TU ? getAST(*TU) : nullptr;
134 }
135
136 boost::asio::thread_pool Pool;
137
138 /// Action right after a document is added (including updates).
139 void actOnDocumentAdd(lspserver::PathRef File,
140 std::optional<int64_t> Version);
141
142 void removeDocument(lspserver::PathRef File);
143
144 void onInitialize(const lspserver::InitializeParams &Params,
146
147 void onInitialized(const lspserver::InitializedParams &Params);
148
149 bool ReceivedShutdown = false;
150
151 void onShutdown(const lspserver::NoParams &,
153
154 void onDocumentDidOpen(const lspserver::DidOpenTextDocumentParams &Params);
155
156 void
157 onDocumentDidChange(const lspserver::DidChangeTextDocumentParams &Params);
158
159 void onDocumentDidClose(const lspserver::DidCloseTextDocumentParams &Params);
160
161 void
162 onCodeAction(const lspserver::CodeActionParams &Params,
163 lspserver::Callback<std::vector<lspserver::CodeAction>> Reply);
164
165 void onHover(const lspserver::TextDocumentPositionParams &Params,
166 lspserver::Callback<std::optional<lspserver::Hover>> Reply);
167
168 void onDocumentSymbol(
170 lspserver::Callback<std::vector<lspserver::DocumentSymbol>> Reply);
171
172 void onSemanticTokens(const lspserver::SemanticTokensParams &Params,
174
175 void
176 onInlayHint(const lspserver::InlayHintsParams &Params,
177 lspserver::Callback<std::vector<lspserver::InlayHint>> Reply);
178
179 void onCompletion(const lspserver::CompletionParams &Params,
181
182 void
183 onCompletionItemResolve(const lspserver::CompletionItem &Params,
185
186 void onDefinition(const lspserver::TextDocumentPositionParams &Params,
188
189 void
190 onReferences(const lspserver::TextDocumentPositionParams &Params,
191 lspserver::Callback<std::vector<lspserver::Location>> Reply);
192
193 void onDocumentHighlight(
195 lspserver::Callback<std::vector<lspserver::DocumentHighlight>> Reply);
196
197 void onDocumentLink(
198 const lspserver::DocumentLinkParams &Params,
199 lspserver::Callback<std::vector<lspserver::DocumentLink>> Reply);
200
201 std::set<nixf::Diagnostic::DiagnosticKind>
202 SuppressedDiagnostics; // GUARDED_BY(SuppressedDiagnosticsLock)
203
204 std::mutex SuppressedDiagnosticsLock;
205
206 /// Update the suppressing set. There might be some invalid names, should be
207 /// logged then.
208 void updateSuppressed(const std::vector<std::string> &Sup);
209
210 /// Determine whether or not this diagnostic is suppressed.
211 bool isSuppressed(nixf::Diagnostic::DiagnosticKind Kind);
212 void publishDiagnostics(lspserver::PathRef File,
213 std::optional<int64_t> Version, std::string_view Src,
214 const std::vector<nixf::Diagnostic> &Diagnostics);
215
216 void onRename(const lspserver::RenameParams &Params,
218
219 void
220 onPrepareRename(const lspserver::TextDocumentPositionParams &Params,
221 lspserver::Callback<std::optional<lspserver::Range>> Reply);
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.