Skip to content

Commit

Permalink
Prefer keeping complete_src on Session
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Oct 29, 2024
1 parent cf44833 commit 3475690
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/cli_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub(crate) fn repl(interrupted: Arc<AtomicBool>) {
start_time: Instant::now(),
trace_exprs: false,
stop_at_expr_id: None,
complete_src: String::new(),
};

let mut rl = new_editor();
Expand Down
4 changes: 4 additions & 0 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ pub(crate) struct Session {
///
/// Useful for 'evaluate up to cursor'.
pub(crate) stop_at_expr_id: Option<SyntaxId>,
/// A transcript of all inputs sent during the session.
pub(crate) complete_src: String,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -4343,6 +4345,7 @@ mod tests {
start_time: Instant::now(),
trace_exprs: false,
stop_at_expr_id: None,
complete_src: String::new(),
};

super::eval_exprs(exprs, env, &mut session)
Expand Down Expand Up @@ -4766,6 +4769,7 @@ mod tests {
start_time: Instant::now(),
trace_exprs: false,
stop_at_expr_id: None,
complete_src: String::new(),
};

let mut env = Env::default();
Expand Down
26 changes: 7 additions & 19 deletions src/json_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ fn handle_eval_request(
end_offset: Option<usize>,
env: &mut Env,
session: &mut Session,
complete_src: &mut String,
) -> Response {
complete_src.push_str(input);
session.complete_src.push_str(input);

let (items, mut errors) = parse_toplevel_items_from_span(
&path
Expand Down Expand Up @@ -398,12 +397,7 @@ fn handle_eval_up_to_request(
}
}

pub(crate) fn handle_request(
req_src: &str,
env: &mut Env,
session: &mut Session,
complete_src: &mut String,
) -> Response {
pub(crate) fn handle_request(req_src: &str, env: &mut Env, session: &mut Session) -> Response {
let Ok(req) = serde_json::from_str::<Request>(req_src) else {
return Response {
kind: ResponseKind::MalformedRequest,
Expand Down Expand Up @@ -501,15 +495,9 @@ pub(crate) fn handle_request(
warnings: vec![],
}
}
Err(CommandParseError::NotCommandSyntax) => handle_eval_request(
path.as_ref(),
&input,
offset,
end_offset,
env,
session,
complete_src,
),
Err(CommandParseError::NotCommandSyntax) => {
handle_eval_request(path.as_ref(), &input, offset, end_offset, env, session)
}
},
Request::FindDefinition { name } => handle_find_def_request(&name, env),
Request::EvalUpToId { path, src, offset } => {
Expand Down Expand Up @@ -653,14 +641,14 @@ pub(crate) fn json_session(interrupted: Arc<AtomicBool>) {
println!("{}", serialized);

let mut env = Env::default();
let mut complete_src = String::new();
let mut session = Session {
interrupted,
has_attached_stdout: false,
start_time: Instant::now(),
trace_exprs: false,
// TODO: set this position limit from the request.
stop_at_expr_id: None,
complete_src: String::new(),
};

loop {
Expand All @@ -687,7 +675,7 @@ pub(crate) fn json_session(interrupted: Arc<AtomicBool>) {

let buf_str = String::from_utf8(buf).unwrap();

let response = handle_request(&buf_str, &mut env, &mut session, &mut complete_src);
let response = handle_request(&buf_str, &mut env, &mut session);
let serialized = serde_json::to_string(&response).unwrap();
println!("{}", serialized);
} else {
Expand Down
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,20 +229,20 @@ fn main() {
let src = read_utf8_or_die(&path);

let mut env = Env::default();
let mut complete_src = String::new();
let mut session = Session {
interrupted,
has_attached_stdout: true,
start_time: Instant::now(),
trace_exprs: false,
stop_at_expr_id: None,
complete_src: String::new(),
};

let json_lines = src
.lines()
.filter(|line| !line.starts_with("//") && !line.is_empty());
for line in json_lines {
let response = handle_request(line, &mut env, &mut session, &mut complete_src);
let response = handle_request(line, &mut env, &mut session);
println!("{}", serde_json::to_string_pretty(&response).unwrap());
}
}
Expand Down Expand Up @@ -273,6 +273,7 @@ fn test_eval_up_to(src: &str, path: &Path, offset: usize, interrupted: Arc<Atomi
start_time: Instant::now(),
trace_exprs: false,
stop_at_expr_id: None,
complete_src: String::new(),
};

let items = parse_toplevel_items_or_die(path, src, &mut env);
Expand Down Expand Up @@ -389,6 +390,7 @@ fn run_sandboxed_tests_in_file(
start_time: Instant::now(),
trace_exprs: false,
stop_at_expr_id: None,
complete_src: String::new(),
};

// TODO: for real IDE usage we'll want to use the environment of
Expand Down Expand Up @@ -481,6 +483,7 @@ fn run_tests_in_file(src: &str, path: &Path, interrupted: Arc<AtomicBool>) {
start_time: Instant::now(),
trace_exprs: false,
stop_at_expr_id: None,
complete_src: String::new(),
};

eval_toplevel_defs(&items, &mut env);
Expand Down Expand Up @@ -574,6 +577,7 @@ fn run_file(src: &str, path: &Path, arguments: &[String], interrupted: Arc<Atomi
start_time: Instant::now(),
trace_exprs: false,
stop_at_expr_id: None,
complete_src: String::new(),
};

eval_toplevel_defs(&items, &mut env);
Expand Down

0 comments on commit 3475690

Please sign in to comment.