Collapse documentation and markdown config settings into an enum

This commit is contained in:
Lukas Wirth 2021-06-21 21:57:01 +02:00
parent 99c95b8fa1
commit 65d683df36
3 changed files with 70 additions and 39 deletions

View File

@ -31,8 +31,19 @@
#[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();

View 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,

View File

@ -10,7 +10,10 @@
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
}
}),
}
}