Fix: #2604, Fix: #2240, Fix: #2341, Fix #1355 - Better error handling and and spaces handling in autofmt (#2736)
* add new autofmt sample * Feat: implement rustfmt::skip support for rsx * generally improve error handling with better expect messages * wip: nested rsx formatting and expression formatting * nested rsx formatting works * collapse autofmt crate * cast indent through macros * use proper whitespace * no more eating comments! * Use proper error handling
This commit is contained in:
1 parent
ef0202f999
commit
828cc502f1
47 files changed
+1621
-1055
No files matched your search
@@ -3,37 +3,66 @@
|
||||
//! Returns all macros that match a pattern. You can use this information to autoformat them later
|
||||
|
||||
use proc_macro2::LineColumn;
|
||||
use syn::{visit::Visit, File, Macro};
|
||||
use syn::{visit::Visit, File, Macro, Meta};
|
||||
|
||||
type CollectedMacro<'a> = &'a Macro;
|
||||
|
||||
pub fn collect_from_file(file: &File) -> Vec<CollectedMacro<'_>> {
|
||||
let mut macros = vec![];
|
||||
MacroCollector::visit_file(
|
||||
&mut MacroCollector {
|
||||
macros: &mut macros,
|
||||
},
|
||||
file,
|
||||
);
|
||||
let mut collector = MacroCollector::new(&mut macros);
|
||||
MacroCollector::visit_file(&mut collector, file);
|
||||
macros
|
||||
}
|
||||
|
||||
struct MacroCollector<'a, 'b> {
|
||||
macros: &'a mut Vec<CollectedMacro<'b>>,
|
||||
skip_count: usize,
|
||||
}
|
||||
|
||||
impl<'a, 'b> MacroCollector<'a, 'b> {
|
||||
fn new(macros: &'a mut Vec<CollectedMacro<'b>>) -> Self {
|
||||
Self {
|
||||
macros,
|
||||
skip_count: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b> Visit<'b> for MacroCollector<'a, 'b> {
|
||||
fn visit_macro(&mut self, i: &'b Macro) {
|
||||
if let Some("rsx" | "render") = i
|
||||
.path
|
||||
.segments
|
||||
.last()
|
||||
.map(|i| i.ident.to_string())
|
||||
.as_deref()
|
||||
{
|
||||
self.macros.push(i)
|
||||
// Visit the regular stuff - this will also ensure paths/attributes are visited
|
||||
syn::visit::visit_macro(self, i);
|
||||
|
||||
let name = &i.path.segments.last().map(|i| i.ident.to_string());
|
||||
if let Some("rsx" | "render") = name.as_deref() {
|
||||
if self.skip_count == 0 {
|
||||
self.macros.push(i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// attributes can occur on stmts and items - we need to make sure the stack is reset when we exit
|
||||
// this means we save the skipped length and set it back to its original length
|
||||
fn visit_stmt(&mut self, i: &'b syn::Stmt) {
|
||||
let skipped_len = self.skip_count;
|
||||
syn::visit::visit_stmt(self, i);
|
||||
self.skip_count = skipped_len;
|
||||
}
|
||||
|
||||
fn visit_item(&mut self, i: &'b syn::Item) {
|
||||
let skipped_len = self.skip_count;
|
||||
syn::visit::visit_item(self, i);
|
||||
self.skip_count = skipped_len;
|
||||
}
|
||||
|
||||
fn visit_attribute(&mut self, i: &'b syn::Attribute) {
|
||||
// we need to communicate that this stmt is skipped up the tree
|
||||
if attr_is_rustfmt_skip(i) {
|
||||
self.skip_count += 1;
|
||||
}
|
||||
|
||||
syn::visit::visit_attribute(self, i);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn byte_offset(input: &str, location: LineColumn) -> usize {
|
||||
@@ -49,10 +78,32 @@ pub fn byte_offset(input: &str, location: LineColumn) -> usize {
|
||||
.sum::<usize>()
|
||||
}
|
||||
|
||||
/// Check if an attribute is a rustfmt skip attribute
|
||||
fn attr_is_rustfmt_skip(i: &syn::Attribute) -> bool {
|
||||
match &i.meta {
|
||||
Meta::Path(path) => {
|
||||
path.segments.len() == 2
|
||||
&& matches!(i.style, syn::AttrStyle::Outer)
|
||||
&& path.segments[0].ident == "rustfmt"
|
||||
&& path.segments[1].ident == "skip"
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_file_and_collects_rsx_macros() {
|
||||
let contents = include_str!("../tests/samples/long.rsx");
|
||||
let parsed = syn::parse_file(contents).unwrap();
|
||||
let parsed = syn::parse_file(contents).expect("parse file okay");
|
||||
let macros = collect_from_file(&parsed);
|
||||
assert_eq!(macros.len(), 3);
|
||||
}
|
||||
|
||||
/// Ensure that we only collect non-skipped macros
|
||||
#[test]
|
||||
fn dont_collect_skipped_macros() {
|
||||
let contents = include_str!("../tests/samples/skip.rsx");
|
||||
let parsed = syn::parse_file(contents).expect("parse file okay");
|
||||
let macros = collect_from_file(&parsed);
|
||||
assert_eq!(macros.len(), 2);
|
||||
}
|
||||
Reference in new issue
Block a user