Skip to content

Commit a543516

Browse files
committed
fix: handle escaped chars in doc comments
1 parent af1fd88 commit a543516

File tree

4 files changed

+69
-12
lines changed

4 files changed

+69
-12
lines changed

crates/hir-def/src/attr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub mod builtin;
55
#[cfg(test)]
66
mod tests;
77

8-
use std::{hash::Hash, ops, slice::Iter as SliceIter};
8+
use std::{borrow::Cow, hash::Hash, ops, slice::Iter as SliceIter};
99

1010
use base_db::CrateId;
1111
use cfg::{CfgExpr, CfgOptions};
@@ -573,6 +573,10 @@ impl<'attr> AttrQuery<'attr> {
573573
self.attrs().find_map(|attr| attr.string_value())
574574
}
575575

576+
pub fn string_value_unescape(self) -> Option<Cow<'attr, str>> {
577+
self.attrs().find_map(|attr| attr.string_value_unescape())
578+
}
579+
576580
pub fn exists(self) -> bool {
577581
self.attrs().next().is_some()
578582
}

crates/hir-def/src/nameres/collector.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,7 +1917,7 @@ impl ModCollector<'_, '_> {
19171917
}
19181918

19191919
fn collect_module(&mut self, module_id: FileItemTreeId<Mod>, attrs: &Attrs) {
1920-
let path_attr = attrs.by_key("path").string_value();
1920+
let path_attr = attrs.by_key("path").string_value_unescape();
19211921
let is_macro_use = attrs.by_key("macro_use").exists();
19221922
let module = &self.item_tree[module_id];
19231923
match &module.kind {
@@ -1931,7 +1931,8 @@ impl ModCollector<'_, '_> {
19311931
module_id,
19321932
);
19331933

1934-
let Some(mod_dir) = self.mod_dir.descend_into_definition(&module.name, path_attr)
1934+
let Some(mod_dir) =
1935+
self.mod_dir.descend_into_definition(&module.name, path_attr.as_deref())
19351936
else {
19361937
return;
19371938
};
@@ -1952,8 +1953,12 @@ impl ModCollector<'_, '_> {
19521953
ModKind::Outline => {
19531954
let ast_id = AstId::new(self.file_id(), module.ast_id);
19541955
let db = self.def_collector.db;
1955-
match self.mod_dir.resolve_declaration(db, self.file_id(), &module.name, path_attr)
1956-
{
1956+
match self.mod_dir.resolve_declaration(
1957+
db,
1958+
self.file_id(),
1959+
&module.name,
1960+
path_attr.as_deref(),
1961+
) {
19571962
Ok((file_id, is_mod_rs, mod_dir)) => {
19581963
let item_tree = db.file_item_tree(file_id.into());
19591964
let krate = self.def_collector.def_map.krate;

crates/hir-expand/src/attrs.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! A higher level attributes based on TokenTree, with also some shortcuts.
2-
use std::{fmt, ops};
2+
use std::{borrow::Cow, fmt, ops};
33

44
use base_db::CrateId;
55
use cfg::CfgExpr;
@@ -297,6 +297,20 @@ impl Attr {
297297
}
298298
}
299299

300+
pub fn string_value_unescape(&self) -> Option<Cow<'_, str>> {
301+
match self.input.as_deref()? {
302+
AttrInput::Literal(it) => match it.text.strip_prefix('r') {
303+
Some(it) => {
304+
it.trim_matches('#').strip_prefix('"')?.strip_suffix('"').map(Cow::Borrowed)
305+
}
306+
None => {
307+
it.text.strip_prefix('"')?.strip_suffix('"').and_then(unescape).map(Cow::Owned)
308+
}
309+
},
310+
_ => None,
311+
}
312+
}
313+
300314
/// #[path(ident)]
301315
pub fn single_ident_value(&self) -> Option<&tt::Ident> {
302316
match self.input.as_deref()? {
@@ -346,6 +360,39 @@ impl Attr {
346360
}
347361
}
348362

363+
fn unescape(s: &str) -> Option<String> {
364+
let mut res = String::with_capacity(s.len());
365+
let mut chars = s.chars();
366+
367+
while let Some(c) = chars.next() {
368+
if c == '\\' {
369+
match chars.next()? {
370+
'n' => res.push('\n'),
371+
'r' => res.push('\r'),
372+
't' => res.push('\t'),
373+
'\\' => res.push('\\'),
374+
'\'' => res.push('\''),
375+
'"' => res.push('"'),
376+
'0' => res.push('\0'),
377+
'x' => {
378+
let hex = chars.by_ref().take(2).collect::<String>();
379+
let c = u8::from_str_radix(&hex, 16).ok()?;
380+
res.push(c as char);
381+
}
382+
'u' => {
383+
let hex = chars.by_ref().take(4).collect::<String>();
384+
let c = u32::from_str_radix(&hex, 16).ok()?;
385+
res.push(char::from_u32(c)?);
386+
}
387+
_ => return None,
388+
}
389+
} else {
390+
res.push(c);
391+
}
392+
}
393+
Some(res)
394+
}
395+
349396
pub fn collect_attrs(
350397
owner: &dyn ast::HasAttrs,
351398
) -> impl Iterator<Item = (AttrId, Either<ast::Attr, ast::Comment>)> {

crates/ide-db/src/documentation.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ pub fn docs_with_rangemap(
9191
db: &dyn DefDatabase,
9292
attrs: &AttrsWithOwner,
9393
) -> Option<(Documentation, DocsRangeMap)> {
94-
let docs =
95-
attrs.by_key("doc").attrs().filter_map(|attr| attr.string_value().map(|s| (s, attr.id)));
94+
let docs = attrs
95+
.by_key("doc")
96+
.attrs()
97+
.filter_map(|attr| attr.string_value_unescape().map(|s| (s, attr.id)));
9698
let indent = doc_indent(attrs);
9799
let mut buf = String::new();
98100
let mut mapping = Vec::new();
@@ -132,7 +134,7 @@ pub fn docs_with_rangemap(
132134
}
133135

134136
pub fn docs_from_attrs(attrs: &hir::Attrs) -> Option<String> {
135-
let docs = attrs.by_key("doc").attrs().filter_map(|attr| attr.string_value());
137+
let docs = attrs.by_key("doc").attrs().filter_map(|attr| attr.string_value_unescape());
136138
let indent = doc_indent(attrs);
137139
let mut buf = String::new();
138140
for doc in docs {
@@ -270,10 +272,9 @@ fn doc_indent(attrs: &hir::Attrs) -> usize {
270272
attrs
271273
.by_key("doc")
272274
.attrs()
273-
.filter_map(|attr| attr.string_value())
275+
.filter_map(|attr| attr.string_value()) // no need to use unescape version here
274276
.flat_map(|s| s.lines())
275-
.filter(|line| !line.chars().all(|c| c.is_whitespace()))
276-
.map(|line| line.chars().take_while(|c| c.is_whitespace()).count())
277+
.filter_map(|line| line.chars().position(|c| !c.is_whitespace()))
277278
.min()
278279
.unwrap_or(0)
279280
}

0 commit comments

Comments
 (0)