From f7a15b5cd1df58e46066bbd27c90cb1ad7f9c316 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 Jan 2021 13:54:28 +0300 Subject: More maintainable config Rather than eagerly converting JSON, we losslessly keep it as is, and change the shape of user-submitted data at the last moment. This also allows us to remove a bunch of wrong Defaults --- crates/ide/src/hover.rs | 29 +++++++---------------------- crates/ide/src/inlay_hints.rs | 21 +++++++++++---------- crates/ide/src/runnables.rs | 35 +++++++++++++++++------------------ 3 files changed, 35 insertions(+), 50 deletions(-) (limited to 'crates/ide/src') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index f2ad95cb6..72c9c66fe 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -17,7 +17,7 @@ use crate::{ doc_links::{remove_links, rewrite_links}, markdown_remove::remove_markdown, markup::Markup, - runnables::runnable, + runnables::{runnable, runnable_fn}, FileId, FilePosition, NavigationTarget, RangeInfo, Runnable, }; @@ -31,19 +31,6 @@ pub struct HoverConfig { pub markdown: bool, } -impl Default for HoverConfig { - fn default() -> Self { - Self { - implementations: true, - run: true, - debug: true, - goto_type_def: true, - links_in_hover: true, - markdown: true, - } - } -} - impl HoverConfig { pub const NO_ACTIONS: Self = Self { implementations: false, @@ -204,22 +191,20 @@ fn runnable_action( match def { Definition::ModuleDef(it) => match it { ModuleDef::Module(it) => match it.definition_source(sema.db).value { - ModuleSource::Module(it) => runnable(&sema, it.syntax().clone(), file_id) - .map(|it| HoverAction::Runnable(it)), + ModuleSource::Module(it) => { + runnable(&sema, it.syntax().clone()).map(|it| HoverAction::Runnable(it)) + } _ => None, }, - ModuleDef::Function(it) => { - #[allow(deprecated)] - let src = it.source(sema.db)?; + ModuleDef::Function(func) => { + let src = func.source(sema.db)?; if src.file_id != file_id.into() { mark::hit!(hover_macro_generated_struct_fn_doc_comment); mark::hit!(hover_macro_generated_struct_fn_doc_attr); - return None; } - runnable(&sema, src.value.syntax().clone(), file_id) - .map(|it| HoverAction::Runnable(it)) + runnable_fn(&sema, func).map(HoverAction::Runnable) } _ => None, }, diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 65df7979c..fe60abfc8 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -18,12 +18,6 @@ pub struct InlayHintsConfig { pub max_length: Option, } -impl Default for InlayHintsConfig { - fn default() -> Self { - Self { type_hints: true, parameter_hints: true, chaining_hints: true, max_length: None } - } -} - #[derive(Clone, Debug, PartialEq, Eq)] pub enum InlayKind { TypeHint, @@ -433,8 +427,15 @@ mod tests { use crate::{fixture, inlay_hints::InlayHintsConfig}; + const TEST_CONFIG: InlayHintsConfig = InlayHintsConfig { + type_hints: true, + parameter_hints: true, + chaining_hints: true, + max_length: None, + }; + fn check(ra_fixture: &str) { - check_with_config(InlayHintsConfig::default(), ra_fixture); + check_with_config(TEST_CONFIG, ra_fixture); } fn check_with_config(config: InlayHintsConfig, ra_fixture: &str) { @@ -748,7 +749,7 @@ fn main() { #[test] fn hint_truncation() { check_with_config( - InlayHintsConfig { max_length: Some(8), ..Default::default() }, + InlayHintsConfig { max_length: Some(8), ..TEST_CONFIG }, r#" struct Smol(T); @@ -831,7 +832,7 @@ fn main() { #[test] fn omitted_parameters_hints_heuristics() { check_with_config( - InlayHintsConfig { max_length: Some(8), ..Default::default() }, + InlayHintsConfig { max_length: Some(8), ..TEST_CONFIG }, r#" fn map(f: i32) {} fn filter(predicate: i32) {} @@ -924,7 +925,7 @@ fn main() { #[test] fn unit_structs_have_no_type_hints() { check_with_config( - InlayHintsConfig { max_length: Some(8), ..Default::default() }, + InlayHintsConfig { max_length: Some(8), ..TEST_CONFIG }, r#" enum Result { Ok(T), Err(E) } use Result::*; diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index c893afc7c..f4030f3ef 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs @@ -2,11 +2,11 @@ use std::fmt; use assists::utils::test_related_attribute; use cfg::CfgExpr; -use hir::{AsAssocItem, HasAttrs, InFile, Semantics}; +use hir::{AsAssocItem, HasAttrs, HasSource, Semantics}; use ide_db::RootDatabase; use itertools::Itertools; use syntax::{ - ast::{self, AstNode, AttrsOwner, ModuleItemOwner, NameOwner}, + ast::{self, AstNode, AttrsOwner, ModuleItemOwner}, match_ast, SyntaxNode, }; @@ -96,17 +96,16 @@ impl Runnable { pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec { let sema = Semantics::new(db); let source_file = sema.parse(file_id); - source_file.syntax().descendants().filter_map(|i| runnable(&sema, i, file_id)).collect() + source_file.syntax().descendants().filter_map(|i| runnable(&sema, i)).collect() } -pub(crate) fn runnable( - sema: &Semantics, - item: SyntaxNode, - file_id: FileId, -) -> Option { +pub(crate) fn runnable(sema: &Semantics, item: SyntaxNode) -> Option { let runnable_item = match_ast! { match (item.clone()) { - ast::Fn(it) => runnable_fn(sema, it, file_id), + ast::Fn(func) => { + let def = sema.to_def(&func)?; + runnable_fn(sema, def) + }, ast::Module(it) => runnable_mod(sema, it), _ => None, } @@ -114,23 +113,23 @@ pub(crate) fn runnable( runnable_item.or_else(|| runnable_doctest(sema, item)) } -fn runnable_fn(sema: &Semantics, func: ast::Fn, file_id: FileId) -> Option { - let def = sema.to_def(&func)?; - let name_string = func.name()?.text().to_string(); +pub(crate) fn runnable_fn(sema: &Semantics, def: hir::Function) -> Option { + let func = def.source(sema.db)?; + let name_string = def.name(sema.db).to_string(); let kind = if name_string == "main" { RunnableKind::Bin } else { - let canonical_path = sema.to_def(&func).and_then(|def| { + let canonical_path = { let def: hir::ModuleDef = def.into(); def.canonical_path(sema.db) - }); + }; let test_id = canonical_path.map(TestId::Path).unwrap_or(TestId::Name(name_string)); - if test_related_attribute(&func).is_some() { - let attr = TestAttr::from_fn(&func); + if test_related_attribute(&func.value).is_some() { + let attr = TestAttr::from_fn(&func.value); RunnableKind::Test { test_id, attr } - } else if func.has_atom_attr("bench") { + } else if func.value.has_atom_attr("bench") { RunnableKind::Bench { test_id } } else { return None; @@ -139,7 +138,7 @@ fn runnable_fn(sema: &Semantics, func: ast::Fn, file_id: FileId) - let nav = NavigationTarget::from_named( sema.db, - InFile::new(file_id.into(), &func), + func.as_ref().map(|it| it as &dyn ast::NameOwner), SymbolKind::Function, ); let cfg = def.attrs(sema.db).cfg(); -- cgit v1.2.3