From 2f62c0117a1d59a531f9c84fbdb2f70ff87d22e0 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 19 Apr 2021 13:20:37 +0200 Subject: Check for rust doc code attributes like rustdoc does --- crates/ide_db/src/helpers.rs | 1 + crates/ide_db/src/helpers/rust_doc.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 crates/ide_db/src/helpers/rust_doc.rs (limited to 'crates/ide_db') diff --git a/crates/ide_db/src/helpers.rs b/crates/ide_db/src/helpers.rs index 83a665b37..720de0d1f 100644 --- a/crates/ide_db/src/helpers.rs +++ b/crates/ide_db/src/helpers.rs @@ -1,6 +1,7 @@ //! A module with ide helpers for high-level ide features. pub mod insert_use; pub mod import_assets; +pub mod rust_doc; use std::collections::VecDeque; diff --git a/crates/ide_db/src/helpers/rust_doc.rs b/crates/ide_db/src/helpers/rust_doc.rs new file mode 100644 index 000000000..e27e23867 --- /dev/null +++ b/crates/ide_db/src/helpers/rust_doc.rs @@ -0,0 +1,34 @@ +//! Rustdoc specific doc comment handling + +// stripped down version of https://github.com/rust-lang/rust/blob/392ba2ba1a7d6c542d2459fb8133bebf62a4a423/src/librustdoc/html/markdown.rs#L810-L933 +pub fn is_rust_fence(s: &str) -> bool { + let mut seen_rust_tags = false; + let mut seen_other_tags = false; + + let tokens = s + .trim() + .split(|c| c == ',' || c == ' ' || c == '\t') + .map(str::trim) + .filter(|t| !t.is_empty()); + + for token in tokens { + match token { + "should_panic" | "no_run" | "ignore" | "allow_fail" => { + seen_rust_tags = !seen_other_tags + } + "rust" => seen_rust_tags = true, + "test_harness" | "compile_fail" => seen_rust_tags = !seen_other_tags || seen_rust_tags, + x if x.starts_with("edition") => {} + x if x.starts_with('E') && x.len() == 5 => { + if x[1..].parse::().is_ok() { + seen_rust_tags = !seen_other_tags || seen_rust_tags; + } else { + seen_other_tags = true; + } + } + _ => seen_other_tags = true, + } + } + + !seen_other_tags || seen_rust_tags +} -- cgit v1.2.3