mirror of
https://github.com/denoland/deno.git
synced 2025-03-11 06:37:12 -04:00
Merge branch 'main' into repl_restart
This commit is contained in:
commit
c38599415e
2 changed files with 62 additions and 40 deletions
|
@ -5,6 +5,7 @@ use crate::colors;
|
||||||
use deno_ast::swc::parser::error::SyntaxError;
|
use deno_ast::swc::parser::error::SyntaxError;
|
||||||
use deno_ast::swc::parser::token::Token;
|
use deno_ast::swc::parser::token::Token;
|
||||||
use deno_ast::swc::parser::token::Word;
|
use deno_ast::swc::parser::token::Word;
|
||||||
|
use deno_core::anyhow::Context as _;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::parking_lot::Mutex;
|
use deno_core::parking_lot::Mutex;
|
||||||
use deno_core::serde_json;
|
use deno_core::serde_json;
|
||||||
|
@ -27,6 +28,8 @@ use rustyline::{ConditionalEventHandler, Event, EventContext, RepeatCount};
|
||||||
use rustyline_derive::{Helper, Hinter};
|
use rustyline_derive::{Helper, Hinter};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::sync::atomic::AtomicBool;
|
||||||
|
use std::sync::atomic::Ordering::Relaxed;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use super::channel::RustylineSyncMessageSender;
|
use super::channel::RustylineSyncMessageSender;
|
||||||
|
@ -368,10 +371,14 @@ impl Highlighter for EditorHelper {
|
||||||
pub struct ReplEditor {
|
pub struct ReplEditor {
|
||||||
inner: Arc<Mutex<Editor<EditorHelper>>>,
|
inner: Arc<Mutex<Editor<EditorHelper>>>,
|
||||||
history_file_path: PathBuf,
|
history_file_path: PathBuf,
|
||||||
|
errored_on_history_save: Arc<AtomicBool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ReplEditor {
|
impl ReplEditor {
|
||||||
pub fn new(helper: EditorHelper, history_file_path: PathBuf) -> Self {
|
pub fn new(
|
||||||
|
helper: EditorHelper,
|
||||||
|
history_file_path: PathBuf,
|
||||||
|
) -> Result<Self, AnyError> {
|
||||||
let editor_config = Config::builder()
|
let editor_config = Config::builder()
|
||||||
.completion_type(CompletionType::List)
|
.completion_type(CompletionType::List)
|
||||||
.build();
|
.build();
|
||||||
|
@ -389,25 +396,35 @@ impl ReplEditor {
|
||||||
EventHandler::Conditional(Box::new(TabEventHandler)),
|
EventHandler::Conditional(Box::new(TabEventHandler)),
|
||||||
);
|
);
|
||||||
|
|
||||||
ReplEditor {
|
let history_file_dir = history_file_path.parent().unwrap();
|
||||||
|
std::fs::create_dir_all(history_file_dir).with_context(|| {
|
||||||
|
format!(
|
||||||
|
"Unable to create directory for the history file: {}",
|
||||||
|
history_file_dir.display()
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(ReplEditor {
|
||||||
inner: Arc::new(Mutex::new(editor)),
|
inner: Arc::new(Mutex::new(editor)),
|
||||||
history_file_path,
|
history_file_path,
|
||||||
}
|
errored_on_history_save: Arc::new(AtomicBool::new(false)),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn readline(&self) -> Result<String, ReadlineError> {
|
pub fn readline(&self) -> Result<String, ReadlineError> {
|
||||||
self.inner.lock().readline("> ")
|
self.inner.lock().readline("> ")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_history_entry(&self, entry: String) {
|
pub fn update_history(&self, entry: String) {
|
||||||
self.inner.lock().add_history_entry(entry);
|
self.inner.lock().add_history_entry(entry);
|
||||||
|
if let Err(e) = self.inner.lock().append_history(&self.history_file_path) {
|
||||||
|
if self.errored_on_history_save.load(Relaxed) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save_history(&self) -> Result<(), AnyError> {
|
self.errored_on_history_save.store(true, Relaxed);
|
||||||
std::fs::create_dir_all(self.history_file_path.parent().unwrap())?;
|
eprintln!("Unable to save history file: {}", e);
|
||||||
|
}
|
||||||
self.inner.lock().save_history(&self.history_file_path)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -154,7 +154,7 @@ pub async fn run(
|
||||||
};
|
};
|
||||||
|
|
||||||
let history_file_path = ps.dir.root.join("deno_history.txt");
|
let history_file_path = ps.dir.root.join("deno_history.txt");
|
||||||
let editor = ReplEditor::new(helper, history_file_path);
|
let editor = ReplEditor::new(helper, history_file_path)?;
|
||||||
|
|
||||||
println!("Deno {}", crate::version::deno());
|
println!("Deno {}", crate::version::deno());
|
||||||
println!("exit using ctrl+d, ctrl+c, or close()");
|
println!("exit using ctrl+d, ctrl+c, or close()");
|
||||||
|
@ -169,9 +169,13 @@ pub async fn run(
|
||||||
match line {
|
match line {
|
||||||
Ok(line) => {
|
Ok(line) => {
|
||||||
should_exit_on_interrupt = false;
|
should_exit_on_interrupt = false;
|
||||||
|
editor.update_history(line.clone());
|
||||||
|
|
||||||
if line == ".restart" {
|
match &line {
|
||||||
println!("Started a new REPL session. Global scope has been reset.");
|
".restart" => {
|
||||||
|
println!(
|
||||||
|
"Started a new REPL session. Global scope has been reset."
|
||||||
|
);
|
||||||
repl_session = create_repl_session(
|
repl_session = create_repl_session(
|
||||||
ps,
|
ps,
|
||||||
module_url.clone(),
|
module_url.clone(),
|
||||||
|
@ -180,14 +184,17 @@ pub async fn run(
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
continue;
|
continue;
|
||||||
} else if line == ".help" {
|
}
|
||||||
|
".help" => {
|
||||||
print_help();
|
print_help();
|
||||||
continue;
|
continue;
|
||||||
} else if line == ".exit" {
|
}
|
||||||
|
".exit" => {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
line => {
|
||||||
let output = repl_session.evaluate_line_and_get_output(&line).await?;
|
let output =
|
||||||
|
repl_session.evaluate_line_and_get_output(&line).await?;
|
||||||
|
|
||||||
// We check for close and break here instead of making it a loop condition to get
|
// We check for close and break here instead of making it a loop condition to get
|
||||||
// consistent behavior in when the user evaluates a call to close().
|
// consistent behavior in when the user evaluates a call to close().
|
||||||
|
@ -196,8 +203,8 @@ pub async fn run(
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("{}", output);
|
println!("{}", output);
|
||||||
|
}
|
||||||
editor.add_history_entry(line);
|
}
|
||||||
}
|
}
|
||||||
Err(ReadlineError::Interrupted) => {
|
Err(ReadlineError::Interrupted) => {
|
||||||
if should_exit_on_interrupt {
|
if should_exit_on_interrupt {
|
||||||
|
@ -217,7 +224,5 @@ pub async fn run(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
editor.save_history()?;
|
|
||||||
|
|
||||||
Ok(repl_session.worker.get_exit_code())
|
Ok(repl_session.worker.get_exit_code())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue