Skip to content
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

nixd: path completion #530

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions libnixf/src/Parse/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ bool Lexer::consumePathStart() {
// And also check if it contains a slash.
LexerCursor Saved = cur();

bool IsStartedWithDot = peek() == '.';

// {PATH_CHAR}*
consumeManyPathChar();

Expand All @@ -233,6 +235,10 @@ bool Lexer::consumePathStart() {
// This should be parsed as path-interpolation.
if (peekPrefix("${"))
return true;
// Or, look back to see if is a './'.
// This should be parsed as path
if (IsStartedWithDot)
return true;
}

// Otherwise, it is not a path, restore cursor.
Expand Down
14 changes: 14 additions & 0 deletions libnixf/test/Parse/Lexer.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <gtest/gtest.h>

#include "Lexer.h"
#include "Token.h"
Origami404 marked this conversation as resolved.
Show resolved Hide resolved

#include "nixf/Basic/Diagnostic.h"
#include "nixf/Basic/TokenKinds.h"

#include <cstddef>

Expand Down Expand Up @@ -173,6 +175,18 @@ TEST_F(LexerTest, lexIDPath) {
ASSERT_EQ(Tokens.size(), sizeof(Match) / sizeof(TokenKind));
}

TEST_F(LexerTest, lexPathStart) {
Lexer Lexer(R"(./)", Diags);
const TokenKind Match[] = {
tok_path_fragment,
};
auto Tokens = collect(Lexer, &Lexer::lex);
for (size_t I = 0; I < sizeof(Match) / sizeof(TokenKind); I++) {
ASSERT_EQ(Tokens[I].kind(), Match[I]);
}
ASSERT_EQ(Tokens.size(), sizeof(Match) / sizeof(TokenKind));
}

TEST_F(LexerTest, lexKW) {
// FIXME: test pp//a to see that we can lex this as Update(pp, a)
Lexer Lexer(R"(if then)", Diags);
Expand Down
66 changes: 57 additions & 9 deletions nixd/lib/Controller/Completion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The 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,
Expand Down Expand Up @@ -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()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (Path.parts().isLiteral()) {
if (Path.parts().isLiteral())

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.
Expand Down
Empty file.
79 changes: 79 additions & 0 deletions nixd/tools/nixd/test/completion-path.md
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"}
```
Loading