fix: dedent documentation correctly (#132)

This commit is contained in:
Myriad-Dreamin 2024-03-30 19:04:07 +08:00 committed by GitHub
parent c32e6e3097
commit b76e80bad3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 2 deletions

View file

@ -0,0 +1,11 @@
/// Docs for f.
///
/// ```typst
/// #show raw: it => it {
/// it
/// }
/// ```
///
#let /* ident after */ f(a) = {
show it: it => it;
};

View file

@ -0,0 +1,12 @@
---
source: crates/tinymist-query/src/analysis.rs
expression: result
input_file: crates/tinymist-query/src/fixtures/docs/raw.typ
---
Docs for f.
```typst
#show raw: it => it {
it
}
```

View file

@ -38,7 +38,7 @@ fn extract_document_between(node: &LinkedNode, rng: Range<usize>) -> Option<Stri
// comments.push(n.text().strip_prefix("//")?.trim().to_owned());
// strip all slash prefix
let text = n.text().trim_start_matches('/');
comments.push(text.trim().to_owned());
comments.push(text.to_owned());
continue;
}
SyntaxKind::BlockComment => {
@ -61,7 +61,18 @@ fn extract_document_between(node: &LinkedNode, rng: Range<usize>) -> Option<Stri
return None;
}
Some(comments.join("\n"))
let dedent = comments.iter().fold(usize::MAX, |acc, c| {
let indent = c.chars().take_while(|c| c.is_whitespace()).count();
acc.min(indent)
});
let docs = comments
.iter()
.map(|c| c.chars().skip(dedent).collect::<String>())
.collect::<Vec<_>>()
.join("\n");
Some(docs)
}
pub fn find_document_before(src: &Source, cursor: usize) -> Option<String> {