manen

Fancy Lua REPL
Log | Files | Refs | README | LICENSE

commit 9797b0c74825cb672d639c24644277a0fdabcc77
parent 822b0447811447928a985d4026454b26f800b2a8
Author: Sylvia Ivory <git@sivory.net>
Date:   Mon, 23 Jun 2025 16:58:57 -0700

Reorganize LuaHinter

Diffstat:
Msrc/editor.rs | 5+++--
Asrc/hinter.rs | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/main.rs | 1+
Msrc/validator.rs | 72++----------------------------------------------------------------------
4 files changed, 74 insertions(+), 72 deletions(-)

diff --git a/src/editor.rs b/src/editor.rs @@ -13,7 +13,8 @@ use reedline::{ }; use crate::{ - format::TableFormat, highlight::LuaHighlighter, inspect::display_basic, validator::LuaValidator, + format::TableFormat, highlight::LuaHighlighter, hinter::LuaHinter, inspect::display_basic, + validator::LuaValidator, }; pub struct Editor { @@ -58,7 +59,7 @@ impl Editor { let editor = Reedline::create() .with_highlighter(Box::new(LuaHighlighter::new())) .with_validator(Box::new(LuaValidator::new())) - .with_hinter(Box::new(LuaValidator::new())) + .with_hinter(Box::new(LuaHinter)) .with_edit_mode(Box::new(Emacs::new(keybindings))); Ok(Self { diff --git a/src/hinter.rs b/src/hinter.rs @@ -0,0 +1,68 @@ +use mlua::prelude::*; +use nu_ansi_term::{Color, Style}; +use reedline::{Hinter, History}; + +use crate::inspect::display_basic; + +fn burner_lua() -> Lua { + #[cfg(any(feature = "lua54", feature = "lua53"))] + let flags = LuaStdLib::MATH | LuaStdLib::STRING | LuaStdLib::UTF8; + #[cfg(not(any(feature = "lua54", feature = "lua53")))] + let flags = LuaStdLib::MATH | LuaStdLib::STRING; + + let lua = Lua::new_with(flags, LuaOptions::new()).unwrap(); + + let globals = lua.globals(); + globals.raw_remove("print").unwrap(); + globals.raw_remove("loadfile").unwrap(); + globals.raw_remove("load").unwrap(); + + let math: LuaTable = globals.get("math").unwrap(); + math.raw_remove("random").unwrap(); + + lua +} + +pub struct LuaHinter; + +impl Hinter for LuaHinter { + fn handle( + &mut self, + line: &str, + _pos: usize, + _history: &dyn History, + _use_ansi_coloring: bool, + _cwd: &str, + ) -> String { + let lua = burner_lua(); + + let value: LuaValue = match lua.load(line).set_name("=").eval() { + Ok(value) => value, + Err(LuaError::SyntaxError { message, .. }) => { + let message = message.split(":").last().unwrap().trim(); + let style = Style::new().fg(Color::Red).dimmed(); + + return style.paint(format!(" ({message})")).to_string(); + } + Err(_) => return String::new(), + }; + + if value.is_nil() { + return String::new(); + } + + let style = Style::new().fg(Color::DarkGray); + + style + .paint(format!(" ({})", display_basic(&value, false))) + .to_string() + } + + fn complete_hint(&self) -> String { + String::new() + } + + fn next_hint_token(&self) -> String { + String::new() + } +} diff --git a/src/main.rs b/src/main.rs @@ -16,6 +16,7 @@ use crate::{format::comfy_table, inspect::inspect}; mod editor; mod format; mod highlight; +mod hinter; mod inspect; mod validator; diff --git a/src/validator.rs b/src/validator.rs @@ -1,40 +1,13 @@ use mlua::prelude::*; -use nu_ansi_term::{Color, Style}; -use reedline::{Hinter, History, ValidationResult, Validator}; - -use crate::inspect::display_basic; +use reedline::{ValidationResult, Validator}; pub struct LuaValidator { lua: Lua, - hint: String, } impl LuaValidator { pub fn new() -> Self { - Self { - lua: Self::burner_lua(), - hint: String::new(), - } - } - - // this is a really bad way of doing things - fn burner_lua() -> Lua { - #[cfg(any(feature = "lua54", feature = "lua53"))] - let flags = LuaStdLib::MATH | LuaStdLib::STRING | LuaStdLib::UTF8; - #[cfg(not(any(feature = "lua54", feature = "lua53")))] - let flags = LuaStdLib::MATH | LuaStdLib::STRING; - - let lua = Lua::new_with(flags, LuaOptions::new()).unwrap(); - - let globals = lua.globals(); - globals.raw_remove("print").unwrap(); - globals.raw_remove("loadfile").unwrap(); - globals.raw_remove("load").unwrap(); - - let math: LuaTable = globals.get("math").unwrap(); - math.raw_remove("random").unwrap(); - - lua + Self { lua: Lua::new() } } } @@ -67,44 +40,3 @@ impl Validator for LuaValidator { } } } - -impl Hinter for LuaValidator { - fn handle( - &mut self, - line: &str, - _pos: usize, - _history: &dyn History, - _use_ansi_coloring: bool, - _cwd: &str, - ) -> String { - let lua = Self::burner_lua(); - - let value: LuaValue = match lua.load(line).set_name("=").eval() { - Ok(value) => value, - Err(LuaError::SyntaxError { message, .. }) => { - let message = message.split(":").last().unwrap().trim(); - let style = Style::new().fg(Color::Red).dimmed(); - - return style.paint(format!(" ({message})")).to_string(); - } - Err(_) => return String::new(), - }; - - if value.is_nil() { - return String::new(); - } - - self.hint = display_basic(&value, false); - let style = Style::new().fg(Color::DarkGray); - - style.paint(format!(" ({})", &self.hint)).to_string() - } - - fn complete_hint(&self) -> String { - String::new() - } - - fn next_hint_token(&self) -> String { - String::new() - } -}