use crate::{editor::Editor, selector::Selector, state::SemanticEditTools};
use anyhow::{anyhow, Result};
use mcplease::{
traits::{Tool, WithExamples},
types::Example,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, JsonSchema, clap::Args)]
#[serde(rename = "retarget_edit")]
#[group(skip)]
pub struct RetargetEdit {
#[serde(flatten)]
#[clap(flatten)]
pub selector: Selector,
}
impl WithExamples for RetargetEdit {
fn examples() -> Vec<Example<Self>> {
vec![
]
}
}
impl Tool<SemanticEditTools> for RetargetEdit {
fn execute(self, state: &mut SemanticEditTools) -> Result<String> {
let Self { selector } = self;
let staged_operation = state
.modify_staged_operation(None, |op| op.retarget(selector))?
.ok_or_else(|| anyhow!("no operation staged"))?;
let editor =
Editor::from_staged_operation(staged_operation.clone(), state.language_registry())?;
let (message, staged_operation) = editor.preview()?;
if staged_operation.is_some() {
state.preview_edit(None, staged_operation)?;
}
Ok(message)
}
}