From 99c95b8fa15f2d9239625c19463f552c84ad99a2 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 21 Jun 2021 21:41:06 +0200 Subject: Split hover actions config into its own config struct --- crates/ide/src/hover.rs | 111 ++++++++++++++++++++++++------------------------ crates/ide/src/lib.rs | 6 +-- 2 files changed, 57 insertions(+), 60 deletions(-) (limited to 'crates/ide/src') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 0c1da8774..35050899d 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -1,5 +1,5 @@ use either::Either; -use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay}; +use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics}; use ide_db::{ base_db::SourceDatabase, defs::{Definition, NameClass, NameRefClass}, @@ -30,41 +30,11 @@ use crate::{ #[derive(Clone, Debug, PartialEq, Eq)] pub struct HoverConfig { - pub implementations: bool, - pub references: bool, - pub run: bool, - pub debug: bool, - pub goto_type_def: bool, pub links_in_hover: bool, pub markdown: bool, pub documentation: bool, } -impl HoverConfig { - pub const NO_ACTIONS: Self = Self { - implementations: false, - references: false, - run: false, - debug: false, - goto_type_def: false, - links_in_hover: true, - markdown: true, - documentation: true, - }; - - pub fn any_actions(&self) -> bool { - self.implementations || self.references || self.runnable() || self.goto_type_def - } - - pub fn no_actions(&self) -> bool { - !self.any_actions() - } - - pub fn runnable(&self) -> bool { - self.run || self.debug - } -} - #[derive(Debug, Clone)] pub enum HoverAction { Runnable(Runnable), @@ -95,9 +65,7 @@ pub struct HoverResult { pub(crate) fn hover( db: &RootDatabase, position: FilePosition, - links_in_hover: bool, - documentation: bool, - markdown: bool, + config: &HoverConfig, ) -> Option> { let sema = hir::Semantics::new(db); let file = sema.parse(position.file_id).syntax().clone(); @@ -156,10 +124,14 @@ pub(crate) fn hover( } _ => None, }; - if let Some(markup) = - hover_for_definition(db, definition, famous_defs.as_ref(), documentation) - { - res.markup = process_markup(sema.db, definition, &markup, links_in_hover, markdown); + if let Some(markup) = hover_for_definition(db, definition, famous_defs.as_ref(), config) { + res.markup = process_markup( + sema.db, + definition, + &markup, + config.links_in_hover, + config.markdown, + ); if let Some(action) = show_implementations_action(db, definition) { res.actions.push(action); } @@ -181,8 +153,7 @@ pub(crate) fn hover( } } - if let res @ Some(_) = hover_for_keyword(&sema, links_in_hover, markdown, documentation, &token) - { + if let res @ Some(_) = hover_for_keyword(&sema, config, &token) { return res; } @@ -201,7 +172,7 @@ pub(crate) fn hover( } }; - res.markup = if markdown { + res.markup = if config.markdown { Markup::fenced_block(&ty.display(db)) } else { ty.display(db).to_string().into() @@ -428,7 +399,7 @@ fn hover_for_definition( db: &RootDatabase, def: Definition, famous_defs: Option<&FamousDefs>, - documentation: bool, + config: &HoverConfig, ) -> Option { let mod_path = definition_mod_path(db, &def); let (label, docs) = match def { @@ -466,7 +437,7 @@ fn hover_for_definition( Definition::Label(it) => return Some(Markup::fenced_block(&it.name(db))), }; - return hover_markup(docs.filter(|_| documentation).map(Into::into), label, mod_path); + return hover_markup(docs.filter(|_| config.documentation).map(Into::into), label, mod_path); fn label_and_docs(db: &RootDatabase, def: D) -> (String, Option) where @@ -502,13 +473,11 @@ fn hover_for_local(it: hir::Local, db: &RootDatabase) -> Option { } fn hover_for_keyword( - sema: &hir::Semantics, - links_in_hover: bool, - markdown: bool, - documentation: bool, + sema: &Semantics, + config: &HoverConfig, token: &SyntaxToken, ) -> Option> { - if !token.kind().is_keyword() || !documentation { + if !token.kind().is_keyword() || !config.documentation { return None; } let famous_defs = FamousDefs(sema, sema.scope(&token.parent()?).krate()); @@ -520,8 +489,8 @@ fn hover_for_keyword( sema.db, Definition::ModuleDef(doc_owner.into()), &hover_markup(Some(docs.into()), token.text().into(), None)?, - links_in_hover, - markdown, + config.links_in_hover, + config.markdown, ); Some(RangeInfo::new(token.text_range(), HoverResult { markup, actions: Default::default() })) } @@ -561,16 +530,28 @@ mod tests { use expect_test::{expect, Expect}; use ide_db::base_db::FileLoader; - use crate::fixture; + use crate::{fixture, HoverConfig}; fn check_hover_no_result(ra_fixture: &str) { let (analysis, position) = fixture::position(ra_fixture); - assert!(analysis.hover(position, true, true, true).unwrap().is_none()); + assert!(analysis + .hover( + position, + &HoverConfig { links_in_hover: true, markdown: true, documentation: true } + ) + .unwrap() + .is_none()); } fn check(ra_fixture: &str, expect: Expect) { let (analysis, position) = fixture::position(ra_fixture); - let hover = analysis.hover(position, true, true, true).unwrap().unwrap(); + let hover = analysis + .hover( + position, + &HoverConfig { links_in_hover: true, markdown: true, documentation: true }, + ) + .unwrap() + .unwrap(); let content = analysis.db.file_text(position.file_id); let hovered_element = &content[hover.range]; @@ -581,7 +562,13 @@ mod tests { fn check_hover_no_links(ra_fixture: &str, expect: Expect) { let (analysis, position) = fixture::position(ra_fixture); - let hover = analysis.hover(position, false, true, true).unwrap().unwrap(); + let hover = analysis + .hover( + position, + &HoverConfig { links_in_hover: false, markdown: true, documentation: true }, + ) + .unwrap() + .unwrap(); let content = analysis.db.file_text(position.file_id); let hovered_element = &content[hover.range]; @@ -592,7 +579,13 @@ mod tests { fn check_hover_no_markdown(ra_fixture: &str, expect: Expect) { let (analysis, position) = fixture::position(ra_fixture); - let hover = analysis.hover(position, true, true, false).unwrap().unwrap(); + let hover = analysis + .hover( + position, + &HoverConfig { links_in_hover: true, markdown: false, documentation: true }, + ) + .unwrap() + .unwrap(); let content = analysis.db.file_text(position.file_id); let hovered_element = &content[hover.range]; @@ -603,7 +596,13 @@ mod tests { fn check_actions(ra_fixture: &str, expect: Expect) { let (analysis, position) = fixture::position(ra_fixture); - let hover = analysis.hover(position, true, true, true).unwrap().unwrap(); + let hover = analysis + .hover( + position, + &HoverConfig { links_in_hover: true, markdown: true, documentation: true }, + ) + .unwrap() + .unwrap(); expect.assert_debug_eq(&hover.info.actions) } diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index aac084012..e24a32218 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -408,11 +408,9 @@ impl Analysis { pub fn hover( &self, position: FilePosition, - links_in_hover: bool, - documentation: bool, - markdown: bool, + config: &HoverConfig, ) -> Cancellable>> { - self.with_db(|db| hover::hover(db, position, links_in_hover, documentation, markdown)) + self.with_db(|db| hover::hover(db, position, config)) } /// Return URL(s) for the documentation of the symbol under the cursor. -- cgit v1.2.3