Skip to content

Commit 1edc205

Browse files
committed
prevent stdin in lsp mode in pad
1 parent 44b40d1 commit 1edc205

File tree

6 files changed

+57
-30
lines changed

6 files changed

+57
-30
lines changed

pad/editor/src/backend.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
io::Cursor,
77
path::{Path, PathBuf},
88
sync::{
9-
atomic::{AtomicUsize, Ordering},
9+
atomic::{AtomicBool, AtomicUsize, Ordering},
1010
Mutex,
1111
},
1212
};
@@ -26,6 +26,7 @@ pub struct WebBackend {
2626
streams: Mutex<HashMap<Handle, VirtualStream>>,
2727
id: u64,
2828
breakpoint: AtomicUsize,
29+
output_enabled: AtomicBool,
2930
}
3031

3132
struct VirtualStream {
@@ -99,6 +100,7 @@ impl WebBackend {
99100
streams: HashMap::new().into(),
100101
id,
101102
breakpoint: AtomicUsize::new(0),
103+
output_enabled: AtomicBool::new(true),
102104
}
103105
}
104106
pub fn finish(&self) {
@@ -157,7 +159,16 @@ impl SysBackend for WebBackend {
157159
fn any_mut(&mut self) -> &mut dyn Any {
158160
self
159161
}
162+
fn output_enabled(&self) -> bool {
163+
self.output_enabled.load(Ordering::Relaxed)
164+
}
165+
fn set_output_enabled(&self, enabled: bool) -> bool {
166+
self.output_enabled.swap(enabled, Ordering::Relaxed)
167+
}
160168
fn print_str_stdout(&self, s: &str) -> Result<(), String> {
169+
if !self.output_enabled() {
170+
return Ok(());
171+
}
161172
if s.contains('\u{07}') {
162173
weewuh();
163174
}
@@ -180,13 +191,22 @@ impl SysBackend for WebBackend {
180191
Ok(())
181192
}
182193
fn print_str_stderr(&self, s: &str) -> Result<(), String> {
194+
if !self.output_enabled() {
195+
return Ok(());
196+
}
183197
self.stderr.lock().unwrap().push_str(s);
184198
Ok(())
185199
}
186200
fn print_str_trace(&self, s: &str) {
201+
if !self.output_enabled() {
202+
return;
203+
}
187204
self.trace.lock().unwrap().push_str(s);
188205
}
189206
fn scan_line_stdin(&self) -> Result<Option<String>, String> {
207+
if !self.output_enabled() {
208+
return Ok(None);
209+
}
190210
Ok(window()
191211
.prompt_with_message("Enter a line of text for stdin")
192212
.unwrap_or(None))

src/compile/modifier.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,9 +1443,10 @@ impl Compiler {
14431443
}
14441444
env.push(formatted);
14451445

1446-
#[cfg(feature = "native_sys")]
1447-
let enabled =
1448-
crate::sys::native::set_output_enabled(self.pre_eval_mode != PreEvalMode::Lsp);
1446+
let enabled = env
1447+
.rt
1448+
.backend
1449+
.set_output_enabled(self.pre_eval_mode != PreEvalMode::Lsp);
14491450

14501451
let res = (|| -> UiuaResult {
14511452
env.exec(mac.root)?;
@@ -1473,8 +1474,7 @@ impl Compiler {
14731474
self.errors.push(e);
14741475
}
14751476

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

14791479
swap(&mut env.asm, &mut self.asm);
14801480
Ok(())

src/format.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,16 +1420,14 @@ impl Formatter<'_> {
14201420
let mut env = Uiua::with_backend(FormatterBackend::default())
14211421
.with_execution_limit(Duration::from_secs(2));
14221422

1423-
#[cfg(feature = "native_sys")]
1424-
let enabled = crate::sys::native::set_output_enabled(false);
1423+
let enabled = env.rt.backend.set_output_enabled(false);
14251424
let res = env.compile_run(|comp| {
14261425
comp.print_diagnostics(false)
14271426
.mode(RunMode::All)
14281427
.pre_eval_mode(PreEvalMode::Lazy)
14291428
.load_str_src(&self.inputs.get(&self.src), self.src.clone())
14301429
});
1431-
#[cfg(feature = "native_sys")]
1432-
crate::sys::native::set_output_enabled(enabled);
1430+
env.rt.backend.set_output_enabled(enabled);
14331431

14341432
let mut values = env.rt.output_comments;
14351433
if let Err(e) = res {

src/lsp.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ impl Spanner {
276276
fn new(src: InputSrc, input: &str, backend: impl SysBackend) -> Self {
277277
let mut compiler = Compiler::with_backend(backend);
278278
compiler.pre_eval_mode(PreEvalMode::Lsp);
279+
compiler.backend().set_output_enabled(false);
279280
let errors = match compiler.load_str_src(input, src.clone()) {
280281
Ok(_) => Vec::new(),
281282
Err(e) => e.into_multi(),
@@ -866,9 +867,6 @@ mod server {
866867

867868
#[doc(hidden)]
868869
pub fn run_language_server() {
869-
#[cfg(feature = "native_sys")]
870-
crate::sys::native::set_output_enabled(false);
871-
872870
tokio::runtime::Builder::new_current_thread()
873871
.build()
874872
.unwrap()

src/sys/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,18 @@ pub trait SysBackend: Any + Send + Sync + 'static {
788788
fn any_mut(&mut self) -> &mut dyn Any;
789789
/// Save a color-formatted version of an error message for later printing
790790
fn save_error_color(&self, message: String, colored: String) {}
791+
/// Check whether output is enabled
792+
fn output_enabled(&self) -> bool {
793+
true
794+
}
795+
/// Set whether output should be enabled
796+
///
797+
/// Returns the previous value.
798+
///
799+
/// It is the trait implementor's responsibility to ensure that this value is respected.
800+
fn set_output_enabled(&self, enabled: bool) -> bool {
801+
true
802+
}
791803
/// Print a string (without a newline) to stdout
792804
fn print_str_stdout(&self, s: &str) -> Result<(), String> {
793805
Err("Printing to stdout is not supported in this environment".into())

src/sys/native.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -297,41 +297,40 @@ pub fn set_audio_stream_time_port(port: u16) -> std::io::Result<()> {
297297
Ok(())
298298
}
299299

300-
pub(crate) fn output_enabled() -> bool {
301-
NATIVE_SYS.output_enabled.load(atomic::Ordering::Relaxed)
302-
}
303-
304-
pub(crate) fn set_output_enabled(enabled: bool) -> bool {
305-
NATIVE_SYS
306-
.output_enabled
307-
.swap(enabled, atomic::Ordering::Relaxed)
308-
}
309-
310300
impl SysBackend for NativeSys {
311301
fn any(&self) -> &dyn Any {
312302
self
313303
}
314304
fn any_mut(&mut self) -> &mut dyn Any {
315305
self
316306
}
307+
fn output_enabled(&self) -> bool {
308+
NATIVE_SYS.output_enabled.load(atomic::Ordering::Relaxed)
309+
}
310+
311+
fn set_output_enabled(&self, enabled: bool) -> bool {
312+
NATIVE_SYS
313+
.output_enabled
314+
.swap(enabled, atomic::Ordering::Relaxed)
315+
}
317316
fn print_str_stdout(&self, s: &str) -> Result<(), String> {
318-
if !output_enabled() {
317+
if !self.output_enabled() {
319318
return Ok(());
320319
}
321320
let mut stdout = stdout().lock();
322321
stdout.write_all(s.as_bytes()).map_err(|e| e.to_string())?;
323322
stdout.flush().map_err(|e| e.to_string())
324323
}
325324
fn print_str_stderr(&self, s: &str) -> Result<(), String> {
326-
if !output_enabled() {
325+
if !self.output_enabled() {
327326
return Ok(());
328327
}
329328
let mut stderr = stderr().lock();
330329
stderr.write_all(s.as_bytes()).map_err(|e| e.to_string())?;
331330
stderr.flush().map_err(|e| e.to_string())
332331
}
333332
fn print_str_trace(&self, s: &str) {
334-
if !output_enabled() {
333+
if !self.output_enabled() {
335334
return;
336335
}
337336
eprint!("{s}");
@@ -345,7 +344,7 @@ impl SysBackend for NativeSys {
345344
self.print_str_stdout(&format!("{}\n", value.show()))
346345
}
347346
fn scan_line_stdin(&self) -> Result<Option<String>, String> {
348-
if !output_enabled() {
347+
if !self.output_enabled() {
349348
return Ok(None);
350349
}
351350
let mut buffer = Vec::new();
@@ -369,7 +368,7 @@ impl SysBackend for NativeSys {
369368
Ok(Some(String::from_utf8(buffer).map_err(|e| e.to_string())?))
370369
}
371370
fn scan_stdin(&self, count: Option<usize>) -> Result<Vec<u8>, String> {
372-
if !output_enabled() {
371+
if !self.output_enabled() {
373372
return Ok(Vec::new());
374373
}
375374
Ok(if let Some(count) = count {
@@ -396,7 +395,7 @@ impl SysBackend for NativeSys {
396395
}
397396
#[cfg(feature = "raw_mode")]
398397
fn set_raw_mode(&self, raw_mode: bool) -> Result<(), String> {
399-
if !output_enabled() {
398+
if !self.output_enabled() {
400399
return Ok(());
401400
}
402401
if raw_mode {
@@ -1222,7 +1221,7 @@ impl SysBackend for NativeSys {
12221221
res
12231222
}
12241223
fn breakpoint(&self, env: &Uiua) -> Result<bool, String> {
1225-
if !output_enabled() {
1224+
if !self.output_enabled() {
12261225
return Ok(true);
12271226
}
12281227
match env.span() {

0 commit comments

Comments
 (0)