Skip to content

Commit

Permalink
nixd-next: init, without any capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Jan 19, 2024
1 parent 9194fc4 commit dd6e0f9
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ subdir('lspserver')
subdir('libnixf')
subdir('libnixt')
subdir('nixd')
subdir('nixd-next')
1 change: 1 addition & 0 deletions nixd-next/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
subdir('tools')
1 change: 1 addition & 0 deletions nixd-next/tools/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
subdir('nixd')
6 changes: 6 additions & 0 deletions nixd-next/tools/nixd/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
nixd_next = executable('nixd-next',
'src/Controller.cpp',
'src/Main.cpp',
install: true,
dependencies: [ nixd_lsp_server, nixf, llvm ]
)
53 changes: 53 additions & 0 deletions nixd-next/tools/nixd/src/Controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/// \file
/// \brief Controller. The process interacting with users.
#include "nixd-config.h"

#include "lspserver/LSPServer.h"

#include <llvm/Support/JSON.h>

namespace {

using namespace lspserver;
using namespace llvm::json;

class Controller : public LSPServer {
public:
Controller(std::unique_ptr<InboundPort> In, std::unique_ptr<OutboundPort> Out)
: LSPServer(std::move(In), std::move(Out)) {

// Life Cycle
Registry.addMethod("initialize", this, &Controller::onInitialize);
Registry.addNotification("initialized", this, &Controller::onInitialized);
}

void onInitialize( // NOLINT(readability-convert-member-functions-to-static)
[[maybe_unused]] const InitializeParams &Params, Callback<Value> Reply) {

Object ServerCaps{
{},
};

Object Result{{
{"serverInfo",
Object{
{"name", "nixd"},
{"version", NIXD_VERSION},
}},
{"capabilities", std::move(ServerCaps)},
}};

Reply(std::move(Result));
}
void onInitialized([[maybe_unused]] const InitializedParams &Params) {}
};

} // namespace

namespace nixd {
void launchController(std::unique_ptr<InboundPort> In,
std::unique_ptr<OutboundPort> Out) {
Controller C(std::move(In), std::move(Out));
C.run();
}
} // namespace nixd
13 changes: 13 additions & 0 deletions nixd-next/tools/nixd/src/Controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "lspserver/Connection.h"

#include <memory>

namespace nixd {

[[noreturn]] void
launchController(std::unique_ptr<lspserver::InboundPort> In,
std::unique_ptr<lspserver::OutboundPort> Out);

} // namespace nixd
83 changes: 83 additions & 0 deletions nixd-next/tools/nixd/src/Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "nixd-config.h"

#include "lspserver/Connection.h"
#include "lspserver/Logger.h"

#include "Controller.h"

#include <llvm/ADT/ArrayRef.h>
#include <llvm/Support/CommandLine.h>

using namespace lspserver;

namespace {

using namespace llvm::cl;

OptionCategory Misc("miscellaneous options");
OptionCategory Debug("debug-only options (for developers)");

const OptionCategory *NixdCatogories[] = {&Misc, &Debug};

opt<JSONStreamStyle> InputStyle{
"input-style",
desc("Input JSON stream encoding"),
values(
clEnumValN(JSONStreamStyle::Standard, "standard", "usual LSP protocol"),
clEnumValN(JSONStreamStyle::Delimited, "delimited",
"messages delimited by `// -----` lines, "
"with // comment support")),
init(JSONStreamStyle::Standard),
cat(Debug),
Hidden,
};
opt<bool> LitTest{
"lit-test",
desc("Abbreviation for -input-style=delimited -pretty -log=verbose. "
"Intended to simplify lit tests"),
init(false), cat(Debug)};
opt<Logger::Level> LogLevel{
"log", desc("Verbosity of log messages written to stderr"),
values(
clEnumValN(Logger::Level::Error, "error", "Error messages only"),
clEnumValN(Logger::Level::Info, "info", "High level execution tracing"),
clEnumValN(Logger::Level::Debug, "debug", "Debugging details"),
clEnumValN(Logger::Level::Verbose, "verbose", "Low level details")),
init(Logger::Level::Info), cat(Misc)};
opt<bool> PrettyPrint{"pretty", desc("Pretty-print JSON output"), init(false),
cat(Debug)};

} // namespace

int main(int argc, char *argv[]) {
SetVersionPrinter([](llvm::raw_ostream &OS) {
OS << "nixd, version: ";
#ifdef NIXD_VCS_TAG
OS << NIXD_VCS_TAG;
#else
OS << NIXD_VERSION;
#endif
OS << "\n";
});

HideUnrelatedOptions(NixdCatogories);
ParseCommandLineOptions(argc, argv, "nixd language server", nullptr,
"NIXD_FLAGS");

if (LitTest) {
InputStyle = JSONStreamStyle::Delimited;
LogLevel = Logger::Level::Verbose;
PrettyPrint = true;
}

StreamLogger Logger(llvm::errs(), LogLevel);
LoggingSession Session(Logger);

auto In = std::make_unique<lspserver::InboundPort>(STDIN_FILENO, InputStyle);

auto Out = std::make_unique<lspserver::OutboundPort>(PrettyPrint);

nixd::launchController(std::move(In), std::move(Out));

return 0;
}

0 comments on commit dd6e0f9

Please sign in to comment.