2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-12-17 20:18:17 -05:00
|
|
|
|
2023-11-05 17:58:59 -05:00
|
|
|
use crate::cdp;
|
2021-12-17 20:18:17 -05:00
|
|
|
use crate::colors;
|
|
|
|
use deno_ast::swc::parser::error::SyntaxError;
|
2023-01-04 18:54:54 -05:00
|
|
|
use deno_ast::swc::parser::token::BinOpToken;
|
2021-12-17 20:18:17 -05:00
|
|
|
use deno_ast::swc::parser::token::Token;
|
|
|
|
use deno_ast::swc::parser::token::Word;
|
2023-01-04 18:54:54 -05:00
|
|
|
use deno_ast::view::AssignOp;
|
2022-11-24 20:56:47 -05:00
|
|
|
use deno_core::anyhow::Context as _;
|
2021-12-17 20:18:17 -05:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::parking_lot::Mutex;
|
2022-02-07 11:05:49 -05:00
|
|
|
use deno_core::serde_json;
|
2021-12-17 20:18:17 -05:00
|
|
|
use rustyline::completion::Completer;
|
|
|
|
use rustyline::error::ReadlineError;
|
|
|
|
use rustyline::highlight::Highlighter;
|
|
|
|
use rustyline::validate::ValidationContext;
|
|
|
|
use rustyline::validate::ValidationResult;
|
|
|
|
use rustyline::validate::Validator;
|
2022-06-14 20:19:06 -04:00
|
|
|
use rustyline::Cmd;
|
2021-12-17 20:18:17 -05:00
|
|
|
use rustyline::CompletionType;
|
2023-01-14 23:18:58 -05:00
|
|
|
use rustyline::ConditionalEventHandler;
|
2021-12-17 20:18:17 -05:00
|
|
|
use rustyline::Config;
|
|
|
|
use rustyline::Context;
|
|
|
|
use rustyline::Editor;
|
2023-01-14 23:18:58 -05:00
|
|
|
use rustyline::Event;
|
|
|
|
use rustyline::EventContext;
|
2022-06-14 20:19:06 -04:00
|
|
|
use rustyline::EventHandler;
|
|
|
|
use rustyline::KeyCode;
|
|
|
|
use rustyline::KeyEvent;
|
|
|
|
use rustyline::Modifiers;
|
2023-01-14 23:18:58 -05:00
|
|
|
use rustyline::RepeatCount;
|
|
|
|
use rustyline_derive::Helper;
|
|
|
|
use rustyline_derive::Hinter;
|
2021-12-17 20:18:17 -05:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::path::PathBuf;
|
2022-11-24 20:56:47 -05:00
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
|
use std::sync::atomic::Ordering::Relaxed;
|
2021-12-17 20:18:17 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use super::channel::RustylineSyncMessageSender;
|
2023-03-15 21:41:13 -04:00
|
|
|
use super::session::REPL_INTERNALS_NAME;
|
2021-12-17 20:18:17 -05:00
|
|
|
|
|
|
|
// Provides helpers to the editor like validation for multi-line edits, completion candidates for
|
|
|
|
// tab completion.
|
|
|
|
#[derive(Helper, Hinter)]
|
|
|
|
pub struct EditorHelper {
|
|
|
|
pub context_id: u64,
|
|
|
|
pub sync_sender: RustylineSyncMessageSender,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EditorHelper {
|
|
|
|
pub fn get_global_lexical_scope_names(&self) -> Vec<String> {
|
|
|
|
let evaluate_response = self
|
|
|
|
.sync_sender
|
|
|
|
.post_message(
|
|
|
|
"Runtime.globalLexicalScopeNames",
|
2022-02-07 11:05:49 -05:00
|
|
|
Some(cdp::GlobalLexicalScopeNamesArgs {
|
|
|
|
execution_context_id: Some(self.context_id),
|
|
|
|
}),
|
2021-12-17 20:18:17 -05:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-02-07 11:05:49 -05:00
|
|
|
let evaluate_response: cdp::GlobalLexicalScopeNamesResponse =
|
|
|
|
serde_json::from_value(evaluate_response).unwrap();
|
|
|
|
evaluate_response.names
|
2021-12-17 20:18:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_expression_property_names(&self, expr: &str) -> Vec<String> {
|
|
|
|
// try to get the properties from the expression
|
|
|
|
if let Some(properties) = self.get_object_expr_properties(expr) {
|
|
|
|
return properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise fall back to the prototype
|
|
|
|
let expr_type = self.get_expression_type(expr);
|
|
|
|
let object_expr = match expr_type.as_deref() {
|
|
|
|
// possibilities: https://chromedevtools.github.io/devtools-protocol/v8/Runtime/#type-RemoteObject
|
|
|
|
Some("object") => "Object.prototype",
|
|
|
|
Some("function") => "Function.prototype",
|
|
|
|
Some("string") => "String.prototype",
|
|
|
|
Some("boolean") => "Boolean.prototype",
|
|
|
|
Some("bigint") => "BigInt.prototype",
|
|
|
|
Some("number") => "Number.prototype",
|
|
|
|
_ => return Vec::new(), // undefined, symbol, and unhandled
|
|
|
|
};
|
|
|
|
|
|
|
|
self
|
|
|
|
.get_object_expr_properties(object_expr)
|
2022-02-24 20:03:12 -05:00
|
|
|
.unwrap_or_default()
|
2021-12-17 20:18:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_expression_type(&self, expr: &str) -> Option<String> {
|
2022-02-07 11:05:49 -05:00
|
|
|
self.evaluate_expression(expr).map(|res| res.result.kind)
|
2021-12-17 20:18:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_object_expr_properties(
|
|
|
|
&self,
|
|
|
|
object_expr: &str,
|
|
|
|
) -> Option<Vec<String>> {
|
|
|
|
let evaluate_result = self.evaluate_expression(object_expr)?;
|
2022-02-07 11:05:49 -05:00
|
|
|
let object_id = evaluate_result.result.object_id?;
|
2021-12-17 20:18:17 -05:00
|
|
|
|
|
|
|
let get_properties_response = self
|
|
|
|
.sync_sender
|
|
|
|
.post_message(
|
|
|
|
"Runtime.getProperties",
|
2022-02-07 11:05:49 -05:00
|
|
|
Some(cdp::GetPropertiesArgs {
|
|
|
|
object_id,
|
|
|
|
own_properties: None,
|
|
|
|
accessor_properties_only: None,
|
|
|
|
generate_preview: None,
|
2023-03-16 15:46:50 -04:00
|
|
|
non_indexed_properties_only: Some(true),
|
2022-02-07 11:05:49 -05:00
|
|
|
}),
|
2021-12-17 20:18:17 -05:00
|
|
|
)
|
|
|
|
.ok()?;
|
2022-02-07 11:05:49 -05:00
|
|
|
let get_properties_response: cdp::GetPropertiesResponse =
|
|
|
|
serde_json::from_value(get_properties_response).ok()?;
|
2021-12-17 20:18:17 -05:00
|
|
|
Some(
|
|
|
|
get_properties_response
|
2022-02-07 11:05:49 -05:00
|
|
|
.result
|
|
|
|
.into_iter()
|
|
|
|
.map(|prop| prop.name)
|
2021-12-17 20:18:17 -05:00
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-07 11:05:49 -05:00
|
|
|
fn evaluate_expression(&self, expr: &str) -> Option<cdp::EvaluateResponse> {
|
2021-12-17 20:18:17 -05:00
|
|
|
let evaluate_response = self
|
|
|
|
.sync_sender
|
|
|
|
.post_message(
|
|
|
|
"Runtime.evaluate",
|
2022-02-07 11:05:49 -05:00
|
|
|
Some(cdp::EvaluateArgs {
|
|
|
|
expression: expr.to_string(),
|
|
|
|
object_group: None,
|
|
|
|
include_command_line_api: None,
|
|
|
|
silent: None,
|
|
|
|
context_id: Some(self.context_id),
|
|
|
|
return_by_value: None,
|
|
|
|
generate_preview: None,
|
|
|
|
user_gesture: None,
|
|
|
|
await_promise: None,
|
|
|
|
throw_on_side_effect: Some(true),
|
|
|
|
timeout: Some(200),
|
|
|
|
disable_breaks: None,
|
|
|
|
repl_mode: None,
|
|
|
|
allow_unsafe_eval_blocked_by_csp: None,
|
|
|
|
unique_context_id: None,
|
|
|
|
}),
|
2021-12-17 20:18:17 -05:00
|
|
|
)
|
|
|
|
.ok()?;
|
2022-02-07 11:05:49 -05:00
|
|
|
let evaluate_response: cdp::EvaluateResponse =
|
|
|
|
serde_json::from_value(evaluate_response).ok()?;
|
2021-12-17 20:18:17 -05:00
|
|
|
|
2022-02-07 11:05:49 -05:00
|
|
|
if evaluate_response.exception_details.is_some() {
|
2021-12-17 20:18:17 -05:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(evaluate_response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_word_boundary(c: char) -> bool {
|
2023-03-15 21:41:13 -04:00
|
|
|
if matches!(c, '.' | '_' | '$') {
|
2021-12-17 20:18:17 -05:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
char::is_ascii_whitespace(&c) || char::is_ascii_punctuation(&c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_expr_from_line_at_pos(line: &str, cursor_pos: usize) -> &str {
|
2023-03-15 17:46:36 -04:00
|
|
|
let start = line[..cursor_pos].rfind(is_word_boundary).unwrap_or(0);
|
2021-12-17 20:18:17 -05:00
|
|
|
let end = line[cursor_pos..]
|
|
|
|
.rfind(is_word_boundary)
|
2023-03-15 17:46:36 -04:00
|
|
|
.map(|i| cursor_pos + i)
|
|
|
|
.unwrap_or(cursor_pos);
|
2021-12-17 20:18:17 -05:00
|
|
|
|
|
|
|
let word = &line[start..end];
|
|
|
|
let word = word.strip_prefix(is_word_boundary).unwrap_or(word);
|
|
|
|
let word = word.strip_suffix(is_word_boundary).unwrap_or(word);
|
|
|
|
|
|
|
|
word
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Completer for EditorHelper {
|
|
|
|
type Candidate = String;
|
|
|
|
|
|
|
|
fn complete(
|
|
|
|
&self,
|
|
|
|
line: &str,
|
|
|
|
pos: usize,
|
|
|
|
_ctx: &Context<'_>,
|
|
|
|
) -> Result<(usize, Vec<String>), ReadlineError> {
|
|
|
|
let lsp_completions = self.sync_sender.lsp_completions(line, pos);
|
|
|
|
if !lsp_completions.is_empty() {
|
|
|
|
// assumes all lsp completions have the same start position
|
|
|
|
return Ok((
|
2022-05-20 16:40:55 -04:00
|
|
|
lsp_completions[0].range.start,
|
2021-12-17 20:18:17 -05:00
|
|
|
lsp_completions.into_iter().map(|c| c.new_text).collect(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let expr = get_expr_from_line_at_pos(line, pos);
|
|
|
|
|
|
|
|
// check if the expression is in the form `obj.prop`
|
|
|
|
if let Some(index) = expr.rfind('.') {
|
|
|
|
let sub_expr = &expr[..index];
|
|
|
|
let prop_name = &expr[index + 1..];
|
|
|
|
let candidates = self
|
|
|
|
.get_expression_property_names(sub_expr)
|
|
|
|
.into_iter()
|
2023-03-15 21:41:13 -04:00
|
|
|
.filter(|n| {
|
|
|
|
!n.starts_with("Symbol(")
|
|
|
|
&& n.starts_with(prop_name)
|
|
|
|
&& n != &*REPL_INTERNALS_NAME
|
|
|
|
})
|
2021-12-17 20:18:17 -05:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok((pos - prop_name.len(), candidates))
|
|
|
|
} else {
|
|
|
|
// combine results of declarations and globalThis properties
|
|
|
|
let mut candidates = self
|
|
|
|
.get_expression_property_names("globalThis")
|
|
|
|
.into_iter()
|
|
|
|
.chain(self.get_global_lexical_scope_names())
|
2023-03-15 21:41:13 -04:00
|
|
|
.filter(|n| n.starts_with(expr) && n != &*REPL_INTERNALS_NAME)
|
2021-12-17 20:18:17 -05:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// sort and remove duplicates
|
|
|
|
candidates.sort();
|
|
|
|
candidates.dedup(); // make sure to sort first
|
|
|
|
|
|
|
|
Ok((pos - expr.len(), candidates))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Validator for EditorHelper {
|
|
|
|
fn validate(
|
|
|
|
&self,
|
|
|
|
ctx: &mut ValidationContext,
|
|
|
|
) -> Result<ValidationResult, ReadlineError> {
|
2023-01-16 09:45:06 -05:00
|
|
|
Ok(validate(ctx.input()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validate(input: &str) -> ValidationResult {
|
|
|
|
let line_info = text_lines::TextLines::new(input);
|
|
|
|
let mut stack: Vec<Token> = Vec::new();
|
|
|
|
let mut in_template = false;
|
|
|
|
let mut div_token_count_on_current_line = 0;
|
|
|
|
let mut last_line_index = 0;
|
2023-03-28 17:49:00 -04:00
|
|
|
let mut queued_validation_error = None;
|
|
|
|
let tokens = deno_ast::lex(input, deno_ast::MediaType::TypeScript)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|item| match item.inner {
|
|
|
|
deno_ast::TokenOrComment::Token(token) => Some((token, item.range)),
|
|
|
|
deno_ast::TokenOrComment::Comment { .. } => None,
|
|
|
|
});
|
|
|
|
|
|
|
|
for (token, range) in tokens {
|
|
|
|
let current_line_index = line_info.line_index(range.start);
|
2023-01-16 09:45:06 -05:00
|
|
|
if current_line_index != last_line_index {
|
|
|
|
div_token_count_on_current_line = 0;
|
|
|
|
last_line_index = current_line_index;
|
2023-03-28 17:49:00 -04:00
|
|
|
|
|
|
|
if let Some(error) = queued_validation_error {
|
|
|
|
return error;
|
|
|
|
}
|
2023-01-16 09:45:06 -05:00
|
|
|
}
|
2023-03-28 17:49:00 -04:00
|
|
|
match token {
|
|
|
|
Token::BinOp(BinOpToken::Div) | Token::AssignOp(AssignOp::DivAssign) => {
|
|
|
|
// it's too complicated to write code to detect regular expression literals
|
|
|
|
// which are no longer tokenized, so if a `/` or `/=` happens twice on the same
|
|
|
|
// line, then we bail
|
|
|
|
div_token_count_on_current_line += 1;
|
|
|
|
if div_token_count_on_current_line >= 2 {
|
|
|
|
return ValidationResult::Valid(None);
|
2023-01-16 09:45:06 -05:00
|
|
|
}
|
2023-03-28 17:49:00 -04:00
|
|
|
}
|
|
|
|
Token::BackQuote => in_template = !in_template,
|
|
|
|
Token::LParen | Token::LBracket | Token::LBrace | Token::DollarLBrace => {
|
|
|
|
stack.push(token)
|
|
|
|
}
|
|
|
|
Token::RParen | Token::RBracket | Token::RBrace => {
|
|
|
|
match (stack.pop(), token) {
|
|
|
|
(Some(Token::LParen), Token::RParen)
|
|
|
|
| (Some(Token::LBracket), Token::RBracket)
|
|
|
|
| (Some(Token::LBrace), Token::RBrace)
|
|
|
|
| (Some(Token::DollarLBrace), Token::RBrace) => {}
|
|
|
|
(Some(left), _) => {
|
2023-06-26 09:10:27 -04:00
|
|
|
// queue up a validation error to surface once we've finished examining the current line
|
2023-03-28 17:49:00 -04:00
|
|
|
queued_validation_error = Some(ValidationResult::Invalid(Some(
|
|
|
|
format!("Mismatched pairs: {left:?} is not properly closed"),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
(None, _) => {
|
|
|
|
// While technically invalid when unpaired, it should be V8's task to output error instead.
|
|
|
|
// Thus marked as valid with no info.
|
|
|
|
return ValidationResult::Valid(None);
|
2021-12-17 20:18:17 -05:00
|
|
|
}
|
2023-01-16 09:45:06 -05:00
|
|
|
}
|
2023-03-28 17:49:00 -04:00
|
|
|
}
|
|
|
|
Token::Error(error) => {
|
|
|
|
match error.kind() {
|
|
|
|
// If there is unterminated template, it continues to read input.
|
|
|
|
SyntaxError::UnterminatedTpl => {}
|
|
|
|
_ => {
|
|
|
|
// If it failed parsing, it should be V8's task to output error instead.
|
|
|
|
// Thus marked as valid with no info.
|
|
|
|
return ValidationResult::Valid(None);
|
2021-12-17 20:18:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-28 17:49:00 -04:00
|
|
|
_ => {}
|
2021-12-17 20:18:17 -05:00
|
|
|
}
|
2023-01-16 09:45:06 -05:00
|
|
|
}
|
2021-12-17 20:18:17 -05:00
|
|
|
|
2023-03-28 17:49:00 -04:00
|
|
|
if let Some(error) = queued_validation_error {
|
|
|
|
error
|
|
|
|
} else if !stack.is_empty() || in_template {
|
|
|
|
ValidationResult::Incomplete
|
|
|
|
} else {
|
|
|
|
ValidationResult::Valid(None)
|
2021-12-17 20:18:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Highlighter for EditorHelper {
|
|
|
|
fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
|
|
|
|
hint.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn highlight_candidate<'c>(
|
|
|
|
&self,
|
|
|
|
candidate: &'c str,
|
|
|
|
completion: rustyline::CompletionType,
|
|
|
|
) -> Cow<'c, str> {
|
|
|
|
if completion == CompletionType::List {
|
|
|
|
candidate.into()
|
|
|
|
} else {
|
|
|
|
self.highlight(candidate, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-06 15:56:40 -05:00
|
|
|
fn highlight_char(&self, line: &str, _: usize, _: bool) -> bool {
|
2021-12-17 20:18:17 -05:00
|
|
|
!line.is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> {
|
|
|
|
let mut out_line = String::from(line);
|
|
|
|
|
2022-08-10 12:39:16 -04:00
|
|
|
let mut lexed_items = deno_ast::lex(line, deno_ast::MediaType::TypeScript)
|
|
|
|
.into_iter()
|
|
|
|
.peekable();
|
|
|
|
while let Some(item) = lexed_items.next() {
|
2021-12-17 20:18:17 -05:00
|
|
|
// Adding color adds more bytes to the string,
|
|
|
|
// so an offset is needed to stop spans falling out of sync.
|
|
|
|
let offset = out_line.len() - line.len();
|
2022-05-20 16:40:55 -04:00
|
|
|
let range = item.range;
|
2021-12-17 20:18:17 -05:00
|
|
|
|
|
|
|
out_line.replace_range(
|
2022-05-20 16:40:55 -04:00
|
|
|
range.start + offset..range.end + offset,
|
2021-12-17 20:18:17 -05:00
|
|
|
&match item.inner {
|
|
|
|
deno_ast::TokenOrComment::Token(token) => match token {
|
|
|
|
Token::Str { .. } | Token::Template { .. } | Token::BackQuote => {
|
2022-05-20 16:40:55 -04:00
|
|
|
colors::green(&line[range]).to_string()
|
2021-12-17 20:18:17 -05:00
|
|
|
}
|
2022-05-20 16:40:55 -04:00
|
|
|
Token::Regex(_, _) => colors::red(&line[range]).to_string(),
|
2022-04-08 12:31:47 -04:00
|
|
|
Token::Num { .. } | Token::BigInt { .. } => {
|
2022-05-20 16:40:55 -04:00
|
|
|
colors::yellow(&line[range]).to_string()
|
2021-12-17 20:18:17 -05:00
|
|
|
}
|
|
|
|
Token::Word(word) => match word {
|
|
|
|
Word::True | Word::False | Word::Null => {
|
2022-05-20 16:40:55 -04:00
|
|
|
colors::yellow(&line[range]).to_string()
|
2021-12-17 20:18:17 -05:00
|
|
|
}
|
2022-05-20 16:40:55 -04:00
|
|
|
Word::Keyword(_) => colors::cyan(&line[range]).to_string(),
|
2021-12-17 20:18:17 -05:00
|
|
|
Word::Ident(ident) => {
|
2023-10-24 09:37:02 -04:00
|
|
|
match ident.as_ref() {
|
|
|
|
"undefined" => colors::gray(&line[range]).to_string(),
|
|
|
|
"Infinity" | "NaN" => {
|
|
|
|
colors::yellow(&line[range]).to_string()
|
|
|
|
}
|
|
|
|
"async" | "of" => colors::cyan(&line[range]).to_string(),
|
|
|
|
_ => {
|
|
|
|
let next = lexed_items.peek().map(|item| &item.inner);
|
|
|
|
if matches!(
|
2023-08-02 16:27:03 -04:00
|
|
|
next,
|
2023-10-24 09:37:02 -04:00
|
|
|
Some(deno_ast::TokenOrComment::Token(Token::LParen))
|
|
|
|
) {
|
|
|
|
// We're looking for something that looks like a function
|
|
|
|
// We use a simple heuristic: 'ident' followed by 'LParen'
|
|
|
|
colors::intense_blue(&line[range]).to_string()
|
|
|
|
} else if ident.as_ref() == "from"
|
|
|
|
&& matches!(
|
|
|
|
next,
|
|
|
|
Some(deno_ast::TokenOrComment::Token(
|
|
|
|
Token::Str { .. }
|
|
|
|
))
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// When ident 'from' is followed by a string literal, highlight it
|
|
|
|
// E.g. "export * from 'something'" or "import a from 'something'"
|
|
|
|
colors::cyan(&line[range]).to_string()
|
|
|
|
} else {
|
|
|
|
line[range].to_string()
|
|
|
|
}
|
2022-08-10 12:39:16 -04:00
|
|
|
}
|
2021-12-17 20:18:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-05-20 16:40:55 -04:00
|
|
|
_ => line[range].to_string(),
|
2021-12-17 20:18:17 -05:00
|
|
|
},
|
|
|
|
deno_ast::TokenOrComment::Comment { .. } => {
|
2022-05-20 16:40:55 -04:00
|
|
|
colors::gray(&line[range]).to_string()
|
2021-12-17 20:18:17 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
out_line.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ReplEditor {
|
2023-12-06 15:56:40 -05:00
|
|
|
inner: Arc<Mutex<Editor<EditorHelper, rustyline::history::FileHistory>>>,
|
2023-03-16 12:22:24 -04:00
|
|
|
history_file_path: Option<PathBuf>,
|
2022-11-24 20:56:47 -05:00
|
|
|
errored_on_history_save: Arc<AtomicBool>,
|
2022-12-16 18:39:52 -05:00
|
|
|
should_exit_on_interrupt: Arc<AtomicBool>,
|
2021-12-17 20:18:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ReplEditor {
|
2022-11-24 20:56:47 -05:00
|
|
|
pub fn new(
|
|
|
|
helper: EditorHelper,
|
2023-03-16 12:22:24 -04:00
|
|
|
history_file_path: Option<PathBuf>,
|
2022-11-24 20:56:47 -05:00
|
|
|
) -> Result<Self, AnyError> {
|
2021-12-17 20:18:17 -05:00
|
|
|
let editor_config = Config::builder()
|
|
|
|
.completion_type(CompletionType::List)
|
|
|
|
.build();
|
|
|
|
|
2022-07-18 14:47:00 -04:00
|
|
|
let mut editor =
|
|
|
|
Editor::with_config(editor_config).expect("Failed to create editor.");
|
2021-12-17 20:18:17 -05:00
|
|
|
editor.set_helper(Some(helper));
|
2023-03-16 12:22:24 -04:00
|
|
|
if let Some(history_file_path) = &history_file_path {
|
|
|
|
editor.load_history(history_file_path).unwrap_or(());
|
|
|
|
}
|
2022-06-14 20:19:06 -04:00
|
|
|
editor.bind_sequence(
|
|
|
|
KeyEvent(KeyCode::Char('s'), Modifiers::CTRL),
|
|
|
|
EventHandler::Simple(Cmd::Newline),
|
|
|
|
);
|
2022-06-20 18:47:25 -04:00
|
|
|
editor.bind_sequence(
|
2022-06-21 15:24:50 -04:00
|
|
|
KeyEvent(KeyCode::Tab, Modifiers::NONE),
|
2022-06-20 18:47:25 -04:00
|
|
|
EventHandler::Conditional(Box::new(TabEventHandler)),
|
|
|
|
);
|
2022-12-16 18:39:52 -05:00
|
|
|
let should_exit_on_interrupt = Arc::new(AtomicBool::new(false));
|
|
|
|
editor.bind_sequence(
|
|
|
|
KeyEvent(KeyCode::Char('r'), Modifiers::CTRL),
|
|
|
|
EventHandler::Conditional(Box::new(ReverseSearchHistoryEventHandler {
|
|
|
|
should_exit_on_interrupt: should_exit_on_interrupt.clone(),
|
|
|
|
})),
|
|
|
|
);
|
2021-12-17 20:18:17 -05:00
|
|
|
|
2023-03-16 12:22:24 -04:00
|
|
|
if let Some(history_file_path) = &history_file_path {
|
|
|
|
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()
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
}
|
2022-11-24 20:56:47 -05:00
|
|
|
|
|
|
|
Ok(ReplEditor {
|
2021-12-17 20:18:17 -05:00
|
|
|
inner: Arc::new(Mutex::new(editor)),
|
|
|
|
history_file_path,
|
2022-11-24 20:56:47 -05:00
|
|
|
errored_on_history_save: Arc::new(AtomicBool::new(false)),
|
2022-12-16 18:39:52 -05:00
|
|
|
should_exit_on_interrupt,
|
2022-11-24 20:56:47 -05:00
|
|
|
})
|
2021-12-17 20:18:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn readline(&self) -> Result<String, ReadlineError> {
|
|
|
|
self.inner.lock().readline("> ")
|
|
|
|
}
|
|
|
|
|
2022-11-24 20:56:47 -05:00
|
|
|
pub fn update_history(&self, entry: String) {
|
2023-12-06 15:56:40 -05:00
|
|
|
let _ = self.inner.lock().add_history_entry(entry);
|
2023-03-16 12:22:24 -04:00
|
|
|
if let Some(history_file_path) = &self.history_file_path {
|
|
|
|
if let Err(e) = self.inner.lock().append_history(history_file_path) {
|
|
|
|
if self.errored_on_history_save.load(Relaxed) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-17 20:18:17 -05:00
|
|
|
|
2023-03-16 12:22:24 -04:00
|
|
|
self.errored_on_history_save.store(true, Relaxed);
|
|
|
|
eprintln!("Unable to save history file: {e}");
|
|
|
|
}
|
2022-11-24 20:56:47 -05:00
|
|
|
}
|
2021-12-17 20:18:17 -05:00
|
|
|
}
|
2022-12-16 18:39:52 -05:00
|
|
|
|
|
|
|
pub fn should_exit_on_interrupt(&self) -> bool {
|
|
|
|
self.should_exit_on_interrupt.load(Relaxed)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_should_exit_on_interrupt(&self, yes: bool) {
|
|
|
|
self.should_exit_on_interrupt.store(yes, Relaxed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Command to reverse search history , same as rustyline default C-R but that resets repl should_exit flag to false
|
|
|
|
struct ReverseSearchHistoryEventHandler {
|
|
|
|
should_exit_on_interrupt: Arc<AtomicBool>,
|
|
|
|
}
|
|
|
|
impl ConditionalEventHandler for ReverseSearchHistoryEventHandler {
|
|
|
|
fn handle(
|
|
|
|
&self,
|
|
|
|
_: &Event,
|
|
|
|
_: RepeatCount,
|
|
|
|
_: bool,
|
|
|
|
_: &EventContext,
|
|
|
|
) -> Option<Cmd> {
|
|
|
|
self.should_exit_on_interrupt.store(false, Relaxed);
|
|
|
|
Some(Cmd::ReverseSearchHistory)
|
|
|
|
}
|
2021-12-17 20:18:17 -05:00
|
|
|
}
|
2022-06-20 18:47:25 -04:00
|
|
|
|
|
|
|
/// A custom tab key event handler
|
|
|
|
/// It uses a heuristic to determine if the user is requesting completion or if they want to insert an actual tab
|
|
|
|
/// The heuristic goes like this:
|
|
|
|
/// - If the last character before the cursor is whitespace, the the user wants to insert a tab
|
|
|
|
/// - Else the user is requesting completion
|
|
|
|
struct TabEventHandler;
|
|
|
|
impl ConditionalEventHandler for TabEventHandler {
|
|
|
|
fn handle(
|
|
|
|
&self,
|
|
|
|
evt: &Event,
|
|
|
|
n: RepeatCount,
|
|
|
|
_: bool,
|
|
|
|
ctx: &EventContext,
|
|
|
|
) -> Option<Cmd> {
|
2022-06-21 15:24:50 -04:00
|
|
|
debug_assert_eq!(
|
|
|
|
*evt,
|
|
|
|
Event::from(KeyEvent(KeyCode::Tab, Modifiers::NONE))
|
|
|
|
);
|
2022-06-20 18:47:25 -04:00
|
|
|
if ctx.line().is_empty()
|
|
|
|
|| ctx.line()[..ctx.pos()]
|
|
|
|
.chars()
|
2023-07-13 17:16:24 -04:00
|
|
|
.next_back()
|
2022-06-20 18:47:25 -04:00
|
|
|
.filter(|c| c.is_whitespace())
|
|
|
|
.is_some()
|
|
|
|
{
|
2022-06-22 10:28:28 -04:00
|
|
|
if cfg!(target_os = "windows") {
|
|
|
|
// Inserting a tab is broken in windows with rustyline
|
|
|
|
// use 4 spaces as a workaround for now
|
|
|
|
Some(Cmd::Insert(n, " ".into()))
|
|
|
|
} else {
|
|
|
|
Some(Cmd::Insert(n, "\t".into()))
|
|
|
|
}
|
2022-06-20 18:47:25 -04:00
|
|
|
} else {
|
|
|
|
None // default complete
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-16 09:45:06 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use rustyline::validate::ValidationResult;
|
|
|
|
|
|
|
|
use super::validate;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn validate_only_one_forward_slash_per_line() {
|
|
|
|
let code = r#"function test(arr){
|
|
|
|
if( arr.length <= 1) return arr.map(a => a / 2)
|
|
|
|
let left = test( arr.slice( 0 , arr.length/2 ) )"#;
|
|
|
|
assert!(matches!(validate(code), ValidationResult::Incomplete));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn validate_regex_looking_code() {
|
|
|
|
let code = r#"/testing/;"#;
|
|
|
|
assert!(matches!(validate(code), ValidationResult::Valid(_)));
|
|
|
|
}
|
|
|
|
}
|