aboutsummaryrefslogtreecommitdiff
path: root/crates/ide
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-12-11 19:57:08 +0000
committerGitHub <[email protected]>2020-12-11 19:57:08 +0000
commit91bf15a2f53629209bd13d2e46121b9be8af1f94 (patch)
treecd954234bf0ca9c8151739fe7c9565249e70c3d0 /crates/ide
parent1e4378b5406756413d0be9e89a13904bfad5ca79 (diff)
parentac19a71459765f897c3bb38ffd9a5a179e0beb02 (diff)
Merge #6834
6834: Use Attrs::docs in runnables instead of DocCommentsOwner r=kjeremy a=Veykril I figured that we should probably move as much of the doc usage to the HIR as possible hence this PR. If we should keep this AST-based feel free to close. This change does have the nice(but not really useful as I doubt anyones gonna write doc tests like these) side effect that these two doc string snippets allow being run now. ![image](https://user-images.githubusercontent.com/3757771/101945607-bf241400-3bee-11eb-96ce-ccae80028b1f.png) ![image](https://user-images.githubusercontent.com/3757771/101946375-2e9a0380-3bef-11eb-9950-e35168fdd048.png) Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/ide')
-rw-r--r--crates/ide/src/runnables.rs18
1 files changed, 9 insertions, 9 deletions
diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs
index e15411777..646f63704 100644
--- a/crates/ide/src/runnables.rs
+++ b/crates/ide/src/runnables.rs
@@ -6,7 +6,7 @@ use hir::{AsAssocItem, Attrs, HirFileId, InFile, Semantics};
6use ide_db::RootDatabase; 6use ide_db::RootDatabase;
7use itertools::Itertools; 7use itertools::Itertools;
8use syntax::{ 8use syntax::{
9 ast::{self, AstNode, AttrsOwner, DocCommentsOwner, ModuleItemOwner, NameOwner}, 9 ast::{self, AstNode, AttrsOwner, ModuleItemOwner, NameOwner},
10 match_ast, SyntaxNode, 10 match_ast, SyntaxNode,
11}; 11};
12 12
@@ -118,6 +118,7 @@ fn runnable_fn(
118) -> Option<Runnable> { 118) -> Option<Runnable> {
119 let name_string = fn_def.name()?.text().to_string(); 119 let name_string = fn_def.name()?.text().to_string();
120 120
121 let attrs = Attrs::from_attrs_owner(sema.db, InFile::new(HirFileId::from(file_id), &fn_def));
121 let kind = if name_string == "main" { 122 let kind = if name_string == "main" {
122 RunnableKind::Bin 123 RunnableKind::Bin
123 } else { 124 } else {
@@ -162,14 +163,13 @@ fn runnable_fn(
162 RunnableKind::Test { test_id, attr } 163 RunnableKind::Test { test_id, attr }
163 } else if fn_def.has_atom_attr("bench") { 164 } else if fn_def.has_atom_attr("bench") {
164 RunnableKind::Bench { test_id } 165 RunnableKind::Bench { test_id }
165 } else if has_runnable_doc_test(&fn_def) { 166 } else if has_runnable_doc_test(&attrs) {
166 RunnableKind::DocTest { test_id } 167 RunnableKind::DocTest { test_id }
167 } else { 168 } else {
168 return None; 169 return None;
169 } 170 }
170 }; 171 };
171 172
172 let attrs = Attrs::from_attrs_owner(sema.db, InFile::new(HirFileId::from(file_id), &fn_def));
173 let cfg = attrs.cfg(); 173 let cfg = attrs.cfg();
174 174
175 let nav = if let RunnableKind::DocTest { .. } = kind { 175 let nav = if let RunnableKind::DocTest { .. } = kind {
@@ -189,13 +189,13 @@ fn runnable_struct(
189 struct_def: ast::Struct, 189 struct_def: ast::Struct,
190 file_id: FileId, 190 file_id: FileId,
191) -> Option<Runnable> { 191) -> Option<Runnable> {
192 if !has_runnable_doc_test(&struct_def) {
193 return None;
194 }
195 let name_string = struct_def.name()?.text().to_string(); 192 let name_string = struct_def.name()?.text().to_string();
196 193
197 let attrs = 194 let attrs =
198 Attrs::from_attrs_owner(sema.db, InFile::new(HirFileId::from(file_id), &struct_def)); 195 Attrs::from_attrs_owner(sema.db, InFile::new(HirFileId::from(file_id), &struct_def));
196 if !has_runnable_doc_test(&attrs) {
197 return None;
198 }
199 let cfg = attrs.cfg(); 199 let cfg = attrs.cfg();
200 200
201 let test_id = match sema.to_def(&struct_def).map(|def| def.module(sema.db)) { 201 let test_id = match sema.to_def(&struct_def).map(|def| def.module(sema.db)) {
@@ -240,11 +240,11 @@ const RUSTDOC_FENCE: &str = "```";
240const RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUNNABLE: &[&str] = 240const RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUNNABLE: &[&str] =
241 &["", "rust", "should_panic", "edition2015", "edition2018"]; 241 &["", "rust", "should_panic", "edition2015", "edition2018"];
242 242
243fn has_runnable_doc_test(def: &dyn DocCommentsOwner) -> bool { 243fn has_runnable_doc_test(attrs: &hir::Attrs) -> bool {
244 def.doc_comment_text().map_or(false, |comments_text| { 244 attrs.docs().map_or(false, |doc| {
245 let mut in_code_block = false; 245 let mut in_code_block = false;
246 246
247 for line in comments_text.lines() { 247 for line in String::from(doc).lines() {
248 if let Some(header) = line.strip_prefix(RUSTDOC_FENCE) { 248 if let Some(header) = line.strip_prefix(RUSTDOC_FENCE) {
249 in_code_block = !in_code_block; 249 in_code_block = !in_code_block;
250 250