-
-
Notifications
You must be signed in to change notification settings - Fork 41
nixd: path completion #530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Origami404
wants to merge
5
commits into
nix-community:main
Choose a base branch
from
Origami404:path-completion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 | ||||
---|---|---|---|---|---|---|
|
@@ -12,6 +12,8 @@ | |||||
|
||||||
#include <boost/asio/post.hpp> | ||||||
|
||||||
#include <llvm/Support/FileSystem.h> | ||||||
#include <llvm/Support/Path.h> | ||||||
#include <semaphore> | ||||||
#include <set> | ||||||
#include <utility> | ||||||
|
@@ -250,6 +252,43 @@ class OptionCompletionProvider { | |||||
} | ||||||
}; | ||||||
|
||||||
void completeExprPath(const std::string &CurFilePath, | ||||||
const nixf::ExprPath &ExprPath, | ||||||
std::vector<CompletionItem> &Items) { | ||||||
using namespace llvm::sys; | ||||||
|
||||||
if (!ExprPath.parts().isLiteral()) { | ||||||
return; | ||||||
} | ||||||
|
||||||
const auto &PathLiteral = ExprPath.parts().literal(); | ||||||
if (PathLiteral.empty()) { | ||||||
return; | ||||||
} | ||||||
|
||||||
llvm::SmallVector<char, 32> Path{PathLiteral.begin(), PathLiteral.end()}; | ||||||
if (PathLiteral[0] == '.') { | ||||||
const auto &CurFileDir = path::parent_path(CurFilePath); | ||||||
fs::make_absolute(CurFileDir, Path); | ||||||
} | ||||||
|
||||||
if (fs::exists(Path) && fs::is_directory(Path)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we do this in std::filesystem? |
||||||
std::error_code EC; | ||||||
for (auto Iter = fs::directory_iterator(Path, EC, false); | ||||||
Iter != fs::directory_iterator(); Iter.increment(EC)) { | ||||||
if (EC) { | ||||||
vlog("failed to read directory: {0}", EC.message()); | ||||||
break; | ||||||
} | ||||||
addItem(Items, CompletionItem{ | ||||||
.label = path::filename(Iter->path()).str(), | ||||||
.kind = CompletionItemKind::File, | ||||||
.data = ExprPath.parts().literal(), | ||||||
}); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
void completeAttrName(const std::vector<std::string> &Scope, | ||||||
const std::string &Prefix, | ||||||
Controller::OptionMapTy &Options, bool CompletionSnippets, | ||||||
|
@@ -299,15 +338,24 @@ void Controller::onCompletion(const CompletionParams &Params, | |||||
ClientCaps.CompletionSnippets, List.items); | ||||||
} | ||||||
} else { | ||||||
const VariableLookupAnalysis &VLA = *TU->variableLookup(); | ||||||
VLACompletionProvider VLAP(VLA); | ||||||
VLAP.complete(*Desc, List.items, PM); | ||||||
if (havePackageScope(*Desc, VLA, PM)) { | ||||||
// Append it with nixpkgs completion | ||||||
// FIXME: handle null nixpkgsClient() | ||||||
NixpkgsCompletionProvider NCP(*nixpkgsClient()); | ||||||
auto [Scope, Prefix] = getScopeAndPrefix(*Desc, PM); | ||||||
NCP.completePackages(Scope, Prefix, List.items); | ||||||
const auto *Parent = PM.upExpr(*Desc); | ||||||
// if we are in a literal path, use PathCompletionProvider | ||||||
if (Parent->kind() == Node::NK_ExprPath) { | ||||||
const auto &Path = static_cast<const nixf::ExprPath &>(*Parent); | ||||||
if (Path.parts().isLiteral()) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
remove this curly brace? |
||||||
completeExprPath(File, Path, List.items); | ||||||
} | ||||||
} else { | ||||||
const VariableLookupAnalysis &VLA = *TU->variableLookup(); | ||||||
VLACompletionProvider VLAP(VLA); | ||||||
VLAP.complete(*Desc, List.items, PM); | ||||||
if (havePackageScope(*Desc, VLA, PM)) { | ||||||
// Append it with nixpkgs completion | ||||||
// FIXME: handle null nixpkgsClient() | ||||||
NixpkgsCompletionProvider NCP(*nixpkgsClient()); | ||||||
auto [Scope, Prefix] = getScopeAndPrefix(*Desc, PM); | ||||||
NCP.completePackages(Scope, Prefix, List.items); | ||||||
} | ||||||
} | ||||||
} | ||||||
// Next, add nixpkgs provided names. | ||||||
|
Empty file.
This file contains hidden or 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,79 @@ | ||
# RUN: sed "s|ROOT|%S|g" < %s | nixd --lit-test | 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://ROOT/completion-path-root/main.nix", | ||
"languageId":"nix", | ||
"version":1, | ||
"text":"{ bar = ./ }" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
```json | ||
{ | ||
"jsonrpc": "2.0", | ||
"id": 1, | ||
"method": "textDocument/completion", | ||
"params": { | ||
"textDocument": { | ||
"uri": "file://ROOT/completion-path-root/main.nix" | ||
}, | ||
"position": { | ||
"line": 0, | ||
"character": 9 | ||
}, | ||
"context": { | ||
"triggerKind": 1, | ||
"triggerCharacter": "/" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
``` | ||
CHECK: "id": 1, | ||
CHECK-NEXT: "jsonrpc": "2.0", | ||
CHECK-NEXT: "result": { | ||
CHECK-NEXT: "isIncomplete": false, | ||
CHECK-NEXT: "items": [ | ||
CHECK-NEXT: { | ||
CHECK-NEXT: "data": "./", | ||
CHECK-NEXT: "kind": 17, | ||
CHECK-NEXT: "label": "main.nix", | ||
CHECK-NEXT: "score": 0 | ||
CHECK-NEXT: } | ||
CHECK-NEXT: ] | ||
CHECK-NEXT: } | ||
``` | ||
|
||
|
||
```json | ||
{"jsonrpc":"2.0","method":"exit"} | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.