-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nixd/Controller: allow setting initial configuration via CLI (#579)
Add a CLI flag `-config` used to set initial configuration. For editor clients which does not support workspace configuration, the flag could be used as a fallback.
- Loading branch information
Showing
9 changed files
with
186 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// \file | ||
/// \brief Allow default configuration being passed via CLI | ||
|
||
#include "nixd/Controller/Configuration.h" | ||
|
||
#include <exception> | ||
|
||
namespace nixd { | ||
|
||
/// \brief Parse the CLI flag and initialize the config nixd::DefaultConfig | ||
nixd::Configuration parseCLIConfig(); | ||
|
||
} // 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,18 @@ | ||
#pragma once | ||
|
||
#include <llvm/Support/Error.h> | ||
|
||
#include <exception> | ||
|
||
namespace nixd { | ||
|
||
class LLVMErrorException : public std::exception { | ||
llvm::Error E; | ||
|
||
public: | ||
LLVMErrorException(llvm::Error E) : E(std::move(E)) {} | ||
|
||
llvm::Error takeError() { return std::move(E); } | ||
}; | ||
|
||
} // 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,38 @@ | ||
#include "Exception.h" | ||
|
||
#include <llvm/ADT/StringRef.h> | ||
#include <llvm/Support/JSON.h> | ||
|
||
#include <exception> | ||
|
||
namespace nixd { | ||
|
||
class JSONParseException : public LLVMErrorException { | ||
public: | ||
JSONParseException(llvm::Error E) : LLVMErrorException(std::move(E)) {} | ||
|
||
[[nodiscard]] const char *what() const noexcept override { | ||
return "JSON result cannot be parsed"; | ||
} | ||
}; | ||
|
||
class JSONSchemaException : public LLVMErrorException { | ||
public: | ||
JSONSchemaException(llvm::Error E) : LLVMErrorException(std::move(E)) {} | ||
|
||
[[nodiscard]] const char *what() const noexcept override { | ||
return "JSON schema mismatch"; | ||
} | ||
}; | ||
|
||
llvm::json::Value parse(llvm::StringRef JSON); | ||
|
||
template <class T> T fromJSON(const llvm::json::Value &V) { | ||
llvm::json::Path::Root R; | ||
T Result; | ||
if (!fromJSON(V, Result, R)) | ||
throw JSONSchemaException(R.getError()); | ||
return Result; | ||
} | ||
|
||
} // 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,32 @@ | ||
/// \file | ||
/// \brief This file implements CLI initialized configuration. | ||
|
||
#include "nixd/CommandLine/Configuration.h" | ||
#include "nixd/CommandLine/Options.h" | ||
#include "nixd/Controller/Configuration.h" | ||
#include "nixd/Support/JSON.h" | ||
|
||
#include "lspserver/Logger.h" | ||
|
||
#include <llvm/Support/CommandLine.h> | ||
#include <llvm/Support/JSON.h> | ||
|
||
#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 {}; | ||
|
||
return nixd::fromJSON<Configuration>(nixd::parse(DefaultConfigJSON)); | ||
} |
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,10 @@ | ||
#include "nixd/Support/JSON.h" | ||
|
||
#include <llvm/ADT/StringRef.h> | ||
|
||
llvm::json::Value nixd::parse(llvm::StringRef JSON) { | ||
llvm::Expected<llvm::json::Value> E = llvm::json::parse(JSON); | ||
if (!E) | ||
throw JSONParseException(E.takeError()); | ||
return *E; | ||
} |
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,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"} | ||
``` |
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,2 @@ | ||
#!/bin/sh | ||
echo "Hello" |