Update derive attibute span to start after opening '('

Fixes 4984

When parsing derive attributes we're only concerned about the traits
and comments listed between the opening and closing parentheses.

Derive attribute spans currently start at the '#'.

    Span starts here
    |
    v
    #[derive(...)]

After this update the derive spans start after the opening '('.

    Span starts here
             |
             V
    #[derive(...)]
This commit is contained in:
Yacin Tmimi 2021-09-23 22:43:03 -04:00 committed by Caleb Cartwright
parent f0f449d6ed
commit 40f4993c67
5 changed files with 54 additions and 1 deletions

View File

@ -13,6 +13,7 @@ use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting,
use crate::overflow;
use crate::rewrite::{Rewrite, RewriteContext};
use crate::shape::Shape;
use crate::source_map::SpanUtils;
use crate::types::{rewrite_path, PathContext};
use crate::utils::{count_newlines, mk_sp};
@ -116,7 +117,9 @@ fn format_derive(
|span| span.lo(),
|span| span.hi(),
|span| Some(context.snippet(*span).to_owned()),
attr.span.lo(),
// We update derive attribute spans to start after the opening '('
// This helps us focus parsing to just what's inside #[derive(...)]
context.snippet_provider.span_after(attr.span, "("),
attr.span.hi(),
false,
);

View File

@ -0,0 +1,2 @@
#[derive(/*Debug, */Clone)]
struct Foo;

View File

@ -0,0 +1,20 @@
#[derive(
/* ---------- Some really important comment that just had to go inside the derive --------- */
Debug, Clone, Eq, PartialEq,
)]
struct Foo {
a: i32,
b: T,
}
#[derive(
/*
Some really important comment that just had to go inside the derive.
Also had to be put over multiple lines
*/
Debug, Clone, Eq, PartialEq,
)]
struct Bar {
a: i32,
b: T,
}

View File

@ -0,0 +1,2 @@
#[derive(/*Debug, */ Clone)]
struct Foo;

View File

@ -0,0 +1,26 @@
#[derive(
/* ---------- Some really important comment that just had to go inside the derive --------- */
Debug,
Clone,
Eq,
PartialEq,
)]
struct Foo {
a: i32,
b: T,
}
#[derive(
/*
Some really important comment that just had to go inside the derive.
Also had to be put over multiple lines
*/
Debug,
Clone,
Eq,
PartialEq,
)]
struct Bar {
a: i32,
b: T,
}