commit ce9a64c79cf8df60c13aa397e9bd1d9879fc1e35
parent 63c65c28925f2c9a10364eaf3ce67a08380510a4
Author: Sylvia Ivory <git@sivory.net>
Date: Fri, 20 Jun 2025 23:00:57 -0700
Add validator
Diffstat:
4 files changed, 28 insertions(+), 4 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
@@ -6,7 +6,7 @@ edition = "2024"
[dependencies]
color-eyre = "0.6.5"
comfy-table = "7.1.4"
-mlua = { version = "0.10.5", features = ["lua54", "vendored", "anyhow"] }
+mlua = { version = "0.10.5", features = ["lua54", "vendored", "anyhow", "send"] }
nu-ansi-term = "0.50.1"
reedline = "0.40.0"
tree-sitter-highlight = "0.25.6"
diff --git a/src/editor.rs b/src/editor.rs
@@ -2,8 +2,8 @@ use mlua::prelude::*;
use reedline::{DefaultPrompt, DefaultPromptSegment, Reedline, Signal};
use crate::{
- format::{TableFormat, lua_to_string},
- highlight::LuaHighlighter,
+ format::{lua_to_string, TableFormat},
+ highlight::LuaHighlighter, validator::LuaValidator,
};
pub struct Editor {
@@ -24,7 +24,9 @@ impl Editor {
DefaultPromptSegment::Empty,
);
- let editor = Reedline::create().with_highlighter(Box::new(LuaHighlighter::new()));
+ let editor = Reedline::create()
+ .with_highlighter(Box::new(LuaHighlighter::new()))
+ .with_validator(Box::new(LuaValidator::new()));
Ok(Self {
prompt,
diff --git a/src/main.rs b/src/main.rs
@@ -3,6 +3,7 @@ use editor::Editor;
mod editor;
mod format;
mod highlight;
+mod validator;
fn main() -> color_eyre::Result<()> {
Editor::new()?.run();
diff --git a/src/validator.rs b/src/validator.rs
@@ -0,0 +1,21 @@
+use mlua::prelude::*;
+use reedline::{ValidationResult, Validator};
+
+pub struct LuaValidator {
+ lua: Lua,
+}
+
+impl LuaValidator {
+ pub fn new() -> Self {
+ Self { lua: Lua::new() }
+ }
+}
+
+impl Validator for LuaValidator {
+ fn validate(&self, line: &str) -> ValidationResult {
+ match self.lua.load(line).into_function() {
+ Ok(_) => ValidationResult::Complete,
+ Err(_) => ValidationResult::Incomplete,
+ }
+ }
+}