Skip to content

Use Attrs::docs in runnables instead of DocCommentsOwner #6834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/hir_def/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ impl Documentation {
}
}

impl Into<String> for Documentation {
fn into(self) -> String {
self.0
impl From<Documentation> for String {
fn from(Documentation(string): Documentation) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little awkward. It took me a second to remember that this was valid rust.

string
}
}

Expand Down
18 changes: 9 additions & 9 deletions crates/ide/src/runnables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use hir::{AsAssocItem, Attrs, HirFileId, InFile, Semantics};
use ide_db::RootDatabase;
use itertools::Itertools;
use syntax::{
ast::{self, AstNode, AttrsOwner, DocCommentsOwner, ModuleItemOwner, NameOwner},
ast::{self, AstNode, AttrsOwner, ModuleItemOwner, NameOwner},
match_ast, SyntaxNode,
};

Expand Down Expand Up @@ -118,6 +118,7 @@ fn runnable_fn(
) -> Option<Runnable> {
let name_string = fn_def.name()?.text().to_string();

let attrs = Attrs::from_attrs_owner(sema.db, InFile::new(HirFileId::from(file_id), &fn_def));
let kind = if name_string == "main" {
RunnableKind::Bin
} else {
Expand Down Expand Up @@ -162,14 +163,13 @@ fn runnable_fn(
RunnableKind::Test { test_id, attr }
} else if fn_def.has_atom_attr("bench") {
RunnableKind::Bench { test_id }
} else if has_runnable_doc_test(&fn_def) {
} else if has_runnable_doc_test(&attrs) {
RunnableKind::DocTest { test_id }
} else {
return None;
}
};

let attrs = Attrs::from_attrs_owner(sema.db, InFile::new(HirFileId::from(file_id), &fn_def));
let cfg = attrs.cfg();

let nav = if let RunnableKind::DocTest { .. } = kind {
Expand All @@ -189,13 +189,13 @@ fn runnable_struct(
struct_def: ast::Struct,
file_id: FileId,
) -> Option<Runnable> {
if !has_runnable_doc_test(&struct_def) {
return None;
}
let name_string = struct_def.name()?.text().to_string();

let attrs =
Attrs::from_attrs_owner(sema.db, InFile::new(HirFileId::from(file_id), &struct_def));
if !has_runnable_doc_test(&attrs) {
return None;
}
let cfg = attrs.cfg();

let test_id = match sema.to_def(&struct_def).map(|def| def.module(sema.db)) {
Expand Down Expand Up @@ -240,11 +240,11 @@ const RUSTDOC_FENCE: &str = "```";
const RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUNNABLE: &[&str] =
&["", "rust", "should_panic", "edition2015", "edition2018"];

fn has_runnable_doc_test(def: &dyn DocCommentsOwner) -> bool {
def.doc_comment_text().map_or(false, |comments_text| {
fn has_runnable_doc_test(attrs: &hir::Attrs) -> bool {
attrs.docs().map_or(false, |doc| {
let mut in_code_block = false;

for line in comments_text.lines() {
for line in String::from(doc).lines() {
if let Some(header) = line.strip_prefix(RUSTDOC_FENCE) {
in_code_block = !in_code_block;

Expand Down