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)]
#[serde(rename = "retarget_staged")]
pub struct RetargetStaged {
#[serde(flatten)]
pub selector: Selector,
}
impl WithExamples for RetargetStaged {
fn examples() -> Vec<Example<Self>> {
vec![
]
}
}
impl Tool<SemanticEditTools> for RetargetStaged {
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, state.language_registry())?;
let (message, staged_operation) = editor.preview()?;
if staged_operation.is_some() {
state.stage_operation(None, staged_operation)?;
}
Ok(message)
}
}