aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-06-21 20:57:01 +0100
committerLukas Wirth <[email protected]>2021-06-21 20:57:01 +0100
commit65d683df367ac1d2b8c421c61b76c9e2c26b8704 (patch)
treeefa86f0065174077b7d3fa1a2c95c50963c2643b /crates/ide/src
parent99c95b8fa15f2d9239625c19463f552c84ad99a2 (diff)
Collapse documentation and markdown config settings into an enum
Diffstat (limited to 'crates/ide/src')
-rw-r--r--crates/ide/src/hover.rs70
-rw-r--r--crates/ide/src/lib.rs2
2 files changed, 47 insertions, 25 deletions
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs
index 35050899d..c6d6bb74a 100644
--- a/crates/ide/src/hover.rs
+++ b/crates/ide/src/hover.rs
@@ -31,8 +31,19 @@ use crate::{
31#[derive(Clone, Debug, PartialEq, Eq)] 31#[derive(Clone, Debug, PartialEq, Eq)]
32pub struct HoverConfig { 32pub struct HoverConfig {
33 pub links_in_hover: bool, 33 pub links_in_hover: bool,
34 pub markdown: bool, 34 pub documentation: Option<HoverDocFormat>,
35 pub documentation: bool, 35}
36
37impl HoverConfig {
38 fn markdown(&self) -> bool {
39 matches!(self.documentation, Some(HoverDocFormat::Markdown))
40 }
41}
42
43#[derive(Clone, Debug, PartialEq, Eq)]
44pub enum HoverDocFormat {
45 Markdown,
46 PlainText,
36} 47}
37 48
38#[derive(Debug, Clone)] 49#[derive(Debug, Clone)]
@@ -125,13 +136,7 @@ pub(crate) fn hover(
125 _ => None, 136 _ => None,
126 }; 137 };
127 if let Some(markup) = hover_for_definition(db, definition, famous_defs.as_ref(), config) { 138 if let Some(markup) = hover_for_definition(db, definition, famous_defs.as_ref(), config) {
128 res.markup = process_markup( 139 res.markup = process_markup(sema.db, definition, &markup, config);
129 sema.db,
130 definition,
131 &markup,
132 config.links_in_hover,
133 config.markdown,
134 );
135 if let Some(action) = show_implementations_action(db, definition) { 140 if let Some(action) = show_implementations_action(db, definition) {
136 res.actions.push(action); 141 res.actions.push(action);
137 } 142 }
@@ -172,7 +177,7 @@ pub(crate) fn hover(
172 } 177 }
173 }; 178 };
174 179
175 res.markup = if config.markdown { 180 res.markup = if config.markdown() {
176 Markup::fenced_block(&ty.display(db)) 181 Markup::fenced_block(&ty.display(db))
177 } else { 182 } else {
178 ty.display(db).to_string().into() 183 ty.display(db).to_string().into()
@@ -346,13 +351,12 @@ fn process_markup(
346 db: &RootDatabase, 351 db: &RootDatabase,
347 def: Definition, 352 def: Definition,
348 markup: &Markup, 353 markup: &Markup,
349 links_in_hover: bool, 354 config: &HoverConfig,
350 markdown: bool,
351) -> Markup { 355) -> Markup {
352 let markup = markup.as_str(); 356 let markup = markup.as_str();
353 let markup = if !markdown { 357 let markup = if !config.markdown() {
354 remove_markdown(markup) 358 remove_markdown(markup)
355 } else if links_in_hover { 359 } else if config.links_in_hover {
356 rewrite_links(db, markup, &def) 360 rewrite_links(db, markup, &def)
357 } else { 361 } else {
358 remove_links(markup) 362 remove_links(markup)
@@ -437,7 +441,11 @@ fn hover_for_definition(
437 Definition::Label(it) => return Some(Markup::fenced_block(&it.name(db))), 441 Definition::Label(it) => return Some(Markup::fenced_block(&it.name(db))),
438 }; 442 };
439 443
440 return hover_markup(docs.filter(|_| config.documentation).map(Into::into), label, mod_path); 444 return hover_markup(
445 docs.filter(|_| config.documentation.is_some()).map(Into::into),
446 label,
447 mod_path,
448 );
441 449
442 fn label_and_docs<D>(db: &RootDatabase, def: D) -> (String, Option<hir::Documentation>) 450 fn label_and_docs<D>(db: &RootDatabase, def: D) -> (String, Option<hir::Documentation>)
443 where 451 where
@@ -477,7 +485,7 @@ fn hover_for_keyword(
477 config: &HoverConfig, 485 config: &HoverConfig,
478 token: &SyntaxToken, 486 token: &SyntaxToken,
479) -> Option<RangeInfo<HoverResult>> { 487) -> Option<RangeInfo<HoverResult>> {
480 if !token.kind().is_keyword() || !config.documentation { 488 if !token.kind().is_keyword() || !config.documentation.is_some() {
481 return None; 489 return None;
482 } 490 }
483 let famous_defs = FamousDefs(sema, sema.scope(&token.parent()?).krate()); 491 let famous_defs = FamousDefs(sema, sema.scope(&token.parent()?).krate());
@@ -489,8 +497,7 @@ fn hover_for_keyword(
489 sema.db, 497 sema.db,
490 Definition::ModuleDef(doc_owner.into()), 498 Definition::ModuleDef(doc_owner.into()),
491 &hover_markup(Some(docs.into()), token.text().into(), None)?, 499 &hover_markup(Some(docs.into()), token.text().into(), None)?,
492 config.links_in_hover, 500 config,
493 config.markdown,
494 ); 501 );
495 Some(RangeInfo::new(token.text_range(), HoverResult { markup, actions: Default::default() })) 502 Some(RangeInfo::new(token.text_range(), HoverResult { markup, actions: Default::default() }))
496} 503}
@@ -530,14 +537,17 @@ mod tests {
530 use expect_test::{expect, Expect}; 537 use expect_test::{expect, Expect};
531 use ide_db::base_db::FileLoader; 538 use ide_db::base_db::FileLoader;
532 539
533 use crate::{fixture, HoverConfig}; 540 use crate::{fixture, hover::HoverDocFormat, HoverConfig};
534 541
535 fn check_hover_no_result(ra_fixture: &str) { 542 fn check_hover_no_result(ra_fixture: &str) {
536 let (analysis, position) = fixture::position(ra_fixture); 543 let (analysis, position) = fixture::position(ra_fixture);
537 assert!(analysis 544 assert!(analysis
538 .hover( 545 .hover(
539 position, 546 position,
540 &HoverConfig { links_in_hover: true, markdown: true, documentation: true } 547 &HoverConfig {
548 links_in_hover: true,
549 documentation: Some(HoverDocFormat::Markdown)
550 }
541 ) 551 )
542 .unwrap() 552 .unwrap()
543 .is_none()); 553 .is_none());
@@ -548,7 +558,10 @@ mod tests {
548 let hover = analysis 558 let hover = analysis
549 .hover( 559 .hover(
550 position, 560 position,
551 &HoverConfig { links_in_hover: true, markdown: true, documentation: true }, 561 &HoverConfig {
562 links_in_hover: true,
563 documentation: Some(HoverDocFormat::Markdown),
564 },
552 ) 565 )
553 .unwrap() 566 .unwrap()
554 .unwrap(); 567 .unwrap();
@@ -565,7 +578,10 @@ mod tests {
565 let hover = analysis 578 let hover = analysis
566 .hover( 579 .hover(
567 position, 580 position,
568 &HoverConfig { links_in_hover: false, markdown: true, documentation: true }, 581 &HoverConfig {
582 links_in_hover: false,
583 documentation: Some(HoverDocFormat::Markdown),
584 },
569 ) 585 )
570 .unwrap() 586 .unwrap()
571 .unwrap(); 587 .unwrap();
@@ -582,7 +598,10 @@ mod tests {
582 let hover = analysis 598 let hover = analysis
583 .hover( 599 .hover(
584 position, 600 position,
585 &HoverConfig { links_in_hover: true, markdown: false, documentation: true }, 601 &HoverConfig {
602 links_in_hover: true,
603 documentation: Some(HoverDocFormat::PlainText),
604 },
586 ) 605 )
587 .unwrap() 606 .unwrap()
588 .unwrap(); 607 .unwrap();
@@ -599,7 +618,10 @@ mod tests {
599 let hover = analysis 618 let hover = analysis
600 .hover( 619 .hover(
601 position, 620 position,
602 &HoverConfig { links_in_hover: true, markdown: true, documentation: true }, 621 &HoverConfig {
622 links_in_hover: true,
623 documentation: Some(HoverDocFormat::Markdown),
624 },
603 ) 625 )
604 .unwrap() 626 .unwrap()
605 .unwrap(); 627 .unwrap();
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs
index e24a32218..b978e36af 100644
--- a/crates/ide/src/lib.rs
+++ b/crates/ide/src/lib.rs
@@ -75,7 +75,7 @@ pub use crate::{
75 expand_macro::ExpandedMacro, 75 expand_macro::ExpandedMacro,
76 file_structure::{StructureNode, StructureNodeKind}, 76 file_structure::{StructureNode, StructureNodeKind},
77 folding_ranges::{Fold, FoldKind}, 77 folding_ranges::{Fold, FoldKind},
78 hover::{HoverAction, HoverConfig, HoverGotoTypeData, HoverResult}, 78 hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult},
79 inlay_hints::{InlayHint, InlayHintsConfig, InlayKind}, 79 inlay_hints::{InlayHint, InlayHintsConfig, InlayKind},
80 markup::Markup, 80 markup::Markup,
81 move_item::Direction, 81 move_item::Direction,