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(
89 const lspserver::ProgressParams<lspserver::WorkDoneProgressReport> &)>
90 ReportWorkDoneProgress;
91
92 void reportWorkDoneProgress(
93 const lspserver::ProgressParams<lspserver::WorkDoneProgressReport>
94 &Params) {
95 if (ClientCaps.WorkDoneProgress)
96 ReportWorkDoneProgress(Params);
97 }
98
99 llvm::unique_function<void(
100 lspserver::ProgressParams<lspserver::WorkDoneProgressEnd>)>
101 EndWorkDoneProgress;
102
103 void endWorkDoneProgress(
104 const lspserver::ProgressParams<lspserver::WorkDoneProgressEnd> &Params) {
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 // Default constructor is broken in Boost 1.87:
137 // https://github.com/boostorg/asio/commit/30b5974ed34bfa321d268b3135ffaffcb261461a
138 boost::asio::thread_pool Pool{
139 static_cast<size_t>(boost::asio::detail::default_thread_pool_size())};
140
141 /// Action right after a document is added (including updates).
142 void actOnDocumentAdd(lspserver::PathRef File,
143 std::optional<int64_t> Version);
144
145 void removeDocument(lspserver::PathRef File);
146
147 void onInitialize(const lspserver::InitializeParams &Params,
149
150 void onInitialized(const lspserver::InitializedParams &Params);
151
152 bool ReceivedShutdown = false;
153
154 void onShutdown(const lspserver::NoParams &,
156
157 void onDocumentDidOpen(const lspserver::DidOpenTextDocumentParams &Params);
158
159 void
160 onDocumentDidChange(const lspserver::DidChangeTextDocumentParams &Params);
161
162 void onDocumentDidClose(const lspserver::DidCloseTextDocumentParams &Params);
163
164 void
165 onCodeAction(const lspserver::CodeActionParams &Params,
166 lspserver::Callback<std::vector<lspserver::CodeAction>> Reply);
167
168 void onHover(const lspserver::TextDocumentPositionParams &Params,
169 lspserver::Callback<std::optional<lspserver::Hover>> Reply);
170
171 void onDocumentSymbol(
172 const lspserver::DocumentSymbolParams &Params,
173 lspserver::Callback<std::vector<lspserver::DocumentSymbol>> Reply);
174
175 void onSemanticTokens(const lspserver::SemanticTokensParams &Params,
177
178 void
179 onInlayHint(const lspserver::InlayHintsParams &Params,
180 lspserver::Callback<std::vector<lspserver::InlayHint>> Reply);
181
182 void onCompletion(const lspserver::CompletionParams &Params,
184
185 void
186 onCompletionItemResolve(const lspserver::CompletionItem &Params,
188
189 void onDefinition(const lspserver::TextDocumentPositionParams &Params,
191
192 void
193 onReferences(const lspserver::TextDocumentPositionParams &Params,
194 lspserver::Callback<std::vector<lspserver::Location>> Reply);
195
196 void onDocumentHighlight(
197 const lspserver::TextDocumentPositionParams &Params,
198 lspserver::Callback<std::vector<lspserver::DocumentHighlight>> Reply);
199
200 void onDocumentLink(
201 const lspserver::DocumentLinkParams &Params,
202 lspserver::Callback<std::vector<lspserver::DocumentLink>> Reply);
203
204 std::set<nixf::Diagnostic::DiagnosticKind>
205 SuppressedDiagnostics; // GUARDED_BY(SuppressedDiagnosticsLock)
206
207 std::mutex SuppressedDiagnosticsLock;
208
209 /// Update the suppressing set. There might be some invalid names, should be
210 /// logged then.
211 void updateSuppressed(const std::vector<std::string> &Sup);
212
213 /// Determine whether or not this diagnostic is suppressed.
214 bool isSuppressed(nixf::Diagnostic::DiagnosticKind Kind);
215 void publishDiagnostics(lspserver::PathRef File,
216 std::optional<int64_t> Version, std::string_view Src,
217 const std::vector<nixf::Diagnostic> &Diagnostics);
218
219 void onRename(const lspserver::RenameParams &Params,
221
222 void
223 onPrepareRename(const lspserver::TextDocumentPositionParams &Params,
224 lspserver::Callback<std::optional<lspserver::Range>> Reply);
225
226 void onFormat(const lspserver::DocumentFormattingParams &Params,
227 lspserver::Callback<std::vector<lspserver::TextEdit>> Reply);
228
229 //---------------------------------------------------------------------------/
230 // Workspace features
231 //---------------------------------------------------------------------------/
232 void onDidChangeConfiguration(
233 const lspserver::DidChangeConfigurationParams &Params);
234
235public:
236 Controller(std::unique_ptr<lspserver::InboundPort> In,
237 std::unique_ptr<lspserver::OutboundPort> Out);
238
239 ~Controller() override { Pool.join(); }
240
241 bool isReadyToEval() { return Eval && Eval->ready(); }
242};
243
244} // namespace nixd
Declares workspace configuration schema.
AttrSetClient * client()
Check if the process is still alive.
~Controller() override
Definition Controller.h:239
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:39