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/Controller: allow setting initial configuration via CLI #579

Merged
merged 6 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions nixd/include/nixd/CommandLine/Configuration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// \file
/// \brief Allow default configuration being passed via CLI

#include "nixd/Controller/Configuration.h"

namespace nixd {

/// \brief Parse the CLI flag and initialize the config nixd::DefaultConfig
nixd::Configuration parseCLIConfig();

} // namespace nixd
50 changes: 50 additions & 0 deletions nixd/lib/CommandLine/Configuration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/// \file
/// \brief This file implements CLI initialized configuration.

#include "nixd/CommandLine/Configuration.h"
#include "nixd/CommandLine/Options.h"
#include "nixd/Controller/Configuration.h"

#include "lspserver/Logger.h"

#include <llvm/Support/CommandLine.h>
#include <llvm/Support/JSON.h>

#include <iostream>
#include <string>

using namespace nixd;
using namespace llvm::cl;

namespace {

opt<std::string> DefaultConfigJSON{"config",
desc("JSON-encoded initial configuration"),
init(""), cat(NixdCategory)};

} // namespace

Configuration nixd::parseCLIConfig() {
if (DefaultConfigJSON.empty())
return {};

llvm::Expected<llvm::json::Value> V = llvm::json::parse(DefaultConfigJSON);

if (!V) {
llvm::errs()
inclyc marked this conversation as resolved.
Show resolved Hide resolved
<< "The JSON string of default configuration cannot be parsed, reason: "
<< V.takeError() << "\n";
std::exit(-1);
}

llvm::json::Path::Root R;
Configuration C;
if (!fromJSON(*V, C, R)) {
// JSON is valid, but it has incorrect schema
llvm::errs()
<< "The JSON string of default configuration has invalid schema: "
<< R.getError() << "\n";
std::exit(-1);
}
return C;
}
2 changes: 2 additions & 0 deletions nixd/lib/Controller/LifeTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "nixd-config.h"

#include "nixd/CommandLine/Configuration.h"
#include "nixd/CommandLine/Options.h"
#include "nixd/Controller/Controller.h"
#include "nixd/Eval/Launch.h"
Expand Down Expand Up @@ -183,6 +184,7 @@ void Controller::
evalExprWithProgress(*Client, getDefaultNixOSOptionsExpr(),
"nixos options");
}
Config = parseCLIConfig();
fetchConfig();
}

Expand Down
1 change: 1 addition & 0 deletions nixd/lib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ libnixd_deps = [ nixd_lsp_server, nixf, llvm, nixt ]

libnixd_lib = library(
'nixd',
'CommandLine/Configuration.cpp',
'CommandLine/Options.cpp',
'Controller/AST.cpp',
'Controller/CodeAction.cpp',
Expand Down
62 changes: 62 additions & 0 deletions nixd/tools/nixd/test/format/format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# RUN: nixd --lit-test -config='{ "formatting": { "command": ["%S/nixfmt"] } }' < %s | FileCheck %s

<-- initialize(0)

```json
{
"jsonrpc":"2.0",
"id":0,
"method":"initialize",
"params":{
"processId":123,
"rootPath":"",
"capabilities":{
},
"trace":"off"
}
}
```

```json
{
"jsonrpc":"2.0",
"method":"textDocument/didOpen",
"params":{
"textDocument":{
"uri":"file:///format.nix",
"languageId":"nix",
"version":1,
"text":"{ stdenv,\npkgs}: \n let x=1; in { y = x; }"
}
}
}
```

<-- textDocument/formatting

```json
{
"jsonrpc": "2.0",
"id": 2,
"method": "textDocument/formatting",
"params": {
"textDocument": {
"uri": "file:///format.nix"
},
"options": {
"tabSize": 2,
"insertSpaces": true,
"trimTrailingWhitespace": true,
"insertFinalNewline": true
}
}
}
```

```
CHECK: "newText": "Hello\n",
```

```json
{"jsonrpc":"2.0","method":"exit"}
```
2 changes: 2 additions & 0 deletions nixd/tools/nixd/test/format/nixfmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
echo "Hello"
Loading