-
-
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.
- Loading branch information
Showing
13 changed files
with
590 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#include "nixf/Sema/ParentMap.h" | ||
#include "nixf/Sema/VariableLookup.h" | ||
|
||
#include <llvm/Support/Error.h> | ||
|
||
namespace nixd { | ||
|
||
/// \brief Heuristically find definition on some node | ||
llvm::Expected<const nixf::Definition &> | ||
findDefinition(const nixf::Node &N, const nixf::ParentMapAnalysis &PMA, | ||
const nixf::VariableLookupAnalysis &VLA); | ||
|
||
} // namespace nixd |
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,71 @@ | ||
/// \file | ||
/// \brief This implements [Find References]. | ||
/// [Find References]: | ||
/// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_references | ||
|
||
#include "Convert.h" | ||
#include "Definition.h" | ||
|
||
#include "nixd/Controller/Controller.h" | ||
|
||
#include <boost/asio/post.hpp> | ||
#include <lspserver/Protocol.h> | ||
#include <nixf/Sema/ParentMap.h> | ||
#include <nixf/Sema/VariableLookup.h> | ||
|
||
using namespace lspserver; | ||
using namespace nixd; | ||
using namespace llvm; | ||
using namespace nixf; | ||
|
||
namespace { | ||
|
||
Error findReferences(const nixf::Node &Desc, const ParentMapAnalysis &PMA, | ||
const VariableLookupAnalysis &VLA, const URIForFile &URI, | ||
std::vector<Location> &Locations) { | ||
|
||
// Two steps. | ||
// 1. Find some "definition" for this node. | ||
// 2. Find all "uses", and construct the vector. | ||
|
||
// Find "definition" | ||
if (auto Def = findDefinition(Desc, PMA, VLA)) { | ||
// OK, iterate all uses. | ||
for (const auto *Use : Def->uses()) { | ||
assert(Use); | ||
Locations.emplace_back(Location{ | ||
.uri = URI, | ||
.range = toLSPRange(Use->range()), | ||
}); | ||
} | ||
return Error::success(); | ||
} | ||
return error("Cannot find definition of this node"); | ||
} | ||
|
||
} // namespace | ||
|
||
void Controller::onReferences(const TextDocumentPositionParams &Params, | ||
Callback<std::vector<Location>> 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; | ||
} | ||
std::vector<Location> Locations; | ||
if (auto Err = findReferences(*Desc, *TU->parentMap(), | ||
*TU->variableLookup(), URI, Locations)) { | ||
Reply(std::move(Err)); | ||
return; | ||
} | ||
Reply(std::move(Locations)); | ||
} | ||
} | ||
}; | ||
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
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,93 @@ | ||
# RUN: nixd --lit-test < %s | FileCheck %s | ||
|
||
<-- 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":"arg @ { foo, bar }: arg + foo + bar + arg" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
<-- textDocument/references(2) | ||
|
||
|
||
```json | ||
{ | ||
"jsonrpc":"2.0", | ||
"id":2, | ||
"method":"textDocument/references", | ||
"params":{ | ||
"textDocument":{ | ||
"uri":"file:///basic.nix" | ||
}, | ||
"position":{ | ||
"line": 0, | ||
"character":0 | ||
} | ||
} | ||
} | ||
``` | ||
|
||
``` | ||
CHECK: "id": 2, | ||
CHECK-NEXT: "jsonrpc": "2.0", | ||
CHECK-NEXT: "result": [ | ||
CHECK-NEXT: { | ||
CHECK-NEXT: "range": { | ||
CHECK-NEXT: "end": { | ||
CHECK-NEXT: "character": 23, | ||
CHECK-NEXT: "line": 0 | ||
CHECK-NEXT: }, | ||
CHECK-NEXT: "start": { | ||
CHECK-NEXT: "character": 20, | ||
CHECK-NEXT: "line": 0 | ||
CHECK-NEXT: } | ||
CHECK-NEXT: }, | ||
CHECK-NEXT: "uri": "file:///basic.nix" | ||
CHECK-NEXT: }, | ||
CHECK-NEXT: { | ||
CHECK-NEXT: "range": { | ||
CHECK-NEXT: "end": { | ||
CHECK-NEXT: "character": 41, | ||
CHECK-NEXT: "line": 0 | ||
CHECK-NEXT: }, | ||
CHECK-NEXT: "start": { | ||
CHECK-NEXT: "character": 38, | ||
CHECK-NEXT: "line": 0 | ||
CHECK-NEXT: } | ||
CHECK-NEXT: }, | ||
CHECK-NEXT: "uri": "file:///basic.nix" | ||
CHECK-NEXT: } | ||
CHECK-NEXT: ] | ||
``` | ||
|
||
```json | ||
{"jsonrpc":"2.0","method":"exit"} | ||
``` |
Oops, something went wrong.