-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes: #255
- Loading branch information
Showing
11 changed files
with
390 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/// \file | ||
/// \brief This implements [Rename]. | ||
/// [Rename]: | ||
/// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_rename | ||
|
||
#include "Convert.h" | ||
#include "Definition.h" | ||
|
||
#include "lspserver/Protocol.h" | ||
#include "nixd/Controller/Controller.h" | ||
|
||
#include <boost/asio/post.hpp> | ||
|
||
using namespace lspserver; | ||
using namespace nixd; | ||
using namespace llvm; | ||
using namespace nixf; | ||
|
||
namespace { | ||
llvm::Expected<WorkspaceEdit> rename(const nixf::Node &Desc, | ||
const std::string &NewText, | ||
const ParentMapAnalysis &PMA, | ||
const VariableLookupAnalysis &VLA, | ||
const URIForFile &URI) { | ||
using lspserver::TextEdit; | ||
// Find "definition" | ||
auto Def = findDefinition(Desc, PMA, VLA); | ||
if (!Def) | ||
return Def.takeError(); | ||
|
||
std::vector<TextEdit> Edits; | ||
|
||
for (const auto *Use : Def->uses()) { | ||
Edits.emplace_back(TextEdit{ | ||
.range = toLSPRange(Use->range()), | ||
.newText = NewText, | ||
}); | ||
} | ||
|
||
Edits.emplace_back(TextEdit{ | ||
.range = toLSPRange(Def->syntax()->range()), | ||
.newText = NewText, | ||
}); | ||
WorkspaceEdit WE; | ||
WE.changes = std::map<std::string, std::vector<TextEdit>>{ | ||
{URI.uri(), std::move(Edits)}}; | ||
return WE; | ||
} | ||
} // namespace | ||
|
||
void Controller::onRename(const RenameParams &Params, | ||
Callback<WorkspaceEdit> Reply) { | ||
auto Action = [Reply = std::move(Reply), URI = Params.textDocument.uri, | ||
Pos = toNixfPosition(Params.position), | ||
NewText = Params.newName, this]() mutable { | ||
std::string File(URI.file()); | ||
if (std::shared_ptr<NixTU> TU = getTU(File, Reply)) [[likely]] { | ||
if (std::shared_ptr<nixf::Node> AST = getAST(*TU, Reply)) [[likely]] { | ||
const nixf::Node *Desc = AST->descend({Pos, Pos}); | ||
if (!Desc) { | ||
Reply(error("cannot find corresponding node on given position")); | ||
return; | ||
} | ||
Reply(rename(*Desc, NewText, *TU->parentMap(), *TU->variableLookup(), | ||
URI)); | ||
return; | ||
} | ||
} | ||
}; | ||
boost::asio::post(Pool, std::move(Action)); | ||
} | ||
|
||
void Controller::onPrepareRename( | ||
const lspserver::TextDocumentPositionParams &Params, | ||
Callback<lspserver::Range> Reply) { | ||
auto Action = [Reply = std::move(Reply), URI = Params.textDocument.uri, | ||
Pos = toNixfPosition(Params.position), this]() mutable { | ||
std::string File(URI.file()); | ||
if (std::shared_ptr<NixTU> TU = getTU(File, Reply)) [[likely]] { | ||
if (std::shared_ptr<nixf::Node> AST = getAST(*TU, Reply)) [[likely]] { | ||
const nixf::Node *Desc = AST->descend({Pos, Pos}); | ||
if (!Desc) { | ||
Reply(error("cannot find corresponding node on given position")); | ||
return; | ||
} | ||
llvm::Expected<WorkspaceEdit> Exp = | ||
rename(*Desc, "", *TU->parentMap(), *TU->variableLookup(), URI); | ||
if (Exp) { | ||
Reply(toLSPRange(Desc->range())); | ||
return; | ||
} | ||
Reply(Exp.takeError()); | ||
return; | ||
} | ||
} | ||
}; | ||
boost::asio::post(Pool, std::move(Action)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# RUN: nixd --lit-test < %s | FileCheck %s | ||
|
||
// https://github.com/nix-community/nixd/issues/255 | ||
|
||
<-- initialize(0) | ||
|
||
```json | ||
{ | ||
"jsonrpc":"2.0", | ||
"id":0, | ||
"method":"initialize", | ||
"params":{ | ||
"processId":123, | ||
"rootPath":"", | ||
"capabilities":{ | ||
}, | ||
"trace":"off" | ||
} | ||
} | ||
``` | ||
|
||
|
||
<-- textDocument/didOpen | ||
|
||
```json | ||
{ | ||
"jsonrpc":"2.0", | ||
"method":"textDocument/didOpen", | ||
"params":{ | ||
"textDocument":{ | ||
"uri":"file:///basic.nix", | ||
"languageId":"nix", | ||
"version":1, | ||
"text":"rec { bar = 1; a = 1; a = bar; b = a; }" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
<-- textDocument/rename(2) | ||
|
||
|
||
```json | ||
{ | ||
"jsonrpc":"2.0", | ||
"id":2, | ||
"method":"textDocument/rename", | ||
"params":{ | ||
"textDocument":{ | ||
"uri":"file:///basic.nix" | ||
}, | ||
"position":{ | ||
"line": 0, | ||
"character":28 | ||
}, | ||
"newName": "b" | ||
} | ||
} | ||
``` | ||
|
||
``` | ||
CHECK: "error": { | ||
CHECK-NEXT: "code": -32001, | ||
CHECK-NEXT: "message": "this varaible is not used in var lookup (duplicated attr?)" | ||
CHECK-NEXT: }, | ||
CHECK-NEXT: "id": 2, | ||
CHECK-NEXT: "jsonrpc": "2.0" | ||
``` | ||
|
||
```json | ||
{"jsonrpc":"2.0","method":"exit"} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# RUN: nixd --lit-test < %s | FileCheck %s | ||
|
||
// https://github.com/nix-community/nixd/issues/255 | ||
|
||
<-- initialize(0) | ||
|
||
```json | ||
{ | ||
"jsonrpc":"2.0", | ||
"id":0, | ||
"method":"initialize", | ||
"params":{ | ||
"processId":123, | ||
"rootPath":"", | ||
"capabilities":{ | ||
}, | ||
"trace":"off" | ||
} | ||
} | ||
``` | ||
|
||
|
||
<-- textDocument/didOpen | ||
|
||
```json | ||
{ | ||
"jsonrpc":"2.0", | ||
"method":"textDocument/didOpen", | ||
"params":{ | ||
"textDocument":{ | ||
"uri":"file:///basic.nix", | ||
"languageId":"nix", | ||
"version":1, | ||
"text":"{a ? 1}: { x = a; }" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
<-- textDocument/rename(2) | ||
|
||
|
||
```json | ||
{ | ||
"jsonrpc":"2.0", | ||
"id":2, | ||
"method":"textDocument/rename", | ||
"params":{ | ||
"textDocument":{ | ||
"uri":"file:///basic.nix" | ||
}, | ||
"position":{ | ||
"line": 0, | ||
"character":1 | ||
}, | ||
"newName": "b" | ||
} | ||
} | ||
``` | ||
|
||
``` | ||
CHECK: "id": 2, | ||
CHECK-NEXT: "jsonrpc": "2.0", | ||
CHECK-NEXT: "result": { | ||
CHECK-NEXT: "changes": { | ||
CHECK-NEXT: "file:///basic.nix": [ | ||
CHECK-NEXT: { | ||
CHECK-NEXT: "newText": "b", | ||
CHECK-NEXT: "range": { | ||
CHECK-NEXT: "end": { | ||
CHECK-NEXT: "character": 16, | ||
CHECK-NEXT: "line": 0 | ||
CHECK-NEXT: }, | ||
CHECK-NEXT: "start": { | ||
CHECK-NEXT: "character": 15, | ||
CHECK-NEXT: "line": 0 | ||
CHECK-NEXT: } | ||
CHECK-NEXT: } | ||
CHECK-NEXT: }, | ||
CHECK-NEXT: { | ||
CHECK-NEXT: "newText": "b", | ||
CHECK-NEXT: "range": { | ||
CHECK-NEXT: "end": { | ||
CHECK-NEXT: "character": 2, | ||
CHECK-NEXT: "line": 0 | ||
CHECK-NEXT: }, | ||
CHECK-NEXT: "start": { | ||
CHECK-NEXT: "character": 1, | ||
CHECK-NEXT: "line": 0 | ||
CHECK-NEXT: } | ||
CHECK-NEXT: } | ||
CHECK-NEXT: } | ||
``` | ||
|
||
```json | ||
{"jsonrpc":"2.0","method":"exit"} | ||
``` |
Oops, something went wrong.