From ec824a92d05caa1908cb25cbd5b969c8e995aaa7 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 17 Mar 2021 14:38:11 +0100 Subject: Better handling of block doc comments --- crates/ide/src/goto_definition.rs | 8 +++---- crates/ide/src/hover.rs | 34 +++++++++++++++++++++++++++ crates/ide/src/runnables.rs | 48 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 83 insertions(+), 7 deletions(-) (limited to 'crates/ide/src') diff --git a/crates/ide/src/goto_definition.rs b/crates/ide/src/goto_definition.rs index b71f4917c..5072ecea0 100644 --- a/crates/ide/src/goto_definition.rs +++ b/crates/ide/src/goto_definition.rs @@ -95,12 +95,10 @@ fn extract_positioned_link_from_comment( let comment_range = comment.syntax().text_range(); let doc_comment = comment.doc_comment()?; let def_links = extract_definitions_from_markdown(doc_comment); + let start = comment_range.start() + TextSize::from(comment.prefix().len() as u32); let (def_link, ns, _) = def_links.iter().min_by_key(|(_, _, def_link_range)| { - let matched_position = comment_range.start() + TextSize::from(def_link_range.start as u32); - match position.offset.checked_sub(matched_position) { - Some(distance) => distance, - None => comment_range.end(), - } + let matched_position = start + TextSize::from(def_link_range.start as u32); + position.offset.checked_sub(matched_position).unwrap_or_else(|| comment_range.end()) })?; Some((def_link.to_string(), *ns)) } diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 325014622..cc2b79124 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -3423,6 +3423,40 @@ mod Foo$0 { ); } + #[test] + fn hover_doc_block_style_indentend() { + check( + r#" +/** + foo + ```rust + let x = 3; + ``` +*/ +fn foo$0() {} +"#, + expect![[r#" + *foo* + + ```rust + test + ``` + + ```rust + fn foo() + ``` + + --- + + foo + + ```rust + let x = 3; + ``` + "#]], + ); + } + #[test] fn hover_comments_dont_highlight_parent() { check_hover_no_result( diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index 397e2126b..bea020b06 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs @@ -576,6 +576,20 @@ fn should_have_runnable_1() {} /// ``` fn should_have_runnable_2() {} +/** +```rust +let z = 55; +``` +*/ +fn should_have_no_runnable_3() {} + +/** + ```rust + let z = 55; + ``` +*/ +fn should_have_no_runnable_4() {} + /// ```no_run /// let z = 55; /// ``` @@ -616,7 +630,7 @@ fn should_have_no_runnable_6() {} struct StructWithRunnable(String); "#, - &[&BIN, &DOCTEST, &DOCTEST, &DOCTEST, &DOCTEST], + &[&BIN, &DOCTEST, &DOCTEST, &DOCTEST, &DOCTEST, &DOCTEST, &DOCTEST], expect![[r#" [ Runnable { @@ -682,7 +696,37 @@ struct StructWithRunnable(String); file_id: FileId( 0, ), - full_range: 756..821, + full_range: 256..320, + name: "should_have_no_runnable_3", + }, + kind: DocTest { + test_id: Path( + "should_have_no_runnable_3", + ), + }, + cfg: None, + }, + Runnable { + nav: NavigationTarget { + file_id: FileId( + 0, + ), + full_range: 322..398, + name: "should_have_no_runnable_4", + }, + kind: DocTest { + test_id: Path( + "should_have_no_runnable_4", + ), + }, + cfg: None, + }, + Runnable { + nav: NavigationTarget { + file_id: FileId( + 0, + ), + full_range: 900..965, name: "StructWithRunnable", }, kind: DocTest { -- cgit v1.2.3