Skip to content

Commit 0088f84

Browse files
committed
Allow several samples in "// Assist:" comments.
1 parent 580e5e2 commit 0088f84

File tree

6 files changed

+223
-42
lines changed

6 files changed

+223
-42
lines changed

crates/ide_assists/src/handlers/introduce_named_lifetime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ static ASSIST_LABEL: &str = "Introduce named lifetime";
3333
// }
3434
// }
3535
// ```
36-
// FIXME: How can we handle renaming any one of multiple anonymous lifetimes?
37-
// FIXME: should also add support for the case fun(f: &Foo) -> &$0Foo
3836
pub(crate) fn introduce_named_lifetime(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
37+
// FIXME: How can we handle renaming any one of multiple anonymous lifetimes?
38+
// FIXME: should also add support for the case fun(f: &Foo) -> &$0Foo
3939
let lifetime =
4040
ctx.find_node_at_offset::<ast::Lifetime>().filter(|lifetime| lifetime.text() == "'_")?;
4141
let lifetime_loc = lifetime.lifetime_ident_token()?.text_range();

crates/ide_assists/src/handlers/reorder_fields.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
2020
// struct Foo {foo: i32, bar: i32};
2121
// const test: Foo = Foo {foo: 1, bar: 0}
2222
// ```
23-
//
2423
pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
2524
let record = ctx
2625
.find_node_at_offset::<ast::RecordExpr>()

crates/ide_assists/src/handlers/reorder_impl.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists};
4444
// fn c() {}
4545
// }
4646
// ```
47-
//
4847
pub(crate) fn reorder_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
4948
let impl_ast = ctx.find_node_at_offset::<ast::Impl>()?;
5049
let items = impl_ast.assoc_item_list()?;

crates/ide_assists/src/handlers/sort_items.rs

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,77 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists};
1111

1212
// Assist: sort_items
1313
//
14+
// Sorts item members alphabetically: fields, enum variants and methods.
15+
//
16+
// ```
17+
// struct $0Foo { second: u32, first: String }
18+
// ```
19+
// ->
20+
// ```
21+
// struct Foo { first: String, second: u32 }
22+
// ```
23+
// ---
24+
// ```
25+
// trait $0Bar {
26+
// fn second(&self) -> u32;
27+
// fn first(&self) -> String;
28+
// }
29+
// ```
30+
// ->
31+
// ```
32+
// trait Bar {
33+
// fn first(&self) -> String;
34+
// fn second(&self) -> u32;
35+
// }
36+
// ```
37+
// ---
38+
// ```
39+
// struct Baz;
40+
// impl $0Baz {
41+
// fn second(&self) -> u32;
42+
// fn first(&self) -> String;
43+
// }
44+
// ```
45+
// ->
46+
// ```
47+
// struct Baz;
48+
// impl Baz {
49+
// fn first(&self) -> String;
50+
// fn second(&self) -> u32;
51+
// }
52+
// ```
53+
// ---
54+
// There is a difference between sorting enum variants:
55+
//
56+
// ```
57+
// en$0um Animal {
58+
// Dog(String, f64),
59+
// Cat { weight: f64, name: String },
60+
// }
61+
// ```
62+
// ->
63+
// ```
64+
// enum Animal {
65+
// // variants sorted
66+
// Cat { weight: f64, name: String },
67+
// Dog(String, f64),
68+
// }
69+
// ```
70+
// and sorting a single enum struct variant:
71+
//
72+
// ```
73+
// enum Animal {
74+
// Dog(String, f64),
75+
// Cat {$0 weight: f64, name: String },
76+
// }
77+
// ```
78+
// ->
79+
// ```
80+
// enum Animal {
81+
// Dog(String, f64),
82+
// Cat { name: String, weight: f64 }, // Cat fields sorted
83+
// }
84+
// ```
1485
pub(crate) fn sort_items(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
1586
if let Some(trait_ast) = ctx.find_node_at_offset::<ast::Trait>() {
1687
add_sort_methods_assist(acc, trait_ast.assoc_item_list()?)
@@ -155,7 +226,7 @@ t$0rait Bar {
155226
check_assist_not_applicable(
156227
sort_items,
157228
r#"
158-
struct Bar;
229+
struct Bar;
159230
$0impl Bar {
160231
}
161232
"#,
@@ -221,7 +292,7 @@ t$0rait Bar {
221292
check_assist_not_applicable(
222293
sort_items,
223294
r#"
224-
struct Bar;
295+
struct Bar;
225296
$0impl Bar {
226297
fn a() {}
227298
fn b() {}

crates/ide_assists/src/tests/generated.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,98 @@ fn main() {
15231523
)
15241524
}
15251525

1526+
#[test]
1527+
fn doctest_sort_items() {
1528+
check_doc_test(
1529+
"sort_items",
1530+
r#####"
1531+
struct $0Foo { second: u32, first: String }
1532+
"#####,
1533+
r#####"
1534+
struct Foo { first: String, second: u32 }
1535+
"#####,
1536+
)
1537+
}
1538+
1539+
#[test]
1540+
fn doctest_sort_items_1() {
1541+
check_doc_test(
1542+
"sort_items_1",
1543+
r#####"
1544+
trait $0Bar {
1545+
fn second(&self) -> u32;
1546+
fn first(&self) -> String;
1547+
}
1548+
"#####,
1549+
r#####"
1550+
trait Bar {
1551+
fn first(&self) -> String;
1552+
fn second(&self) -> u32;
1553+
}
1554+
"#####,
1555+
)
1556+
}
1557+
1558+
#[test]
1559+
fn doctest_sort_items_2() {
1560+
check_doc_test(
1561+
"sort_items_2",
1562+
r#####"
1563+
struct Baz;
1564+
impl $0Baz {
1565+
fn second(&self) -> u32;
1566+
fn first(&self) -> String;
1567+
}
1568+
"#####,
1569+
r#####"
1570+
struct Baz;
1571+
impl Baz {
1572+
fn first(&self) -> String;
1573+
fn second(&self) -> u32;
1574+
}
1575+
"#####,
1576+
)
1577+
}
1578+
1579+
#[test]
1580+
fn doctest_sort_items_3() {
1581+
check_doc_test(
1582+
"sort_items_3",
1583+
r#####"
1584+
en$0um Animal {
1585+
Dog(String, f64),
1586+
Cat { weight: f64, name: String },
1587+
}
1588+
"#####,
1589+
r#####"
1590+
enum Animal {
1591+
// variants sorted
1592+
Cat { weight: f64, name: String },
1593+
Dog(String, f64),
1594+
}
1595+
"#####,
1596+
)
1597+
}
1598+
1599+
#[test]
1600+
fn doctest_sort_items_4() {
1601+
check_doc_test(
1602+
"sort_items_4",
1603+
r#####"
1604+
enum Animal {
1605+
Dog(String, f64),
1606+
Cat {$0 weight: f64, name: String },
1607+
}
1608+
"#####,
1609+
r#####"
1610+
enum Animal {
1611+
Dog(String, f64),
1612+
Cat { name: String, weight: f64 }, // Cat fields sorted
1613+
}
1614+
"#####,
1615+
)
1616+
}
1617+
15261618
#[test]
15271619
fn doctest_split_import() {
15281620
check_doc_test(

crates/ide_assists/src/tests/sourcegen.rs

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use super::check_doc_test;
1616
"
1717
.to_string();
1818
for assist in assists.iter() {
19-
let test = format!(
19+
for (idx, section) in assist.sections.iter().enumerate() {
20+
let id = if idx == 0 { assist.id.clone() } else { format!("{}_{}", &assist.id, idx)};
21+
let test = format!(
2022
r######"
2123
#[test]
2224
fn doctest_{}() {{
@@ -27,13 +29,14 @@ r#####"
2729
{}"#####)
2830
}}
2931
"######,
30-
assist.id,
31-
assist.id,
32-
reveal_hash_comments(&assist.before),
33-
reveal_hash_comments(&assist.after)
34-
);
35-
36-
buf.push_str(&test)
32+
&id,
33+
&id,
34+
reveal_hash_comments(&section.before),
35+
reveal_hash_comments(&section.after)
36+
);
37+
38+
buf.push_str(&test)
39+
}
3740
}
3841
let buf = sourcegen::add_preamble("sourcegen_assists_docs", sourcegen::reformat(buf));
3942
sourcegen::ensure_file_contents(
@@ -55,14 +58,17 @@ r#####"
5558
fs::write(dst, contents).unwrap();
5659
}
5760
}
61+
#[derive(Debug)]struct Section {
62+
doc: String,
63+
before: String,
64+
after: String,
65+
}
5866

5967
#[derive(Debug)]
6068
struct Assist {
6169
id: String,
6270
___location: sourcegen::Location,
63-
doc: String,
64-
before: String,
65-
after: String,
71+
sections: Vec<Section>
6672
}
6773

6874
impl Assist {
@@ -89,22 +95,28 @@ impl Assist {
8995
"invalid assist id: {:?}",
9096
id
9197
);
92-
let mut lines = block.contents.iter();
93-
94-
let doc = take_until(lines.by_ref(), "```").trim().to_string();
95-
assert!(
96-
doc.chars().next().unwrap().is_ascii_uppercase() && doc.ends_with('.'),
97-
"\n\n{}: assist docs should be proper sentences, with capitalization and a full stop at the end.\n\n{}\n\n",
98-
id, doc,
99-
);
100-
101-
let before = take_until(lines.by_ref(), "```");
102-
103-
assert_eq!(lines.next().unwrap().as_str(), "->");
104-
assert_eq!(lines.next().unwrap().as_str(), "```");
105-
let after = take_until(lines.by_ref(), "```");
98+
let mut lines = block.contents.iter().peekable();
10699
let ___location = sourcegen::Location { file: path.to_path_buf(), line: block.line };
107-
acc.push(Assist { id, ___location, doc, before, after })
100+
let mut assist = Assist { id, ___location, sections: Vec::new() };
101+
102+
while lines.peek().is_some() {
103+
let doc = take_until(lines.by_ref(), "```").trim().to_string();
104+
assert!(
105+
(doc.chars().next().unwrap().is_ascii_uppercase() && doc.ends_with('.')) || assist.sections.len() > 0,
106+
"\n\n{}: assist docs should be proper sentences, with capitalization and a full stop at the end.\n\n{}\n\n",
107+
&assist.id, doc,
108+
);
109+
110+
let before = take_until(lines.by_ref(), "```");
111+
112+
assert_eq!(lines.next().unwrap().as_str(), "->");
113+
assert_eq!(lines.next().unwrap().as_str(), "```");
114+
let after = take_until(lines.by_ref(), "```");
115+
116+
assist.sections.push(Section{doc, before, after});
117+
}
118+
119+
acc.push(assist)
108120
}
109121
}
110122

@@ -123,13 +135,20 @@ impl Assist {
123135

124136
impl fmt::Display for Assist {
125137
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126-
let before = self.before.replace("$0", "┃"); // Unicode pseudo-graphics bar
127-
let after = self.after.replace("$0", "┃");
128-
writeln!(
138+
let _ = writeln!(
129139
f,
130140
"[discrete]\n=== `{}`
131-
**Source:** {}
141+
**Source:** {}",
142+
self.id,
143+
self.___location,
144+
);
132145

146+
for section in &self.sections {
147+
let before = section.before.replace("$0", "┃"); // Unicode pseudo-graphics bar
148+
let after = section.after.replace("$0", "┃");
149+
let _= writeln!(
150+
f,
151+
"
133152
{}
134153
135154
.Before
@@ -139,12 +158,13 @@ impl fmt::Display for Assist {
139158
.After
140159
```rust
141160
{}```",
142-
self.id,
143-
self.___location,
144-
self.doc,
145-
hide_hash_comments(&before),
146-
hide_hash_comments(&after)
147-
)
161+
section.doc,
162+
hide_hash_comments(&before),
163+
hide_hash_comments(&after)
164+
);
165+
}
166+
167+
Ok(())
148168
}
149169
}
150170

0 commit comments

Comments
 (0)