use ecow::eco_format;
use crate::analysis::CodeActionWorker;
use crate::prelude::*;
use crate::syntax::{InterpretMode, interpret_mode_at};
impl<'a> CodeActionWorker<'a> {
pub(super) fn figure_actions(&mut self, node: &LinkedNode) -> Option<()> {
match node.kind() {
SyntaxKind::FuncCall => self.figure_actions_for_func_call(node),
SyntaxKind::CodeBlock => self.figure_actions_for_code_block(node),
SyntaxKind::ContentBlock => self.figure_actions_for_content_block(node),
SyntaxKind::Raw => self.figure_actions_for_raw(node),
_ => None,
}
}
fn figure_actions_for_func_call(&mut self, node: &LinkedNode) -> Option<()> {
let call = node.cast::<ast::FuncCall>()?;
let callee = call.callee();
let func_name = match callee {
ast::Expr::Ident(ident) => ident.get().as_str(),
_ => return None,
};
if !matches!(func_name, "image" | "table" | "raw") {
return None;
}
self.wrap_in_figure(node, func_name, false)
}
fn figure_actions_for_code_block(&mut self, node: &LinkedNode) -> Option<()> {
self.wrap_in_figure(node, "code block", false)
}
fn figure_actions_for_content_block(&mut self, node: &LinkedNode) -> Option<()> {
self.wrap_in_figure(node, "content block", false)
}
fn figure_actions_for_raw(&mut self, node: &LinkedNode) -> Option<()> {
let needs_hash = matches!(
interpret_mode_at(Some(node.parent()?)),
InterpretMode::Markup | InterpretMode::Math
);
self.wrap_in_figure(node, "raw block", needs_hash)
}
fn wrap_in_figure(&mut self, node: &LinkedNode, func_name: &str, add_hash: bool) -> Option<()> {
let mode = if add_hash { "#" } else { "" };
let call_range = node.range();
let call_text = self.source.text().get(call_range.clone())?;
let caption_before = if self.ctx.analysis.extended_code_action {
eco_format!("{mode}figure(\n caption: [${{1:Caption}}],\n {call_text}\n)$0")
} else {
eco_format!("{mode}figure(\n caption: [Caption],\n {call_text}\n)$0")
};
let edit_before = self.local_edit(EcoSnippetTextEdit::new(
self.ctx.to_lsp_range(call_range.clone(), &self.source),
caption_before,
))?;
let action_before = CodeAction {
title: tinymist_l10n::t!(
"tinymist-query.code-action.wrapFigureCaption",
"Wrap {func_name} in figure with a caption",
func_name = func_name.into()
)
.to_string(),
kind: Some(CodeActionKind::REFACTOR_REWRITE),
edit: Some(edit_before),
..CodeAction::default()
};
self.actions.push(action_before);
Some(())
}
}