Skip to content

Commit 62f0a51

Browse files
authored
rune: Remove boxing and mutex in lsp and introduce builder (#969)
1 parent 2cac7d6 commit 62f0a51

File tree

9 files changed

+399
-244
lines changed

9 files changed

+399
-244
lines changed

crates/rune-cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ categories = ["parser-implementations"]
1616
default-run = "rune"
1717

1818
[dependencies]
19-
rune = { version = "0.14.0", path = "../rune", features = ["cli"] }
19+
rune = { version = "0.14.0", path = "../rune", features = ["cli", "tracing"] }
2020
rune-modules = { version = "0.14.0", path = "../rune-modules", features = ["full"] }
2121

2222
[build-dependencies]

crates/rune-languageserver/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ tracing = "0.1.37"
2020
tracing-appender = "0.2.2"
2121
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
2222

23-
rune = { version = "0.14.0", path = "../rune", features = ["languageserver"] }
23+
rune = { version = "0.14.0", path = "../rune", features = ["languageserver", "tracing"] }
2424
rune-modules = { version = "0.14.0", path = "../rune-modules", features = ["full"] }
2525

2626
[build-dependencies]

crates/rune-languageserver/src/main.rs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,46 @@
1515
//! <br>
1616
//!
1717
//! A language server for the Rune Language, an embeddable dynamic programming language for Rust.
18-
use anyhow::{bail, Result};
19-
use rune::languageserver::{Input, Output};
18+
19+
use anyhow::{anyhow, bail, Result};
20+
use rune::languageserver;
2021
use rune::Options;
2122
use std::env;
23+
use std::fs::File;
2224
use std::path::PathBuf;
23-
use tracing_appender::non_blocking::WorkerGuard;
2425
use tracing_subscriber::EnvFilter;
2526

2627
pub const VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/version.txt"));
2728

28-
fn setup_logging() -> Result<Option<WorkerGuard>> {
29-
let mut guard = None;
30-
31-
let env_filter = EnvFilter::from_env("RUNE_LOG");
29+
fn setup_logging() -> Result<()> {
30+
let Ok(env_filter) = EnvFilter::try_from_env("RUNE_LOG") else {
31+
return Ok(());
32+
};
3233

3334
// Set environment variable to get the language server to trace log to the
3435
// given file.
35-
if let Some(log_path) = std::env::var_os("RUNE_LOG_FILE") {
36-
let log_path = PathBuf::from(log_path);
36+
let Some(log_path) = std::env::var_os("RUNE_LOG_FILE") else {
37+
return Ok(());
38+
};
3739

38-
if let (Some(d), Some(name)) = (log_path.parent(), log_path.file_name()) {
39-
let file_appender = tracing_appender::rolling::never(d, name);
40-
let (non_blocking, g) = tracing_appender::non_blocking(file_appender);
40+
let log_path = PathBuf::from(log_path);
4141

42-
tracing_subscriber::fmt()
43-
.with_env_filter(env_filter)
44-
.with_writer(non_blocking)
45-
.init();
46-
47-
guard = Some(g);
48-
}
42+
if let Some(d) = log_path.parent() {
43+
_ = std::fs::create_dir_all(d);
4944
}
5045

51-
Ok(guard)
46+
tracing_subscriber::fmt()
47+
.with_env_filter(env_filter)
48+
.with_writer(File::options().append(true).create(true).open(log_path)?)
49+
.try_init()
50+
.map_err(|e| anyhow!("{e}"))?;
51+
52+
Ok(())
5253
}
5354

54-
fn main() -> Result<()> {
55-
let _guard = setup_logging()?;
55+
#[tokio::main]
56+
async fn main() -> Result<()> {
57+
setup_logging()?;
5658

5759
let mut it = env::args();
5860
it.next();
@@ -74,20 +76,15 @@ fn main() -> Result<()> {
7476
}
7577

7678
let context = rune_modules::default_context()?;
77-
7879
let options = Options::from_default_env()?;
7980

80-
let runtime = tokio::runtime::Builder::new_current_thread()
81-
.enable_all()
81+
let languageserver = languageserver::builder()
82+
.with_context(context)
83+
.with_options(options)
84+
.with_stdio()
8285
.build()?;
8386

84-
let result = runtime.block_on(rune::languageserver::run(
85-
context,
86-
options,
87-
(Input::from_stdin()?, Output::from_stdout()?),
88-
));
89-
90-
match result {
87+
match languageserver.run().await {
9188
Ok(()) => {
9289
tracing::info!("Server shutting down");
9390
}

crates/rune/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ byte-code = ["alloc", "musli/storage", "musli/std", "rune-alloc/std"]
2929
capture-io = ["alloc", "parking_lot"]
3030
disable-io = ["alloc"]
3131
fmt = ["alloc", "anyhow"]
32-
std = ["alloc", "num/std", "serde/std", "rune-core/std", "rune-alloc/std", "musli?/std", "once_cell/std", "anyhow?/std", "syntree/std"]
32+
std = ["alloc", "num/std", "serde/std", "rune-core/std", "rune-alloc/std", "musli?/std", "once_cell/std", "anyhow?/std", "syntree/std", "tokio?/io-std"]
3333
alloc = ["rune-alloc/alloc", "rune-core/alloc", "once_cell/alloc", "serde?/alloc"]
3434
musli = ["dep:musli", "rune-core/musli", "rune-alloc/musli"]
3535
serde = ["dep:serde", "rune-alloc/serde", "relative-path?/serde"]
@@ -62,7 +62,7 @@ rust-embed = { version = "8.7.2", optional = true }
6262
semver = { version = "1.0.17", optional = true, features = ["serde"] }
6363
serde-hashkey = { version = "0.4.5", optional = true }
6464
syntect = { version = "5.2.0", optional = true, default-features = false, features = ["default-fancy"] }
65-
tokio = { version = "1.28.1", features = ["rt-multi-thread", "fs", "macros", "sync", "io-std", "io-util"], optional = true }
65+
tokio = { version = "1.28.1", features = ["rt-multi-thread", "fs", "macros", "sync", "io-util"], optional = true }
6666
toml = { version = "0.8.19", optional = true, features = ["parse"] }
6767
tracing-subscriber = { version = "0.3.17", features = ["env-filter"], optional = true }
6868
webbrowser = { version = "1.0.2", optional = true }
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use anyhow::Result;
22

3-
use crate::{
4-
languageserver::{Input, Output},
5-
Context, Options,
6-
};
3+
use crate::languageserver;
4+
use crate::{Context, Options};
75

86
pub(super) async fn run(context: Context) -> Result<()> {
97
let options = Options::from_default_env()?;
10-
crate::languageserver::run(
11-
context,
12-
options,
13-
(Input::from_stdin()?, Output::from_stdout()?),
14-
)
15-
.await?;
8+
9+
let ls = languageserver::builder()
10+
.with_context(context)
11+
.with_options(options)
12+
.with_stdio()
13+
.build()?;
14+
15+
ls.run().await?;
1616
Ok(())
1717
}

0 commit comments

Comments
 (0)