commit ff1ce2f8495c5aaf6d3538683f45c6f0618052b1
parent cc2d431d1d966fcc45578fc7a01c8f6d4b7ac51c
Author: Sylvia Ivory <git@sivory.net>
Date: Fri, 20 Jun 2025 23:23:35 -0700
Begin autocomplete functionality
Diffstat:
2 files changed, 66 insertions(+), 5 deletions(-)
diff --git a/src/editor.rs b/src/editor.rs
@@ -2,8 +2,9 @@ use mlua::prelude::*;
use reedline::{DefaultPrompt, DefaultPromptSegment, Reedline, Signal};
use crate::{
- format::{lua_to_string, TableFormat},
- highlight::LuaHighlighter, validator::LuaValidator,
+ format::{TableFormat, lua_to_string},
+ highlight::LuaHighlighter,
+ validator::LuaValidator,
};
pub struct Editor {
@@ -26,7 +27,8 @@ impl Editor {
let editor = Reedline::create()
.with_highlighter(Box::new(LuaHighlighter::new()))
- .with_validator(Box::new(LuaValidator::new()));
+ .with_validator(Box::new(LuaValidator::new()))
+ .with_hinter(Box::new(LuaValidator::new()));
Ok(Self {
prompt,
diff --git a/src/validator.rs b/src/validator.rs
@@ -1,13 +1,32 @@
use mlua::prelude::*;
-use reedline::{ValidationResult, Validator};
+use nu_ansi_term::{Color, Style};
+use reedline::{Hinter, History, ValidationResult, Validator};
pub struct LuaValidator {
lua: Lua,
+ hint: String,
}
impl LuaValidator {
pub fn new() -> Self {
- Self { lua: Lua::new() }
+ Self {
+ lua: Self::burner_lua(),
+ hint: String::new(),
+ }
+ }
+
+ fn burner_lua() -> Lua {
+ let lua = Lua::new_with(
+ LuaStdLib::MATH | LuaStdLib::STRING | LuaStdLib::UTF8,
+ LuaOptions::new(),
+ )
+ .unwrap();
+
+ let math: LuaTable = lua.globals().get("math").unwrap();
+
+ math.raw_remove("random").unwrap();
+
+ lua
}
}
@@ -19,3 +38,43 @@ 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 = if let Ok(value) = lua.load(line).eval() {
+ value
+ } else {
+ return String::new();
+ };
+
+ if value.is_nil() {
+ return String::new();
+ }
+
+ if let Ok(str) = value.to_string() {
+ self.hint = str.clone();
+ let style = Style::new().fg(Color::DarkGray);
+
+ style.paint(format!(" ({str})")).to_string()
+ } else {
+ String::new()
+ }
+ }
+
+ fn complete_hint(&self) -> String {
+ String::new()
+ }
+
+ fn next_hint_token(&self) -> String {
+ String::new()
+ }
+}