|
7 | 7 | //! and highlight injection). |
8 | 8 |
|
9 | 9 | use std::{ |
| 10 | + borrow::Cow, |
10 | 11 | convert::Infallible, |
11 | 12 | ops::{ControlFlow, Range}, |
12 | 13 | }; |
@@ -222,9 +223,12 @@ impl Docs { |
222 | 223 | fn extend_with_doc_attr(&mut self, value: ast::String, indent: &mut Indent) { |
223 | 224 | let Some(value_offset) = value.text_range_between_quotes() else { return }; |
224 | 225 | let value_offset = value_offset.start(); |
225 | | - let Ok(value) = value.value() else { return }; |
226 | | - // FIXME: Handle source maps for escaped text. |
227 | | - self.extend_with_doc_str(&value, value_offset, DocCommentKind::Desugared, indent); |
| 226 | + let Ok(unescaped) = value.value() else { return }; |
| 227 | + // `value()` borrows the source text only when the string has no escapes. With escapes, |
| 228 | + // offsets in the unescaped text don't match offsets in the source, so leave it unmapped. |
| 229 | + // FIXME: Map the text between escapes. |
| 230 | + let ast_offset = matches!(unescaped, Cow::Borrowed(_)).then_some(value_offset); |
| 231 | + self.push_doc_lines(&unescaped, ast_offset, DocCommentKind::Desugared, indent); |
228 | 232 | } |
229 | 233 |
|
230 | 234 | fn extend_with_doc_str( |
@@ -791,7 +795,7 @@ pub(crate) fn extract_docs<'a, 'db>( |
791 | 795 | mod tests { |
792 | 796 | use expect_test::expect; |
793 | 797 | use hir_expand::InFile; |
794 | | - use syntax::{AstNode, ast}; |
| 798 | + use syntax::{AstNode, AstToken, ast}; |
795 | 799 | use test_fixture::WithFixture; |
796 | 800 | use thin_vec::ThinVec; |
797 | 801 | use tt::{TextRange, TextSize}; |
@@ -1079,4 +1083,54 @@ mod tests { |
1079 | 1083 | assert_eq!(mapped(0, 3), Some((in_file(range(7, 10)), IsInnerDoc::No))); |
1080 | 1084 | assert_eq!(mapped(4, 7), Some((in_file(range(14, 17)), IsInnerDoc::No))); |
1081 | 1085 | } |
| 1086 | + |
| 1087 | + /// Extracts the docs of the first string in `source`, as if it were a `#[doc = "..."]` value. |
| 1088 | + fn doc_attr_docs(source: &str) -> Docs { |
| 1089 | + let (_db, file_id) = TestDB::with_single_file(""); |
| 1090 | + let value = syntax::SourceFile::parse(source, span::Edition::CURRENT) |
| 1091 | + .syntax_node() |
| 1092 | + .descendants_with_tokens() |
| 1093 | + .filter_map(|it| it.into_token()) |
| 1094 | + .find_map(ast::String::cast) |
| 1095 | + .expect("no string in the fixture"); |
| 1096 | + let mut docs = Docs { |
| 1097 | + docs: String::new(), |
| 1098 | + docs_source_map: Vec::new(), |
| 1099 | + outline_mod: None, |
| 1100 | + inline_file: file_id.into(), |
| 1101 | + prefix_len: TextSize::new(0), |
| 1102 | + inline_inner_docs_start: None, |
| 1103 | + outline_inner_docs_start: None, |
| 1104 | + macro_calls: ThinVec::new(), |
| 1105 | + }; |
| 1106 | + let mut indent = Indent::default(); |
| 1107 | + docs.extend_with_doc_attr(value, &mut indent); |
| 1108 | + docs.remove_indent(&indent); |
| 1109 | + docs.remove_last_newline(); |
| 1110 | + docs |
| 1111 | + } |
| 1112 | + |
| 1113 | + #[test] |
| 1114 | + fn doc_attr_source_map() { |
| 1115 | + let range = |start, end| TextRange::new(TextSize::new(start), TextSize::new(end)); |
| 1116 | + let mapped = |docs: &Docs, start, end| { |
| 1117 | + docs.find_ast_range(range(start, end)).map(|(range, _)| range.value) |
| 1118 | + }; |
| 1119 | + |
| 1120 | + // No escapes: `bar` maps back to the source. |
| 1121 | + let docs = doc_attr_docs(r#"#[doc = "foo bar"] fn f() {}"#); |
| 1122 | + assert_eq!(docs.docs, "foo bar"); |
| 1123 | + assert_eq!(mapped(&docs, 4, 7), Some(range(13, 16))); |
| 1124 | + |
| 1125 | + // Raw strings have no escapes either. |
| 1126 | + let docs = doc_attr_docs(r##"#[doc = r"a\b"] fn f() {}"##); |
| 1127 | + assert_eq!(docs.docs, r"a\b"); |
| 1128 | + assert_eq!(mapped(&docs, 2, 3), Some(range(12, 13))); |
| 1129 | + |
| 1130 | + // With an escape, `b` is at offset 2 in the docs but at offset 3 in the source, |
| 1131 | + // so the docs can't be mapped back. |
| 1132 | + let docs = doc_attr_docs(r#"#[doc = "a\"b"] fn f() {}"#); |
| 1133 | + assert_eq!(docs.docs, "a\"b"); |
| 1134 | + assert_eq!(mapped(&docs, 2, 3), None); |
| 1135 | + } |
1082 | 1136 | } |
0 commit comments