Skip to content

Commit 81f0108

Browse files
committed
Remove markdown module from rust-analyzer crate
1 parent 8eddc64 commit 81f0108

File tree

7 files changed

+188
-187
lines changed

7 files changed

+188
-187
lines changed

crates/hir-def/src/attr.rs

+1-3
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};
8+
use std::{hash::Hash, ops, slice::Iter as SliceIter};
99

1010
use base_db::CrateId;
1111
use cfg::{CfgExpr, CfgOptions};
@@ -251,7 +251,6 @@ impl Attrs {
251251
}
252252
}
253253

254-
use std::slice::Iter as SliceIter;
255254
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
256255
pub enum DocAtom {
257256
/// eg. `#[doc(hidden)]`
@@ -265,7 +264,6 @@ pub enum DocAtom {
265264

266265
// Adapted from `CfgExpr` parsing code
267266
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
268-
// #[cfg_attr(test, derive(derive_arbitrary::Arbitrary))]
269267
pub enum DocExpr {
270268
Invalid,
271269
/// eg. `#[doc(hidden)]`, `#[doc(alias = "x")]`

crates/ide-db/src/documentation.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Documentation attribute related utilties.
12
use either::Either;
23
use hir::{
34
db::{DefDatabase, HirDatabase},

crates/ide-db/src/rust_doc.rs

+169
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Rustdoc specific doc comment handling
22
3+
use crate::documentation::Documentation;
4+
35
// stripped down version of https://github.com/rust-lang/rust/blob/392ba2ba1a7d6c542d2459fb8133bebf62a4a423/src/librustdoc/html/markdown.rs#L810-L933
46
pub fn is_rust_fence(s: &str) -> bool {
57
let mut seen_rust_tags = false;
@@ -32,3 +34,170 @@ pub fn is_rust_fence(s: &str) -> bool {
3234

3335
!seen_other_tags || seen_rust_tags
3436
}
37+
38+
const RUSTDOC_FENCES: [&str; 2] = ["```", "~~~"];
39+
40+
pub fn format_docs(src: &Documentation) -> String {
41+
format_docs_(src.as_str())
42+
}
43+
44+
fn format_docs_(src: &str) -> String {
45+
let mut processed_lines = Vec::new();
46+
let mut in_code_block = false;
47+
let mut is_rust = false;
48+
49+
for mut line in src.lines() {
50+
if in_code_block && is_rust && code_line_ignored_by_rustdoc(line) {
51+
continue;
52+
}
53+
54+
if let Some(header) = RUSTDOC_FENCES.into_iter().find_map(|fence| line.strip_prefix(fence))
55+
{
56+
in_code_block ^= true;
57+
58+
if in_code_block {
59+
is_rust = is_rust_fence(header);
60+
61+
if is_rust {
62+
line = "```rust";
63+
}
64+
}
65+
}
66+
67+
if in_code_block {
68+
let trimmed = line.trim_start();
69+
if is_rust && trimmed.starts_with("##") {
70+
line = &trimmed[1..];
71+
}
72+
}
73+
74+
processed_lines.push(line);
75+
}
76+
processed_lines.join("\n")
77+
}
78+
79+
fn code_line_ignored_by_rustdoc(line: &str) -> bool {
80+
let trimmed = line.trim();
81+
trimmed == "#" || trimmed.starts_with("# ") || trimmed.starts_with("#\t")
82+
}
83+
84+
#[cfg(test)]
85+
mod tests {
86+
use super::*;
87+
88+
#[test]
89+
fn test_format_docs_adds_rust() {
90+
let comment = "```\nfn some_rust() {}\n```";
91+
assert_eq!(format_docs_(comment), "```rust\nfn some_rust() {}\n```");
92+
}
93+
94+
#[test]
95+
fn test_format_docs_handles_plain_text() {
96+
let comment = "```text\nthis is plain text\n```";
97+
assert_eq!(format_docs_(comment), "```text\nthis is plain text\n```");
98+
}
99+
100+
#[test]
101+
fn test_format_docs_handles_non_rust() {
102+
let comment = "```sh\nsupposedly shell code\n```";
103+
assert_eq!(format_docs_(comment), "```sh\nsupposedly shell code\n```");
104+
}
105+
106+
#[test]
107+
fn test_format_docs_handles_rust_alias() {
108+
let comment = "```ignore\nlet z = 55;\n```";
109+
assert_eq!(format_docs_(comment), "```rust\nlet z = 55;\n```");
110+
}
111+
112+
#[test]
113+
fn test_format_docs_handles_complex_code_block_attrs() {
114+
let comment = "```rust,no_run\nlet z = 55;\n```";
115+
assert_eq!(format_docs_(comment), "```rust\nlet z = 55;\n```");
116+
}
117+
118+
#[test]
119+
fn test_format_docs_handles_error_codes() {
120+
let comment = "```compile_fail,E0641\nlet b = 0 as *const _;\n```";
121+
assert_eq!(format_docs_(comment), "```rust\nlet b = 0 as *const _;\n```");
122+
}
123+
124+
#[test]
125+
fn test_format_docs_skips_comments_in_rust_block() {
126+
let comment =
127+
"```rust\n # skip1\n# skip2\n#stay1\nstay2\n#\n #\n # \n #\tskip3\n\t#\t\n```";
128+
assert_eq!(format_docs_(comment), "```rust\n#stay1\nstay2\n```");
129+
}
130+
131+
#[test]
132+
fn test_format_docs_does_not_skip_lines_if_plain_text() {
133+
let comment =
134+
"```text\n # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t\n```";
135+
assert_eq!(
136+
format_docs_(comment),
137+
"```text\n # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t\n```",
138+
);
139+
}
140+
141+
#[test]
142+
fn test_format_docs_keeps_comments_outside_of_rust_block() {
143+
let comment = " # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t";
144+
assert_eq!(format_docs_(comment), comment);
145+
}
146+
147+
#[test]
148+
fn test_format_docs_preserves_newlines() {
149+
let comment = "this\nis\nmultiline";
150+
assert_eq!(format_docs_(comment), comment);
151+
}
152+
153+
#[test]
154+
fn test_code_blocks_in_comments_marked_as_rust() {
155+
let comment = r#"```rust
156+
fn main(){}
157+
```
158+
Some comment.
159+
```
160+
let a = 1;
161+
```"#;
162+
163+
assert_eq!(
164+
format_docs_(comment),
165+
"```rust\nfn main(){}\n```\nSome comment.\n```rust\nlet a = 1;\n```"
166+
);
167+
}
168+
169+
#[test]
170+
fn test_code_blocks_in_comments_marked_as_text() {
171+
let comment = r#"```text
172+
filler
173+
text
174+
```
175+
Some comment.
176+
```
177+
let a = 1;
178+
```"#;
179+
180+
assert_eq!(
181+
format_docs_(comment),
182+
"```text\nfiller\ntext\n```\nSome comment.\n```rust\nlet a = 1;\n```"
183+
);
184+
}
185+
186+
#[test]
187+
fn test_format_docs_handles_escape_double_hashes() {
188+
let comment = r#"```rust
189+
let s = "foo
190+
## bar # baz";
191+
```"#;
192+
193+
assert_eq!(format_docs_(comment), "```rust\nlet s = \"foo\n# bar # baz\";\n```");
194+
}
195+
196+
#[test]
197+
fn test_format_docs_handles_double_hashes_non_rust() {
198+
let comment = r#"```markdown
199+
## A second-level heading
200+
```"#;
201+
assert_eq!(format_docs_(comment), "```markdown\n## A second-level heading\n```");
202+
}
203+
}

crates/ide/src/signature_help.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use hir::{AssocItem, GenericParam, HirDisplay, ModuleDef, PathResolution, Semant
88
use ide_db::{
99
active_parameter::{callable_for_node, generic_def_for_node},
1010
base_db::FilePosition,
11-
documentation::HasDocs,
11+
documentation::{Documentation, HasDocs},
1212
FxIndexMap,
1313
};
1414
use stdx::format_to;
@@ -27,7 +27,7 @@ use crate::RootDatabase;
2727
/// edited.
2828
#[derive(Debug)]
2929
pub struct SignatureHelp {
30-
pub doc: Option<String>,
30+
pub doc: Option<Documentation>,
3131
pub signature: String,
3232
pub active_parameter: Option<usize>,
3333
parameters: Vec<TextRange>,
@@ -178,19 +178,19 @@ fn signature_help_for_call(
178178
let mut fn_params = None;
179179
match callable.kind() {
180180
hir::CallableKind::Function(func) => {
181-
res.doc = func.docs(db).map(|it| it.into());
181+
res.doc = func.docs(db);
182182
format_to!(res.signature, "fn {}", func.name(db).display(db));
183183
fn_params = Some(match callable.receiver_param(db) {
184184
Some(_self) => func.params_without_self(db),
185185
None => func.assoc_fn_params(db),
186186
});
187187
}
188188
hir::CallableKind::TupleStruct(strukt) => {
189-
res.doc = strukt.docs(db).map(|it| it.into());
189+
res.doc = strukt.docs(db);
190190
format_to!(res.signature, "struct {}", strukt.name(db).display(db));
191191
}
192192
hir::CallableKind::TupleEnumVariant(variant) => {
193-
res.doc = variant.docs(db).map(|it| it.into());
193+
res.doc = variant.docs(db);
194194
format_to!(
195195
res.signature,
196196
"enum {}::{}",
@@ -264,38 +264,38 @@ fn signature_help_for_generics(
264264
let db = sema.db;
265265
match generics_def {
266266
hir::GenericDef::Function(it) => {
267-
res.doc = it.docs(db).map(|it| it.into());
267+
res.doc = it.docs(db);
268268
format_to!(res.signature, "fn {}", it.name(db).display(db));
269269
}
270270
hir::GenericDef::Adt(hir::Adt::Enum(it)) => {
271-
res.doc = it.docs(db).map(|it| it.into());
271+
res.doc = it.docs(db);
272272
format_to!(res.signature, "enum {}", it.name(db).display(db));
273273
}
274274
hir::GenericDef::Adt(hir::Adt::Struct(it)) => {
275-
res.doc = it.docs(db).map(|it| it.into());
275+
res.doc = it.docs(db);
276276
format_to!(res.signature, "struct {}", it.name(db).display(db));
277277
}
278278
hir::GenericDef::Adt(hir::Adt::Union(it)) => {
279-
res.doc = it.docs(db).map(|it| it.into());
279+
res.doc = it.docs(db);
280280
format_to!(res.signature, "union {}", it.name(db).display(db));
281281
}
282282
hir::GenericDef::Trait(it) => {
283-
res.doc = it.docs(db).map(|it| it.into());
283+
res.doc = it.docs(db);
284284
format_to!(res.signature, "trait {}", it.name(db).display(db));
285285
}
286286
hir::GenericDef::TraitAlias(it) => {
287-
res.doc = it.docs(db).map(|it| it.into());
287+
res.doc = it.docs(db);
288288
format_to!(res.signature, "trait {}", it.name(db).display(db));
289289
}
290290
hir::GenericDef::TypeAlias(it) => {
291-
res.doc = it.docs(db).map(|it| it.into());
291+
res.doc = it.docs(db);
292292
format_to!(res.signature, "type {}", it.name(db).display(db));
293293
}
294294
hir::GenericDef::Variant(it) => {
295295
// In paths, generics of an enum can be specified *after* one of its variants.
296296
// eg. `None::<u8>`
297297
// We'll use the signature of the enum, but include the docs of the variant.
298-
res.doc = it.docs(db).map(|it| it.into());
298+
res.doc = it.docs(db);
299299
let enum_ = it.parent_enum(db);
300300
format_to!(res.signature, "enum {}", enum_.name(db).display(db));
301301
generics_def = enum_.into();

crates/rust-analyzer/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ mod dispatch;
2626
mod global_state;
2727
mod line_index;
2828
mod main_loop;
29-
mod markdown;
3029
mod mem_docs;
3130
mod op_queue;
3231
mod reload;

crates/rust-analyzer/src/lsp/to_proto.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use ide::{
1313
RenameError, Runnable, Severity, SignatureHelp, SnippetEdit, SourceChange, StructureNodeKind,
1414
SymbolKind, TextEdit, TextRange, TextSize,
1515
};
16+
use ide_db::rust_doc::format_docs;
1617
use itertools::Itertools;
1718
use serde_json::to_value;
1819
use vfs::AbsPath;
@@ -105,7 +106,7 @@ pub(crate) fn diagnostic_severity(severity: Severity) -> lsp_types::DiagnosticSe
105106
}
106107

107108
pub(crate) fn documentation(documentation: Documentation) -> lsp_types::Documentation {
108-
let value = crate::markdown::format_docs(documentation.as_str());
109+
let value = format_docs(&documentation);
109110
let markup_content = lsp_types::MarkupContent { kind: lsp_types::MarkupKind::Markdown, value };
110111
lsp_types::Documentation::MarkupContent(markup_content)
111112
}
@@ -416,7 +417,7 @@ pub(crate) fn signature_help(
416417
let documentation = call_info.doc.filter(|_| config.docs).map(|doc| {
417418
lsp_types::Documentation::MarkupContent(lsp_types::MarkupContent {
418419
kind: lsp_types::MarkupKind::Markdown,
419-
value: crate::markdown::format_docs(&doc),
420+
value: format_docs(&doc),
420421
})
421422
});
422423

@@ -1531,7 +1532,7 @@ pub(crate) fn markup_content(
15311532
ide::HoverDocFormat::Markdown => lsp_types::MarkupKind::Markdown,
15321533
ide::HoverDocFormat::PlainText => lsp_types::MarkupKind::PlainText,
15331534
};
1534-
let value = crate::markdown::format_docs(markup.as_str());
1535+
let value = format_docs(&Documentation::new(markup.into()));
15351536
lsp_types::MarkupContent { kind, value }
15361537
}
15371538

0 commit comments

Comments
 (0)