diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs
index d9294d93aff..0171d8a92a1 100644
--- a/crates/hir_def/src/attr.rs
+++ b/crates/hir_def/src/attr.rs
@@ -484,10 +484,10 @@ impl AttrsWithOwner {
let mut buf = String::new();
let mut mapping = Vec::new();
for (doc, idx) in docs {
- // str::lines doesn't yield anything for the empty string
if !doc.is_empty() {
- for line in doc.split('\n') {
- let line = line.trim_end();
+ let mut base_offset = 0;
+ for raw_line in doc.split('\n') {
+ let line = raw_line.trim_end();
let line_len = line.len();
let (offset, line) = match line.char_indices().nth(indent) {
Some((offset, _)) => (offset, &line[offset..]),
@@ -498,9 +498,13 @@ impl AttrsWithOwner {
mapping.push((
TextRange::new(buf_offset.try_into().ok()?, buf.len().try_into().ok()?),
idx,
- TextRange::new(offset.try_into().ok()?, line_len.try_into().ok()?),
+ TextRange::at(
+ (base_offset + offset).try_into().ok()?,
+ line_len.try_into().ok()?,
+ ),
));
buf.push('\n');
+ base_offset += raw_line.len() + 1;
}
} else {
buf.push('\n');
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html
index 638f42c2f08..8d83ba2065d 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html
@@ -142,6 +142,7 @@ It is beyond me why you'd use these when you got ///
```rust
let _ = example(&[1, 2, 3]);[`block_comments2`]
pub fn block_comments() {}
@@ -150,5 +151,6 @@ It is beyond me why you'd use these when you got ///
```rust
let _ = example(&[1, 2, 3]);[`block_comments`]
pub fn block_comments2() {}
\ No newline at end of file
diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs
index 17cc6334b9e..b6e952b0888 100644
--- a/crates/ide/src/syntax_highlighting/tests.rs
+++ b/crates/ide/src/syntax_highlighting/tests.rs
@@ -618,6 +618,7 @@ It is beyond me why you'd use these when you got ///
```rust
let _ = example(&[1, 2, 3]);
```
+[`block_comments2`] tests these with indentation
*/
pub fn block_comments() {}
@@ -626,6 +627,7 @@ pub fn block_comments() {}
```rust
let _ = example(&[1, 2, 3]);
```
+ [`block_comments`] tests these without indentation
*/
pub fn block_comments2() {}
"#