Repository navigation
Expand file tree
/
Copy pathanalysis-presentation.rs
More file actions
80 lines (80 loc) · 3.26 KB
/
Copy pathanalysis-presentation.rs
File metadata and controls
80 lines (80 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! Session host: one immutable snapshot, one example parse, three derived products.
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[path = "support/presented_analysis.rs"]
mod host;
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn main() -> Result<(), Box<dyn std::error::Error>> {
use replai::*;
use std::time::Duration;
// Optional qualification fixture: delay the first nonempty display until editing advances.
let mut delay_first = std::env::args().any(|a| a == "--delayed-analysis");
let mut delayed = None;
let mut input = Interaction::new(Editor::new(1_048_576, 100));
input.set_submission_policy(SubmissionPolicy::Validated)?;
println!(
"Host analysis demo: type bu, Tab to choose; braces enable multiline. [~...] is a hint, never submitted. Ctrl-D on empty exits."
);
loop {
input.open_driven(
&std::io::stdin(),
&std::io::stdout(),
Prompt::new("analyze")?,
)?;
let mut parsed = None;
let mut revision = None;
loop {
let mut event = input.poll(Duration::from_millis(100))?;
if input.is_open() && revision != Some(input.revision()) {
let current = host::Parsed::new(input.analysis_snapshot());
if let Some(old) = delayed.take() {
let outcome = input.present_analysis(old)?;
assert_eq!(outcome, AnalysisOutcome::Stale);
eprintln!("DELAYED_PRESENTATION {outcome:?}");
}
if delay_first && !input.editor().text().is_empty() {
delayed = Some(current.presentation()?);
delay_first = false;
eprintln!("ANALYSIS_PENDING");
} else {
input.present_analysis(current.presentation()?)?;
}
revision = Some(input.revision());
parsed = Some(current);
}
match event {
Some(Event::CompletionRequested) => {
input.present_completions(parsed.as_ref().unwrap().completions()?)?;
}
Some(Event::SubmissionRequested(_)) => {
event = input
.apply_validation(parsed.as_ref().unwrap().validation()?)?
.event;
}
_ => {}
}
match event {
Some(Event::Submitted(text)) => {
println!("host received: {text}");
if !text.is_empty() {
input.editor_mut()?.admit_history(&text)?;
}
input.editor_mut()?.clear();
break;
}
Some(Event::Interrupted) => {
input.editor_mut()?.clear();
break;
}
Some(Event::EndOfInput) => return Ok(()),
Some(Event::Rejected(e)) => {
input.external_output(Role::Warning, &e.to_string())?;
}
_ => {}
}
}
}
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
fn main() {
eprintln!("This example requires the Linux/macOS terminal backend.");
}