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

feat(nargo): Add lsp command to start server that reports no capabilities #1560

Merged
merged 7 commits into from
Jun 7, 2023
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
127 changes: 127 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,27 @@ fm = { path = "crates/fm" }
iter-extended = { path = "crates/iter-extended" }
nargo = { path = "crates/nargo" }
nargo_cli = { path = "crates/nargo_cli" }
nargo_lsp = { path = "crates/nargo_lsp" }
noirc_abi = { path = "crates/noirc_abi" }
noirc_driver = { path = "crates/noirc_driver" }
noirc_errors = { path = "crates/noirc_errors" }
noirc_evaluator = { path = "crates/noirc_evaluator" }
noirc_frontend = { path = "crates/noirc_frontend" }
noir_wasm = { path = "crates/wasm" }

async-lsp = { version = "0.0.4", default-features = false, features = ["omni-trait"] }
cfg-if = "1.0.0"
clap = { version = "4.1.4", features = ["derive"]}
codespan = "0.9.5"
codespan-reporting = "0.9.5"
chumsky = { git = "https://github.com/jfecher/chumsky", rev = "ad9d312" }
dirs = "4"
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0"
smol_str = "0.1.17"
thiserror = "1.0.21"
toml = "0.7.2"
tower = "0.4"
url = "2.2.0"
wasm-bindgen = { version = "0.2.83", features = ["serde-serialize"] }
wasm-bindgen-test = "0.3.33"
19 changes: 16 additions & 3 deletions crates/nargo_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,25 @@ build-data = "0.1.3"
toml.workspace = true

[dependencies]
async-lsp.workspace = true
cfg-if.workspace = true
clap.workspace = true
dirs.workspace = true
url.workspace = true
iter-extended.workspace = true
nargo.workspace = true
nargo_lsp.workspace = true
noirc_driver.workspace = true
noirc_frontend.workspace = true
noirc_abi.workspace = true
acvm.workspace = true
toml.workspace = true
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
tower.workspace = true
const_format = "0.2.30"
hex = "0.4.2"
serde_json = "1.0"
termcolor = "1.1.2"
color-eyre = "0.6.2"
tokio = "1.0"
Expand All @@ -50,5 +53,15 @@ predicates = "2.1.5"
[features]
default = ["plonk_bn254"]
# The plonk backend can only use bn254, so we do not specify the field
plonk_bn254 = ["acvm-backend-barretenberg/native"]
plonk_bn254_wasm = ["acvm-backend-barretenberg/wasm"]
plonk_bn254 = [
"acvm-backend-barretenberg/native",
"async-lsp/client-monitor",
"async-lsp/stdio",
"async-lsp/tracing"
]
plonk_bn254_wasm = [
"acvm-backend-barretenberg/wasm",
"async-lsp/client-monitor",
"async-lsp/stdio",
"async-lsp/tracing"
]
phated marked this conversation as resolved.
Show resolved Hide resolved
48 changes: 48 additions & 0 deletions crates/nargo_cli/src/cli/lsp_cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use acvm::Backend;
use async_lsp::{
client_monitor::ClientProcessMonitorLayer, concurrency::ConcurrencyLayer,
panic::CatchUnwindLayer, server::LifecycleLayer, stdio::PipeStdin, tracing::TracingLayer,
};
use clap::Args;
use nargo_lsp::NargoLspService;
use noirc_driver::CompileOptions;
use tokio::io::BufReader;
use tower::ServiceBuilder;

use super::NargoConfig;
use crate::errors::CliError;

#[derive(Debug, Clone, Args)]
pub(crate) struct LspCommand {
#[clap(flatten)]
compile_options: CompileOptions,
}

pub(crate) fn run<B: Backend>(
// Backend is currently unused, but we might want to use it to inform the lsp in the future
_backend: &B,
_args: LspCommand,
_config: NargoConfig,
) -> Result<(), CliError<B>> {
use tokio::runtime::Builder;

let runtime = Builder::new_current_thread().enable_all().build().unwrap();

let (server, _) = async_lsp::Frontend::new_server(|client| {
let router = NargoLspService::new();

ServiceBuilder::new()
.layer(TracingLayer::default())
.layer(LifecycleLayer::default())
.layer(CatchUnwindLayer::default())
.layer(ConcurrencyLayer::default())
.layer(ClientProcessMonitorLayer::new(client))
.service(router)
});

runtime.block_on(async {
let stdin = BufReader::new(PipeStdin::lock().unwrap());
let stdout = async_lsp::stdio::PipeStdout::lock().unwrap();
server.run(stdin, stdout).await.map_err(CliError::LspError)
})
}
5 changes: 4 additions & 1 deletion crates/nargo_cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod codegen_verifier_cmd;
mod compile_cmd;
mod execute_cmd;
mod gates_cmd;
mod lsp_cmd;
mod new_cmd;
mod prove_cmd;
mod test_cmd;
Expand Down Expand Up @@ -55,13 +56,14 @@ enum NargoCommand {
Verify(verify_cmd::VerifyCommand),
Test(test_cmd::TestCommand),
Gates(gates_cmd::GatesCommand),
Lsp(lsp_cmd::LspCommand),
}

pub fn start_cli() -> eyre::Result<()> {
let NargoCli { command, mut config } = NargoCli::parse();

// Search through parent directories to find package root if necessary.
if !matches!(command, NargoCommand::New(_)) {
if !matches!(command, NargoCommand::New(_)) && !matches!(command, NargoCommand::Lsp(_)) {
phated marked this conversation as resolved.
Show resolved Hide resolved
config.program_dir = find_package_root(&config.program_dir)?;
}

Expand All @@ -77,6 +79,7 @@ pub fn start_cli() -> eyre::Result<()> {
NargoCommand::Test(args) => test_cmd::run(&backend, args, config),
NargoCommand::Gates(args) => gates_cmd::run(&backend, args, config),
NargoCommand::CodegenVerifier(args) => codegen_verifier_cmd::run(&backend, args, config),
NargoCommand::Lsp(args) => lsp_cmd::run(&backend, args, config),
}?;

Ok(())
Expand Down
3 changes: 3 additions & 0 deletions crates/nargo_cli/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ pub(crate) enum CliError<B: Backend> {
#[error(transparent)]
FilesystemError(#[from] FilesystemError),

#[error(transparent)]
LspError(#[from] async_lsp::Error),

/// Error from Nargo
#[error(transparent)]
NargoError(#[from] NargoError),
Expand Down
Loading