Skip to content

Commit

Permalink
prevent stdin in lsp mode in pad
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Dec 31, 2024
1 parent 44b40d1 commit 1edc205
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 30 deletions.
22 changes: 21 additions & 1 deletion pad/editor/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
io::Cursor,
path::{Path, PathBuf},
sync::{
atomic::{AtomicUsize, Ordering},
atomic::{AtomicBool, AtomicUsize, Ordering},
Mutex,
},
};
Expand All @@ -26,6 +26,7 @@ pub struct WebBackend {
streams: Mutex<HashMap<Handle, VirtualStream>>,
id: u64,
breakpoint: AtomicUsize,
output_enabled: AtomicBool,
}

struct VirtualStream {
Expand Down Expand Up @@ -99,6 +100,7 @@ impl WebBackend {
streams: HashMap::new().into(),
id,
breakpoint: AtomicUsize::new(0),
output_enabled: AtomicBool::new(true),
}
}
pub fn finish(&self) {
Expand Down Expand Up @@ -157,7 +159,16 @@ impl SysBackend for WebBackend {
fn any_mut(&mut self) -> &mut dyn Any {
self
}
fn output_enabled(&self) -> bool {
self.output_enabled.load(Ordering::Relaxed)
}
fn set_output_enabled(&self, enabled: bool) -> bool {
self.output_enabled.swap(enabled, Ordering::Relaxed)
}
fn print_str_stdout(&self, s: &str) -> Result<(), String> {
if !self.output_enabled() {
return Ok(());
}
if s.contains('\u{07}') {
weewuh();
}
Expand All @@ -180,13 +191,22 @@ impl SysBackend for WebBackend {
Ok(())
}
fn print_str_stderr(&self, s: &str) -> Result<(), String> {
if !self.output_enabled() {
return Ok(());
}
self.stderr.lock().unwrap().push_str(s);
Ok(())
}
fn print_str_trace(&self, s: &str) {
if !self.output_enabled() {
return;
}
self.trace.lock().unwrap().push_str(s);
}
fn scan_line_stdin(&self) -> Result<Option<String>, String> {
if !self.output_enabled() {
return Ok(None);
}
Ok(window()
.prompt_with_message("Enter a line of text for stdin")
.unwrap_or(None))
Expand Down
10 changes: 5 additions & 5 deletions src/compile/modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1443,9 +1443,10 @@ impl Compiler {
}
env.push(formatted);

#[cfg(feature = "native_sys")]
let enabled =
crate::sys::native::set_output_enabled(self.pre_eval_mode != PreEvalMode::Lsp);
let enabled = env
.rt
.backend
.set_output_enabled(self.pre_eval_mode != PreEvalMode::Lsp);

let res = (|| -> UiuaResult {
env.exec(mac.root)?;
Expand Down Expand Up @@ -1473,8 +1474,7 @@ impl Compiler {
self.errors.push(e);
}

#[cfg(feature = "native_sys")]
crate::sys::native::set_output_enabled(enabled);
env.rt.backend.set_output_enabled(enabled);

swap(&mut env.asm, &mut self.asm);
Ok(())
Expand Down
6 changes: 2 additions & 4 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,16 +1420,14 @@ impl Formatter<'_> {
let mut env = Uiua::with_backend(FormatterBackend::default())
.with_execution_limit(Duration::from_secs(2));

#[cfg(feature = "native_sys")]
let enabled = crate::sys::native::set_output_enabled(false);
let enabled = env.rt.backend.set_output_enabled(false);
let res = env.compile_run(|comp| {
comp.print_diagnostics(false)
.mode(RunMode::All)
.pre_eval_mode(PreEvalMode::Lazy)
.load_str_src(&self.inputs.get(&self.src), self.src.clone())
});
#[cfg(feature = "native_sys")]
crate::sys::native::set_output_enabled(enabled);
env.rt.backend.set_output_enabled(enabled);

let mut values = env.rt.output_comments;
if let Err(e) = res {
Expand Down
4 changes: 1 addition & 3 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ impl Spanner {
fn new(src: InputSrc, input: &str, backend: impl SysBackend) -> Self {
let mut compiler = Compiler::with_backend(backend);
compiler.pre_eval_mode(PreEvalMode::Lsp);
compiler.backend().set_output_enabled(false);
let errors = match compiler.load_str_src(input, src.clone()) {
Ok(_) => Vec::new(),
Err(e) => e.into_multi(),
Expand Down Expand Up @@ -866,9 +867,6 @@ mod server {

#[doc(hidden)]
pub fn run_language_server() {
#[cfg(feature = "native_sys")]
crate::sys::native::set_output_enabled(false);

tokio::runtime::Builder::new_current_thread()
.build()
.unwrap()
Expand Down
12 changes: 12 additions & 0 deletions src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,18 @@ pub trait SysBackend: Any + Send + Sync + 'static {
fn any_mut(&mut self) -> &mut dyn Any;
/// Save a color-formatted version of an error message for later printing
fn save_error_color(&self, message: String, colored: String) {}
/// Check whether output is enabled
fn output_enabled(&self) -> bool {
true
}
/// Set whether output should be enabled
///
/// Returns the previous value.
///
/// It is the trait implementor's responsibility to ensure that this value is respected.
fn set_output_enabled(&self, enabled: bool) -> bool {
true
}
/// Print a string (without a newline) to stdout
fn print_str_stdout(&self, s: &str) -> Result<(), String> {
Err("Printing to stdout is not supported in this environment".into())
Expand Down
33 changes: 16 additions & 17 deletions src/sys/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,41 +297,40 @@ pub fn set_audio_stream_time_port(port: u16) -> std::io::Result<()> {
Ok(())
}

pub(crate) fn output_enabled() -> bool {
NATIVE_SYS.output_enabled.load(atomic::Ordering::Relaxed)
}

pub(crate) fn set_output_enabled(enabled: bool) -> bool {
NATIVE_SYS
.output_enabled
.swap(enabled, atomic::Ordering::Relaxed)
}

impl SysBackend for NativeSys {
fn any(&self) -> &dyn Any {
self
}
fn any_mut(&mut self) -> &mut dyn Any {
self
}
fn output_enabled(&self) -> bool {
NATIVE_SYS.output_enabled.load(atomic::Ordering::Relaxed)
}

fn set_output_enabled(&self, enabled: bool) -> bool {
NATIVE_SYS
.output_enabled
.swap(enabled, atomic::Ordering::Relaxed)
}
fn print_str_stdout(&self, s: &str) -> Result<(), String> {
if !output_enabled() {
if !self.output_enabled() {
return Ok(());
}
let mut stdout = stdout().lock();
stdout.write_all(s.as_bytes()).map_err(|e| e.to_string())?;
stdout.flush().map_err(|e| e.to_string())
}
fn print_str_stderr(&self, s: &str) -> Result<(), String> {
if !output_enabled() {
if !self.output_enabled() {
return Ok(());
}
let mut stderr = stderr().lock();
stderr.write_all(s.as_bytes()).map_err(|e| e.to_string())?;
stderr.flush().map_err(|e| e.to_string())
}
fn print_str_trace(&self, s: &str) {
if !output_enabled() {
if !self.output_enabled() {
return;
}
eprint!("{s}");
Expand All @@ -345,7 +344,7 @@ impl SysBackend for NativeSys {
self.print_str_stdout(&format!("{}\n", value.show()))
}
fn scan_line_stdin(&self) -> Result<Option<String>, String> {
if !output_enabled() {
if !self.output_enabled() {
return Ok(None);
}
let mut buffer = Vec::new();
Expand All @@ -369,7 +368,7 @@ impl SysBackend for NativeSys {
Ok(Some(String::from_utf8(buffer).map_err(|e| e.to_string())?))
}
fn scan_stdin(&self, count: Option<usize>) -> Result<Vec<u8>, String> {
if !output_enabled() {
if !self.output_enabled() {
return Ok(Vec::new());
}
Ok(if let Some(count) = count {
Expand All @@ -396,7 +395,7 @@ impl SysBackend for NativeSys {
}
#[cfg(feature = "raw_mode")]
fn set_raw_mode(&self, raw_mode: bool) -> Result<(), String> {
if !output_enabled() {
if !self.output_enabled() {
return Ok(());
}
if raw_mode {
Expand Down Expand Up @@ -1222,7 +1221,7 @@ impl SysBackend for NativeSys {
res
}
fn breakpoint(&self, env: &Uiua) -> Result<bool, String> {
if !output_enabled() {
if !self.output_enabled() {
return Ok(true);
}
match env.span() {
Expand Down

0 comments on commit 1edc205

Please sign in to comment.