Skip to content

Commit

Permalink
feat: Add history hints and bracket highlight to the REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Jul 29, 2019
1 parent ec1d26d commit 10ef8cd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion repl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ path = "src/main.rs"
doc = false

[dependencies]

gluon = { version = "0.12.0", path = "..", features = ["serialization"] } # GLUON
gluon_vm = { version = "0.12.0", path = "../vm", features = ["serialization"] } # GLUON
gluon_completion = { path = "../completion", version = "0.12.0" } # GLUON
gluon_codegen = { path = "../codegen", version = "0.12.0" } # GLUON
gluon_format = { version = "0.12.0", path = "../format" } # GLUON
gluon_doc = { version = "0.12.0", path = "../doc" } # GLUON

ansi_term = "0.12"
app_dirs = "1.0.0"
futures = "0.1.11"
futures-cpupool = "0.1"
Expand Down
66 changes: 54 additions & 12 deletions repl/src/repl.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
extern crate codespan_reporting;
extern crate futures_cpupool;
extern crate rustyline;

extern crate gluon_completion as completion;

use std::{error::Error as StdError, path::PathBuf, str::FromStr, sync::Mutex};
use std::{borrow::Cow, error::Error as StdError, path::PathBuf, str::FromStr, sync::Mutex};

use futures::{
future::{self, Either},
Expand Down Expand Up @@ -165,13 +161,55 @@ fn complete(thread: &Thread, name: &str, fileinput: &str, pos: usize) -> GluonRe
.collect())
}

struct Completer(RootedThread);
struct Completer {
thread: RootedThread,
hinter: rustyline::hint::HistoryHinter,
highlighter: rustyline::highlight::MatchingBracketHighlighter,
}

impl rustyline::Helper for Completer {}

impl rustyline::hint::Hinter for Completer {}
impl rustyline::hint::Hinter for Completer {
fn hint(&self, line: &str, pos: usize, ctx: &rustyline::Context) -> Option<String> {
self.hinter.hint(line, pos, ctx)
}
}

impl rustyline::highlight::Highlighter for Completer {
fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> {
self.highlighter.highlight(line, pos)
}

fn highlight_prompt<'b, 's: 'b, 'p: 'b>(
&'s self,
prompt: &'p str,
default: bool,
) -> Cow<'b, str> {
self.highlighter.highlight_prompt(prompt, default)
}

impl rustyline::highlight::Highlighter for Completer {}
fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
// TODO Detect when windows supports ANSI escapes
if cfg!(windows) {
Cow::Borrowed(hint)
} else {
use ansi_term::Style;
Cow::Owned(Style::new().dimmed().paint(hint).to_string())
}
}

fn highlight_candidate<'c>(
&self,
candidate: &'c str,
completion: rustyline::CompletionType,
) -> Cow<'c, str> {
self.highlighter.highlight_candidate(candidate, completion)
}

fn highlight_char(&self, line: &str, pos: usize) -> bool {
self.highlighter.highlight_char(line, pos)
}
}

impl rustyline::completion::Completer for Completer {
type Candidate = String;
Expand All @@ -182,7 +220,7 @@ impl rustyline::completion::Completer for Completer {
pos: usize,
_: &rustyline::Context,
) -> rustyline::Result<(usize, Vec<String>)> {
let result = complete(&self.0, "<repl>", line, pos);
let result = complete(&self.thread, "<repl>", line, pos);

// Get the start of the completed identifier
let ident_start = line[..pos]
Expand Down Expand Up @@ -214,7 +252,7 @@ impl_userdata! { Editor }
#[derive(Userdata, Trace, VmType)]
#[gluon(vm_type = "CpuPool")]
#[gluon_trace(skip)]
struct CpuPool(self::futures_cpupool::CpuPool);
struct CpuPool(futures_cpupool::CpuPool);

impl_userdata! { CpuPool }

Expand Down Expand Up @@ -262,7 +300,11 @@ fn new_editor(vm: WithVM<()>) -> IO<Editor> {
if let Err(err) = history_result {
warn!("Unable to load history: {}", err);
}
editor.set_helper(Some(Completer(vm.vm.root_thread())));
editor.set_helper(Some(Completer {
thread: vm.vm.root_thread(),
hinter: rustyline::hint::HistoryHinter {},
highlighter: rustyline::highlight::MatchingBracketHighlighter::default(),
}));
IO::Value(Editor {
editor: Mutex::new(editor),
})
Expand All @@ -286,7 +328,7 @@ fn readline(editor: &Editor, prompt: &str) -> IO<Result<String, ReadlineError>>
}

fn new_cpu_pool(size: usize) -> IO<CpuPool> {
IO::Value(CpuPool(self::futures_cpupool::CpuPool::new(size)))
IO::Value(CpuPool(futures_cpupool::CpuPool::new(size)))
}

fn eval_line(
Expand Down

0 comments on commit 10ef8cd

Please sign in to comment.