13#include <boost/asio/post.hpp>
24struct RenameException : std::exception {};
26struct RenameWithException : RenameException {
27 [[nodiscard]]
const char *what() const noexcept
override {
28 return "cannot rename `with` defined variable";
32struct RenameBuiltinException : RenameException {
33 [[nodiscard]]
const char *what() const noexcept
override {
34 return "cannot rename builtin variable";
41 llvm::StringRef Src) {
47 throw RenameWithException();
50 throw RenameBuiltinException();
52 std::vector<TextEdit> Edits;
54 for (
const auto *Use : Def.uses()) {
62 .range =
toLSPRange(Src, Def.syntax()->range()),
66 WE.
changes = std::map<std::string, std::vector<TextEdit>>{
67 {
URI.uri(), std::move(Edits)}};
77 NewText = Params.
newName,
this]()
mutable {
79 return Reply([&]() -> llvm::Expected<WorkspaceEdit> {
83 AST->descend({Pos, Pos}),
84 error(
"cannot find corresponding node on given position"));
86 const auto &PM = *TU->parentMap();
87 const auto &VLA = *TU->variableLookup();
89 return rename(Desc, NewText, PM, VLA,
URI, TU->src());
90 }
catch (std::exception &E) {
91 return error(E.what());
95 boost::asio::post(Pool, std::move(Action));
98void Controller::onPrepareRename(
100 Callback<std::optional<lspserver::Range>> Reply) {
101 using CheckTy = std::optional<lspserver::Range>;
105 return Reply([&]() -> llvm::Expected<CheckTy> {
109 const auto &Desc = *
CheckDefault(AST->descend({Pos, Pos}));
111 const auto &PM = *TU->parentMap();
112 const auto &VLA = *TU->variableLookup();
116 }
catch (std::exception &E) {
117 return error(E.what());
121 boost::asio::post(Pool, std::move(Action));
#define CheckDefault(x)
Variant of CheckReturn, but returns default constructed CheckTy
#define CheckReturn(x, Ret)
Used for simplify early-returns.
Convert between LSP and nixf types.
@ DS_With
From with <expr>;.
LexerCursorRange range() const
Whether current platform treats paths case insensitively.
llvm::unique_function< void(llvm::Expected< T >)> Callback
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&...Vals)
nixf::Position toNixfPosition(const lspserver::Position &P)
const nixf::Definition & findDefinition(const nixf::Node &N, const nixf::ParentMapAnalysis &PMA, const nixf::VariableLookupAnalysis &VLA)
Heuristically find definition on some node.
lspserver::Range toLSPRange(llvm::StringRef Code, const nixf::LexerCursorRange &R)
std::string newName
The new name of the symbol.
Position position
The position at which this request was sent.
TextDocumentIdentifier textDocument
The document that was opened.
URIForFile uri
The text document's URI.
Position position
The position inside the text document.
TextDocumentIdentifier textDocument
The text document.
std::optional< std::map< std::string, std::vector< TextEdit > > > changes
Holds changes to existing resources.