* feat(sidebar): supports select files chore (context) update add type annotations to context functions chore (sidebar) remove unused notify function call refactor (sidebar) remove setting search file to file path chore (sidebar) remove nvim_notify debugging api call * feat (files) allow selecting a file by string via cmp suggestion menu * chore (context) refactor to allow context using @file with a context view * refactor (context) refactor seletected file types as an array of path and content * refactor (config) remove unused configuration options * refactor (sidebar) remove unused unbild key * refactor (context) remove unused imports * refactor (mentions) update mentions to support items with callback functions and removal of the underlying selection. * fix (sidebar) add file context as a window that is visitable via the tab key * refactor (file_content) remove file content as an input to llm * feat (sidebar) support suggesting and applying code in all languages that are in the context * feat (sidebar) configurable mapping for removing a file from the context. * feat (context_view) configure hints for the context view for adding and deleting a file. * feat (context) add hints for the context view. * fix (sidebar) type when scrolling the results buffer. * refactor (selected files) refactor llm stream to accept an array of selected file metadata * refactor: context => selected_files --------- Co-authored-by: yetone <yetoneful@gmail.com>
98 lines
2.8 KiB
Rust
98 lines
2.8 KiB
Rust
use minijinja::{context, path_loader, Environment};
|
|
use mlua::prelude::*;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
struct State<'a> {
|
|
environment: Mutex<Option<Environment<'a>>>,
|
|
}
|
|
|
|
impl<'a> State<'a> {
|
|
fn new() -> Self {
|
|
State {
|
|
environment: Mutex::new(None),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
struct SelectedFile {
|
|
path: String,
|
|
content: String,
|
|
file_type: String,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
struct TemplateContext {
|
|
use_xml_format: bool,
|
|
ask: bool,
|
|
code_lang: String,
|
|
selected_files: Vec<SelectedFile>,
|
|
selected_code: Option<String>,
|
|
project_context: Option<String>,
|
|
diagnostics: Option<String>,
|
|
}
|
|
|
|
// Given the file name registered after add, the context table in Lua, resulted in a formatted
|
|
// Lua string
|
|
#[allow(clippy::needless_pass_by_value)]
|
|
fn render(state: &State, template: &str, context: TemplateContext) -> LuaResult<String> {
|
|
let environment = state.environment.lock().unwrap();
|
|
match environment.as_ref() {
|
|
Some(environment) => {
|
|
let jinja_template = environment
|
|
.get_template(template)
|
|
.map_err(LuaError::external)
|
|
.unwrap();
|
|
|
|
Ok(jinja_template
|
|
.render(context! {
|
|
use_xml_format => context.use_xml_format,
|
|
ask => context.ask,
|
|
code_lang => context.code_lang,
|
|
selected_files => context.selected_files,
|
|
selected_code => context.selected_code,
|
|
project_context => context.project_context,
|
|
diagnostics => context.diagnostics,
|
|
})
|
|
.map_err(LuaError::external)
|
|
.unwrap())
|
|
}
|
|
None => Err(LuaError::RuntimeError(
|
|
"Environment not initialized".to_string(),
|
|
)),
|
|
}
|
|
}
|
|
|
|
fn initialize(state: &State, directory: String) {
|
|
let mut environment_mutex = state.environment.lock().unwrap();
|
|
// add directory as a base path for base directory template path
|
|
let mut env = Environment::new();
|
|
env.set_loader(path_loader(directory));
|
|
*environment_mutex = Some(env);
|
|
}
|
|
|
|
#[mlua::lua_module]
|
|
fn avante_templates(lua: &Lua) -> LuaResult<LuaTable> {
|
|
let core = State::new();
|
|
let state = Arc::new(core);
|
|
let state_clone = Arc::clone(&state);
|
|
|
|
let exports = lua.create_table()?;
|
|
exports.set(
|
|
"initialize",
|
|
lua.create_function(move |_, model: String| {
|
|
initialize(&state, model);
|
|
Ok(())
|
|
})?,
|
|
)?;
|
|
exports.set(
|
|
"render",
|
|
lua.create_function_mut(move |lua, (template, context): (String, LuaValue)| {
|
|
let ctx = lua.from_value(context)?;
|
|
render(&state_clone, template.as_str(), ctx)
|
|
})?,
|
|
)?;
|
|
Ok(exports)
|
|
}
|