]> git.lizzy.rs Git - rust.git/commitdiff
Collapse documentation and markdown config settings into an enum
authorLukas Wirth <lukastw97@gmail.com>
Mon, 21 Jun 2021 19:57:01 +0000 (21:57 +0200)
committerLukas Wirth <lukastw97@gmail.com>
Mon, 21 Jun 2021 19:57:01 +0000 (21:57 +0200)
crates/ide/src/hover.rs
crates/ide/src/lib.rs
crates/rust-analyzer/src/config.rs

index 35050899d19a0a9b8619696e29db57dcf5ef8988..c6d6bb74a829d314182ff279bdb2acdf142e682b 100644 (file)
 #[derive(Clone, Debug, PartialEq, Eq)]
 pub struct HoverConfig {
     pub links_in_hover: bool,
-    pub markdown: bool,
-    pub documentation: bool,
+    pub documentation: Option<HoverDocFormat>,
+}
+
+impl HoverConfig {
+    fn markdown(&self) -> bool {
+        matches!(self.documentation, Some(HoverDocFormat::Markdown))
+    }
+}
+
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub enum HoverDocFormat {
+    Markdown,
+    PlainText,
 }
 
 #[derive(Debug, Clone)]
@@ -125,13 +136,7 @@ pub(crate) fn hover(
             _ => None,
         };
         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,
-            );
+            res.markup = process_markup(sema.db, definition, &markup, config);
             if let Some(action) = show_implementations_action(db, definition) {
                 res.actions.push(action);
             }
@@ -172,7 +177,7 @@ pub(crate) fn hover(
         }
     };
 
-    res.markup = if config.markdown {
+    res.markup = if config.markdown() {
         Markup::fenced_block(&ty.display(db))
     } else {
         ty.display(db).to_string().into()
@@ -346,13 +351,12 @@ fn process_markup(
     db: &RootDatabase,
     def: Definition,
     markup: &Markup,
-    links_in_hover: bool,
-    markdown: bool,
+    config: &HoverConfig,
 ) -> Markup {
     let markup = markup.as_str();
-    let markup = if !markdown {
+    let markup = if !config.markdown() {
         remove_markdown(markup)
-    } else if links_in_hover {
+    } else if config.links_in_hover {
         rewrite_links(db, markup, &def)
     } else {
         remove_links(markup)
@@ -437,7 +441,11 @@ fn hover_for_definition(
         Definition::Label(it) => return Some(Markup::fenced_block(&it.name(db))),
     };
 
-    return hover_markup(docs.filter(|_| config.documentation).map(Into::into), label, mod_path);
+    return hover_markup(
+        docs.filter(|_| config.documentation.is_some()).map(Into::into),
+        label,
+        mod_path,
+    );
 
     fn label_and_docs<D>(db: &RootDatabase, def: D) -> (String, Option<hir::Documentation>)
     where
@@ -477,7 +485,7 @@ fn hover_for_keyword(
     config: &HoverConfig,
     token: &SyntaxToken,
 ) -> Option<RangeInfo<HoverResult>> {
-    if !token.kind().is_keyword() || !config.documentation {
+    if !token.kind().is_keyword() || !config.documentation.is_some() {
         return None;
     }
     let famous_defs = FamousDefs(sema, sema.scope(&token.parent()?).krate());
@@ -489,8 +497,7 @@ fn hover_for_keyword(
         sema.db,
         Definition::ModuleDef(doc_owner.into()),
         &hover_markup(Some(docs.into()), token.text().into(), None)?,
-        config.links_in_hover,
-        config.markdown,
+        config,
     );
     Some(RangeInfo::new(token.text_range(), HoverResult { markup, actions: Default::default() }))
 }
@@ -530,14 +537,17 @@ mod tests {
     use expect_test::{expect, Expect};
     use ide_db::base_db::FileLoader;
 
-    use crate::{fixture, HoverConfig};
+    use crate::{fixture, hover::HoverDocFormat, HoverConfig};
 
     fn check_hover_no_result(ra_fixture: &str) {
         let (analysis, position) = fixture::position(ra_fixture);
         assert!(analysis
             .hover(
                 position,
-                &HoverConfig { links_in_hover: true, markdown: true, documentation: true }
+                &HoverConfig {
+                    links_in_hover: true,
+                    documentation: Some(HoverDocFormat::Markdown)
+                }
             )
             .unwrap()
             .is_none());
@@ -548,7 +558,10 @@ fn check(ra_fixture: &str, expect: Expect) {
         let hover = analysis
             .hover(
                 position,
-                &HoverConfig { links_in_hover: true, markdown: true, documentation: true },
+                &HoverConfig {
+                    links_in_hover: true,
+                    documentation: Some(HoverDocFormat::Markdown),
+                },
             )
             .unwrap()
             .unwrap();
@@ -565,7 +578,10 @@ fn check_hover_no_links(ra_fixture: &str, expect: Expect) {
         let hover = analysis
             .hover(
                 position,
-                &HoverConfig { links_in_hover: false, markdown: true, documentation: true },
+                &HoverConfig {
+                    links_in_hover: false,
+                    documentation: Some(HoverDocFormat::Markdown),
+                },
             )
             .unwrap()
             .unwrap();
@@ -582,7 +598,10 @@ fn check_hover_no_markdown(ra_fixture: &str, expect: Expect) {
         let hover = analysis
             .hover(
                 position,
-                &HoverConfig { links_in_hover: true, markdown: false, documentation: true },
+                &HoverConfig {
+                    links_in_hover: true,
+                    documentation: Some(HoverDocFormat::PlainText),
+                },
             )
             .unwrap()
             .unwrap();
@@ -599,7 +618,10 @@ fn check_actions(ra_fixture: &str, expect: Expect) {
         let hover = analysis
             .hover(
                 position,
-                &HoverConfig { links_in_hover: true, markdown: true, documentation: true },
+                &HoverConfig {
+                    links_in_hover: true,
+                    documentation: Some(HoverDocFormat::Markdown),
+                },
             )
             .unwrap()
             .unwrap();
index e24a322185288f771fbc67b8cb7cd52b762e1555..b978e36af2e5b3a76197b4ef51033751cf6af742 100644 (file)
@@ -75,7 +75,7 @@ macro_rules! eprintln {
     expand_macro::ExpandedMacro,
     file_structure::{StructureNode, StructureNodeKind},
     folding_ranges::{Fold, FoldKind},
-    hover::{HoverAction, HoverConfig, HoverGotoTypeData, HoverResult},
+    hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult},
     inlay_hints::{InlayHint, InlayHintsConfig, InlayKind},
     markup::Markup,
     move_item::Direction,
index de70959a5381ae80e257f70c1f7ebc13db8b0a50..b9aa6f0aada0fb8efe25facccc981bbf56abdfe4 100644 (file)
 use std::{ffi::OsString, iter, path::PathBuf};
 
 use flycheck::FlycheckConfig;
-use ide::{AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig};
+use ide::{
+    AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, HoverDocFormat,
+    InlayHintsConfig,
+};
 use ide_db::helpers::{
     insert_use::{ImportGranularity, InsertUseConfig, PrefixKind},
     SnippetCap,
@@ -777,19 +780,25 @@ pub fn highlighting_strings(&self) -> bool {
     pub fn hover(&self) -> HoverConfig {
         HoverConfig {
             links_in_hover: self.data.hover_linksInHover,
-            markdown: try_or!(
-                self.caps
-                    .text_document
-                    .as_ref()?
-                    .hover
-                    .as_ref()?
-                    .content_format
-                    .as_ref()?
-                    .as_slice(),
-                &[]
-            )
-            .contains(&MarkupKind::Markdown),
-            documentation: self.data.hover_documentation,
+            documentation: self.data.hover_documentation.then(|| {
+                let is_markdown = try_or!(
+                    self.caps
+                        .text_document
+                        .as_ref()?
+                        .hover
+                        .as_ref()?
+                        .content_format
+                        .as_ref()?
+                        .as_slice(),
+                    &[]
+                )
+                .contains(&MarkupKind::Markdown);
+                if is_markdown {
+                    HoverDocFormat::Markdown
+                } else {
+                    HoverDocFormat::PlainText
+                }
+            }),
         }
     }