use std::cmp;
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, LitStr};
#[proc_macro]
pub fn dedent(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as LitStr);
let input_str = input.value();
let lines: Vec<&str> = input_str.lines().collect();
if lines.is_empty() {
return quote! { "" }.into();
}
let min_indent = lines
.iter()
.filter(|line| !line.trim().is_empty())
.map(|line| line.len() - line.trim_start().len())
.min()
.unwrap_or(0);
let mut result = String::new();
let mut first = true;
for line in lines {
if !first {
result.push('\n');
}
first = false;
if line.trim().is_empty() {
continue;
}
let start = cmp::min(min_indent, line.len() - line.trim_start().len());
result.push_str(&line[start..]);
}
let result = result.trim_matches('\n');
let result_str = LitStr::new(result, input.span());
quote! { #result_str }.into()
}