nixd
Loading...
Searching...
No Matches
CodeAction.cpp
Go to the documentation of this file.
1/// \file
2/// \brief Implementation of [Code Action].
3/// [Code Action]:
4/// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_codeAction
5
6#include "Convert.h"
7
9
10#include <boost/asio/post.hpp>
11
12namespace nixd {
13
14using namespace llvm::json;
15using namespace lspserver;
16
17void Controller::onCodeAction(const lspserver::CodeActionParams &Params,
18 Callback<std::vector<CodeAction>> Reply) {
19 std::string File(Params.textDocument.uri.file());
20 Range Range = Params.range;
21 auto Action = [Reply = std::move(Reply), File, Range, this]() mutable {
22 if (auto TU = getTU(File, Reply)) {
23 std::vector<nixf::Diagnostic> Diagnostics = TU->diagnostics();
24 std::vector<CodeAction> Actions;
25 Actions.reserve(Diagnostics.size());
26 for (const nixf::Diagnostic &D : Diagnostics) {
27 auto DRange = toLSPRange(TU->src(), D.range());
28 if (!Range.overlap(DRange))
29 continue;
30
31 // Add fixes.
32 for (const nixf::Fix &F : D.fixes()) {
33 std::vector<TextEdit> Edits;
34 Edits.reserve(F.edits().size());
35 for (const nixf::TextEdit &TE : F.edits()) {
36 Edits.emplace_back(TextEdit{
37 .range = toLSPRange(TU->src(), TE.oldRange()),
38 .newText = std::string(TE.newText()),
39 });
40 }
41 using Changes = std::map<std::string, std::vector<TextEdit>>;
42 std::string FileURI = URIForFile::canonicalize(File, File).uri();
43 WorkspaceEdit WE{.changes = Changes{
44 {std::move(FileURI), std::move(Edits)},
45 }};
46 Actions.emplace_back(CodeAction{
47 .title = F.message(),
48 .kind = std::string(CodeAction::QUICKFIX_KIND),
49 .edit = std::move(WE),
50 });
51 }
52 }
53 Reply(std::move(Actions));
54 }
55 };
56 boost::asio::post(Pool, std::move(Action));
57}
58
59} // namespace nixd
Convert between LSP and nixf types.
Whether current platform treats paths case insensitively.
Definition Connection.h:11
llvm::unique_function< void(llvm::Expected< T >)> Callback
Definition Function.h:14
lspserver::Range toLSPRange(llvm::StringRef Code, const nixf::LexerCursorRange &R)
Definition Convert.cpp:40
Range range
The range for which the command was invoked.
TextDocumentIdentifier textDocument
The document in which the command was invoked.
std::string title
A short, human-readable, title for this code action.
static const llvm::StringLiteral QUICKFIX_KIND
bool overlap(const Range &RHS) const
static URIForFile canonicalize(llvm::StringRef AbsPath, llvm::StringRef TUPath)
llvm::StringRef file() const
Retrieves absolute path to the file.
std::optional< std::map< std::string, std::vector< TextEdit > > > changes
Holds changes to existing resources.