Skip to content

Commit 5734b34

Browse files
committed
Fix incorrect newline emission in Attrs::docs
1 parent ec824a9 commit 5734b34

File tree

4 files changed

+80
-62
lines changed

4 files changed

+80
-62
lines changed

crates/hir_def/src/attr.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl ops::Deref for Attrs {
7676
impl RawAttrs {
7777
pub(crate) const EMPTY: Self = Self { entries: None };
7878

79-
pub(crate) fn new(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Self {
79+
pub(crate) fn new(owner: &dyn ast::AttrsOwner, hygiene: &Hygiene) -> Self {
8080
let entries = collect_attrs(owner)
8181
.enumerate()
8282
.flat_map(|(i, attr)| match attr {
@@ -92,7 +92,7 @@ impl RawAttrs {
9292
Self { entries: if entries.is_empty() { None } else { Some(entries) } }
9393
}
9494

95-
fn from_attrs_owner(db: &dyn DefDatabase, owner: InFile<&dyn AttrsOwner>) -> Self {
95+
fn from_attrs_owner(db: &dyn DefDatabase, owner: InFile<&dyn ast::AttrsOwner>) -> Self {
9696
let hygiene = Hygiene::new(db.upcast(), owner.file_id);
9797
Self::new(owner.value, &hygiene)
9898
}
@@ -178,7 +178,7 @@ impl Attrs {
178178
Some(it) => {
179179
let raw_attrs = RawAttrs::from_attrs_owner(
180180
db,
181-
it.as_ref().map(|it| it as &dyn AttrsOwner),
181+
it.as_ref().map(|it| it as &dyn ast::AttrsOwner),
182182
);
183183
match mod_data.definition_source(db) {
184184
InFile { file_id, value: ModuleSource::SourceFile(file) } => raw_attrs
@@ -189,9 +189,9 @@ impl Attrs {
189189
None => RawAttrs::from_attrs_owner(
190190
db,
191191
mod_data.definition_source(db).as_ref().map(|src| match src {
192-
ModuleSource::SourceFile(file) => file as &dyn AttrsOwner,
193-
ModuleSource::Module(module) => module as &dyn AttrsOwner,
194-
ModuleSource::BlockExpr(block) => block as &dyn AttrsOwner,
192+
ModuleSource::SourceFile(file) => file as &dyn ast::AttrsOwner,
193+
ModuleSource::Module(module) => module as &dyn ast::AttrsOwner,
194+
ModuleSource::BlockExpr(block) => block as &dyn ast::AttrsOwner,
195195
}),
196196
),
197197
}
@@ -249,7 +249,7 @@ impl Attrs {
249249
let mut res = ArenaMap::default();
250250

251251
for (id, var) in src.value.iter() {
252-
let attrs = RawAttrs::from_attrs_owner(db, src.with_value(var as &dyn AttrsOwner))
252+
let attrs = RawAttrs::from_attrs_owner(db, src.with_value(var as &dyn ast::AttrsOwner))
253253
.filter(db, krate);
254254

255255
res.insert(id, attrs)
@@ -283,7 +283,7 @@ impl Attrs {
283283
/// Constructs a map that maps the lowered `Attr`s in this `Attrs` back to its original syntax nodes.
284284
///
285285
/// `owner` must be the original owner of the attributes.
286-
pub fn source_map(&self, owner: &dyn AttrsOwner) -> AttrSourceMap {
286+
pub fn source_map(&self, owner: &dyn ast::AttrsOwner) -> AttrSourceMap {
287287
AttrSourceMap { attrs: collect_attrs(owner).collect() }
288288
}
289289

@@ -321,9 +321,7 @@ impl Attrs {
321321
let mut buf = String::new();
322322
for doc in docs {
323323
// str::lines doesn't yield anything for the empty string
324-
if doc.is_empty() {
325-
buf.push('\n');
326-
} else {
324+
if !doc.is_empty() {
327325
buf.extend(Itertools::intersperse(
328326
doc.lines().map(|line| {
329327
line.char_indices()
@@ -436,7 +434,7 @@ impl Attr {
436434
///
437435
/// Note that the returned syntax node might be a `#[cfg_attr]`, or a doc comment, instead of
438436
/// the attribute represented by `Attr`.
439-
pub fn to_src(&self, owner: &dyn AttrsOwner) -> Either<ast::Attr, ast::Comment> {
437+
pub fn to_src(&self, owner: &dyn ast::AttrsOwner) -> Either<ast::Attr, ast::Comment> {
440438
collect_attrs(owner).nth(self.index as usize).unwrap_or_else(|| {
441439
panic!("cannot find `Attr` at index {} in {}", self.index, owner.syntax())
442440
})
@@ -528,7 +526,7 @@ where
528526
N: ast::AttrsOwner,
529527
{
530528
let src = InFile::new(src.file_id, src.to_node(db.upcast()));
531-
RawAttrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner))
529+
RawAttrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn ast::AttrsOwner))
532530
}
533531

534532
fn attrs_from_item_tree<N: ItemTreeNode>(id: ItemTreeId<N>, db: &dyn DefDatabase) -> RawAttrs {
@@ -537,7 +535,9 @@ fn attrs_from_item_tree<N: ItemTreeNode>(id: ItemTreeId<N>, db: &dyn DefDatabase
537535
tree.raw_attrs(mod_item.into()).clone()
538536
}
539537

540-
fn collect_attrs(owner: &dyn AttrsOwner) -> impl Iterator<Item = Either<ast::Attr, ast::Comment>> {
538+
fn collect_attrs(
539+
owner: &dyn ast::AttrsOwner,
540+
) -> impl Iterator<Item = Either<ast::Attr, ast::Comment>> {
541541
let (inner_attrs, inner_docs) = inner_attributes(owner.syntax())
542542
.map_or((None, None), |(attrs, docs)| ((Some(attrs), Some(docs))));
543543

crates/ide/src/hover.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -1533,12 +1533,21 @@ fn my() {}
15331533
fn test_hover_struct_doc_comment() {
15341534
check(
15351535
r#"
1536-
/// bar docs
1536+
/// This is an example
1537+
/// multiline doc
1538+
///
1539+
/// # Example
1540+
///
1541+
/// ```
1542+
/// let five = 5;
1543+
///
1544+
/// assert_eq!(6, my_crate::add_one(5));
1545+
/// ```
15371546
struct Bar;
15381547
15391548
fn foo() { let bar = Ba$0r; }
15401549
"#,
1541-
expect![[r#"
1550+
expect![[r##"
15421551
*Bar*
15431552
15441553
```rust
@@ -1551,8 +1560,17 @@ fn foo() { let bar = Ba$0r; }
15511560
15521561
---
15531562
1554-
bar docs
1555-
"#]],
1563+
This is an example
1564+
multiline doc
1565+
1566+
# Example
1567+
1568+
```
1569+
let five = 5;
1570+
1571+
assert_eq!(6, my_crate::add_one(5));
1572+
```
1573+
"##]],
15561574
);
15571575
}
15581576

crates/ide_db/src/call_info.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ pub fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo>
5353

5454
match callable.kind() {
5555
hir::CallableKind::Function(func) => {
56-
res.doc = func.docs(db).map(|it| it.as_str().to_string());
56+
res.doc = func.docs(db).map(|it| it.into());
5757
format_to!(res.signature, "fn {}", func.name(db));
5858
}
5959
hir::CallableKind::TupleStruct(strukt) => {
60-
res.doc = strukt.docs(db).map(|it| it.as_str().to_string());
60+
res.doc = strukt.docs(db).map(|it| it.into());
6161
format_to!(res.signature, "struct {}", strukt.name(db));
6262
}
6363
hir::CallableKind::TupleEnumVariant(variant) => {
64-
res.doc = variant.docs(db).map(|it| it.as_str().to_string());
64+
res.doc = variant.docs(db).map(|it| it.into());
6565
format_to!(
6666
res.signature,
6767
"enum {}::{}",

crates/ide_db/src/call_info/tests.rs

+41-41
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ fn bar() {
220220
}
221221
"#,
222222
expect![[r#"
223-
test
224-
------
225-
fn foo(j: u32) -> u32
226-
(<j: u32>)
227-
"#]],
223+
test
224+
------
225+
fn foo(j: u32) -> u32
226+
(<j: u32>)
227+
"#]],
228228
);
229229
}
230230

@@ -249,19 +249,19 @@ pub fn do() {
249249
add_one($0
250250
}"#,
251251
expect![[r##"
252-
Adds one to the number given.
252+
Adds one to the number given.
253253
254-
# Examples
254+
# Examples
255255
256-
```
257-
let five = 5;
256+
```
257+
let five = 5;
258258
259-
assert_eq!(6, my_crate::add_one(5));
260-
```
261-
------
262-
fn add_one(x: i32) -> i32
263-
(<x: i32>)
264-
"##]],
259+
assert_eq!(6, my_crate::add_one(5));
260+
```
261+
------
262+
fn add_one(x: i32) -> i32
263+
(<x: i32>)
264+
"##]],
265265
);
266266
}
267267

@@ -291,19 +291,19 @@ pub fn do_it() {
291291
}
292292
"#,
293293
expect![[r##"
294-
Adds one to the number given.
294+
Adds one to the number given.
295295
296-
# Examples
296+
# Examples
297297
298-
```
299-
let five = 5;
298+
```
299+
let five = 5;
300300
301-
assert_eq!(6, my_crate::add_one(5));
302-
```
303-
------
304-
fn add_one(x: i32) -> i32
305-
(<x: i32>)
306-
"##]],
301+
assert_eq!(6, my_crate::add_one(5));
302+
```
303+
------
304+
fn add_one(x: i32) -> i32
305+
(<x: i32>)
306+
"##]],
307307
);
308308
}
309309

@@ -335,13 +335,13 @@ pub fn foo(mut r: WriteHandler<()>) {
335335
}
336336
"#,
337337
expect![[r#"
338-
Method is called when writer finishes.
338+
Method is called when writer finishes.
339339
340-
By default this method stops actor's `Context`.
341-
------
342-
fn finished(&mut self, ctx: &mut {unknown})
343-
(<ctx: &mut {unknown}>)
344-
"#]],
340+
By default this method stops actor's `Context`.
341+
------
342+
fn finished(&mut self, ctx: &mut {unknown})
343+
(<ctx: &mut {unknown}>)
344+
"#]],
345345
);
346346
}
347347

@@ -389,11 +389,11 @@ fn main() {
389389
}
390390
"#,
391391
expect![[r#"
392-
A cool tuple struct
393-
------
394-
struct S(u32, i32)
395-
(u32, <i32>)
396-
"#]],
392+
A cool tuple struct
393+
------
394+
struct S(u32, i32)
395+
(u32, <i32>)
396+
"#]],
397397
);
398398
}
399399

@@ -431,11 +431,11 @@ fn main() {
431431
}
432432
"#,
433433
expect![[r#"
434-
A Variant
435-
------
436-
enum E::A(i32)
437-
(<i32>)
438-
"#]],
434+
A Variant
435+
------
436+
enum E::A(i32)
437+
(<i32>)
438+
"#]],
439439
);
440440
}
441441

0 commit comments

Comments
 (0)