Skip to content

Commit d138e01

Browse files
committed
support --disable-bytecode
1 parent 187d190 commit d138e01

File tree

6 files changed

+45
-30
lines changed

6 files changed

+45
-30
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,7 @@ Please see https://github.com/blahgeek/emacs-lsp-booster/issues/1 . Huge thanks
107107

108108
1. Check that `emacs-lsp-booster` process is running
109109
2. Check the stderr buffer (e.g. for lsp-mode, `*pyright::stderr*` buffer), it should contain `emacs_lsp_booster` related log.
110+
111+
### Advanced usage
112+
113+
Run `emacs-lsp-booster --help` for more options.

src/app.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::{mpsc, Arc, atomic::{AtomicI32, self}};
22

3-
use log::{warn, info};
3+
use log::{warn, info, debug};
44
use anyhow::Result;
55
use serde_json as json;
66

@@ -64,36 +64,41 @@ fn process_client_reader(reader: impl std::io::Read,
6464

6565
fn process_server_reader(reader: impl std::io::Read,
6666
channel_pub: mpsc::Sender<String>,
67-
bytecode_options: BytecodeOptions) -> Result<()> {
67+
bytecode_options: Option<BytecodeOptions>) -> Result<()> {
6868
let mut bufreader = std::io::BufReader::new(reader);
6969
loop {
7070
let msg = rpcio::rpc_read(&mut bufreader)?;
7171
if msg.is_empty() {
7272
break
7373
}
74-
let json_val = json::from_str(&msg)?;
75-
match bytecode::generate_bytecode_repl(&json_val, bytecode_options.clone()) {
76-
Ok(bytecode_str) => {
74+
if let Some(ref bytecode_options) = bytecode_options {
75+
let json_val = json::from_str(&msg)?;
76+
if let Ok(bytecode_str) = bytecode::generate_bytecode_repl(&json_val, bytecode_options.clone()) {
7777
channel_pub.send(bytecode_str)?;
78-
},
79-
Err(e) => {
80-
warn!("Failed to generate bytecode: {}; fallback to original json", e);
81-
channel_pub.send(msg)?;
82-
},
78+
continue
79+
}
8380
}
81+
channel_pub.send(msg)?;
8482
}
8583
Ok(())
8684
}
8785

8886
pub struct AppOptions {
89-
pub bytecode_options: bytecode::BytecodeOptions,
87+
// if bytecode_options is None, then don't generate bytecode!
88+
pub bytecode_options: Option<bytecode::BytecodeOptions>,
9089
}
9190

9291
pub fn run_app_forever(client_reader: impl std::io::Read + Send + 'static,
9392
client_writer: impl std::io::Write + Send + 'static,
9493
mut server_cmd: std::process::Command,
9594
options: AppOptions) -> Result<std::process::ExitStatus> {
9695
info!("Running server {:?}", server_cmd);
96+
if let Some(ref bytecode_options) = options.bytecode_options {
97+
info!("Will convert server json to bytecode! bytecode options: {:?}", bytecode_options);
98+
} else {
99+
info!("Bytecode disabled! Will forward server json as-is.")
100+
}
101+
97102
let mut proc = server_cmd
98103
.stdin(std::process::Stdio::piped())
99104
.stdout(std::process::Stdio::piped())
@@ -108,30 +113,30 @@ pub fn run_app_forever(client_reader: impl std::io::Read + Send + 'static,
108113
let c2s_channel_counter = c2s_channel_counter.clone();
109114
let proc_stdin = proc.stdin.take().unwrap();
110115
std::thread::spawn(move || {
111-
info!("Started client->server write thread");
116+
debug!("Started client->server write thread");
112117
process_channel_to_writer(c2s_channel_sub, Some(c2s_channel_counter), proc_stdin).unwrap();
113-
info!("Finished client->server write thread");
118+
debug!("Finished client->server write thread");
114119
});
115120
}
116121
std::thread::spawn(move || {
117-
info!("Started server->client write thread");
122+
debug!("Started server->client write thread");
118123
process_channel_to_writer(s2c_channel_sub, None, client_writer).unwrap();
119-
info!("Finished server->client write thread");
124+
debug!("Finished server->client write thread");
120125
});
121126
{
122127
let s2c_channel_pub = s2c_channel_pub.clone();
123128
let proc_stdout = proc.stdout.take().unwrap();
124129
std::thread::spawn(move || {
125-
info!("Started server->client read thread");
130+
debug!("Started server->client read thread");
126131
process_server_reader(proc_stdout, s2c_channel_pub, options.bytecode_options).unwrap();
127-
info!("Finished server->client read thread");
132+
debug!("Finished server->client read thread");
128133
});
129134
}
130135
std::thread::spawn(move || {
131-
info!("Started client->server read thread");
136+
debug!("Started client->server read thread");
132137
process_client_reader(
133138
client_reader, c2s_channel_pub, c2s_channel_counter, s2c_channel_pub).unwrap();
134-
info!("Finished client->server read thread");
139+
debug!("Finished client->server read thread");
135140
});
136141

137142
Ok(proc.wait()?)

src/bytecode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub enum ObjectType {
163163
Alist,
164164
}
165165

166-
#[derive(Clone)]
166+
#[derive(Clone, Debug)]
167167
pub struct BytecodeOptions {
168168
pub object_type: ObjectType,
169169
// TODO: array_type

src/main.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ use emacs_lsp_booster::bytecode;
1010
arg_required_else_help = true, after_help = "For backward compatibility, `emacs-lsp-booster <SERVER_CMD>...` (without any options) is also supported" )]
1111
struct Cli {
1212
#[command(flatten)]
13-
verbose: clap_verbosity_flag::Verbosity,
13+
verbose: clap_verbosity_flag::Verbosity<clap_verbosity_flag::InfoLevel>,
1414

1515
#[arg(last = true)]
1616
server_cmd: Vec<String>,
1717

18+
#[arg(short = 'n', long,
19+
help = "Disable bytecode generation. Simply forward server json as-is. Useful for debugging or benchmarking.")]
20+
disable_bytecode: bool,
21+
1822
#[arg(long, default_value = "plist",
1923
help = "Lisp type used to represent a JSON object. Plist is the most performant one.\nMust match what lsp client expects.\n")]
2024
json_object_type: bytecode::ObjectType,
@@ -61,11 +65,12 @@ fn main() -> Result<()> {
6165
cmd.args(&cli.server_cmd[1..]);
6266

6367
let exit_status = app::run_app_forever(std::io::stdin(), std::io::stdout(), cmd, app::AppOptions {
64-
bytecode_options: bytecode::BytecodeOptions {
65-
object_type: cli.json_object_type,
66-
null_value: cli.json_null_value,
67-
false_value: cli.json_false_value,
68-
},
68+
bytecode_options: if !cli.disable_bytecode {
69+
Some(bytecode::BytecodeOptions {
70+
object_type: cli.json_object_type,
71+
null_value: cli.json_null_value,
72+
false_value: cli.json_false_value,
73+
}) } else { None },
6974
})?;
7075
std::process::exit(exit_status.code().unwrap_or(1))
7176
}
@@ -74,6 +79,7 @@ fn main() -> Result<()> {
7479
fn test_parse_args() {
7580
let cli = parse_args(vec!["emacs-lsp-booster", "server_cmd", "arg1"]);
7681
assert_eq!(cli.server_cmd, vec!["server_cmd", "arg1"]);
82+
assert_eq!(cli.verbose.log_level_filter(), log::LevelFilter::Info);
7783

7884
let cli = parse_args(vec!["emacs-lsp-booster", "--", "server_cmd", "arg1"]);
7985
assert_eq!(cli.server_cmd, vec!["server_cmd", "arg1"]);
@@ -83,7 +89,7 @@ fn test_parse_args() {
8389
"--json-null-value", ":null",
8490
"--json-false-value", ":json-false",
8591
"--", "server_cmd", "arg1"]);
86-
assert_eq!(cli.verbose.log_level_filter(), log::LevelFilter::Warn);
92+
assert_eq!(cli.verbose.log_level_filter(), log::LevelFilter::Debug);
8793
assert_eq!(cli.server_cmd, vec!["server_cmd", "arg1"]);
8894
assert_eq!(cli.json_object_type, bytecode::ObjectType::Hashtable);
8995
assert_eq!(cli.json_null_value, bytecode::LispObject::Symbol("null".into()));

src/rpcio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{str::FromStr, io::Write};
22

3-
use log::debug;
3+
use log::trace;
44
use anyhow::{Result, bail};
55

66
// return empty string on EOF
@@ -24,7 +24,7 @@ pub fn rpc_read(reader: &mut impl std::io::BufRead) -> Result<String> {
2424
if splitted.len() != 2 {
2525
bail!("Invalid header format");
2626
}
27-
debug!("Header: {:?}", splitted);
27+
trace!("Header: {:?}", splitted);
2828
if splitted[0] == "Content-Length" {
2929
content_len = Some(usize::from_str(splitted[1])?);
3030
}

tests/app_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn test_app_with_echo_server() -> Result<()> {
2626
cmd.args(&["1", "cat"]);
2727

2828
let exit_status = app::run_app_forever(input_pair_out, output_file, cmd, app::AppOptions {
29-
bytecode_options: Default::default(),
29+
bytecode_options: Some(Default::default()),
3030
})?;
3131
assert!(!exit_status.success()); // timeout kill
3232

0 commit comments

Comments
 (0)