From b8d4bb76703bcb15578e2b23c5d256532180b894 Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Tue, 3 Dec 2019 03:01:24 +0000 Subject: CTAN sync 201912030301 --- support/texlab/src/hover/bibtex_entry_type.rs | 111 ++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 support/texlab/src/hover/bibtex_entry_type.rs (limited to 'support/texlab/src/hover/bibtex_entry_type.rs') diff --git a/support/texlab/src/hover/bibtex_entry_type.rs b/support/texlab/src/hover/bibtex_entry_type.rs new file mode 100644 index 0000000000..adf833b689 --- /dev/null +++ b/support/texlab/src/hover/bibtex_entry_type.rs @@ -0,0 +1,111 @@ +use crate::range::RangeExt; +use crate::syntax::*; +use crate::workspace::*; +use futures_boxed::boxed; +use lsp_types::*; + +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct BibtexEntryTypeHoverProvider; + +impl FeatureProvider for BibtexEntryTypeHoverProvider { + type Params = TextDocumentPositionParams; + type Output = Option; + + #[boxed] + async fn execute<'a>( + &'a self, + request: &'a FeatureRequest, + ) -> Option { + if let SyntaxTree::Bibtex(tree) = &request.document().tree { + for entry in tree.entries() { + if entry.ty.range().contains(request.params.position) { + let ty = &entry.ty.text()[1..]; + if let Some(documentation) = LANGUAGE_DATA.entry_type_documentation(ty) { + return Some(Hover { + contents: HoverContents::Markup(MarkupContent { + kind: MarkupKind::Markdown, + value: documentation.into(), + }), + range: None, + }); + } + } + } + } + None + } +} + +#[cfg(test)] +mod tests { + use super::*; + use lsp_types::Position; + + #[test] + fn test_known_entry_type() { + let hover = test_feature( + BibtexEntryTypeHoverProvider, + FeatureSpec { + files: vec![FeatureSpec::file("foo.bib", "@article{foo,}")], + main_file: "foo.bib", + position: Position::new(0, 3), + ..FeatureSpec::default() + }, + ); + assert_eq!( + hover, + Some(Hover { + contents: HoverContents::Markup(MarkupContent { + kind: MarkupKind::Markdown, + value: LANGUAGE_DATA + .entry_type_documentation("article") + .unwrap() + .into(), + }), + range: None, + }) + ); + } + + #[test] + fn test_unknown_entry_type() { + let hover = test_feature( + BibtexEntryTypeHoverProvider, + FeatureSpec { + files: vec![FeatureSpec::file("foo.bib", "@foo{bar,}")], + main_file: "foo.bib", + position: Position::new(0, 3), + ..FeatureSpec::default() + }, + ); + assert_eq!(hover, None); + } + + #[test] + fn test_entry_key() { + let hover = test_feature( + BibtexEntryTypeHoverProvider, + FeatureSpec { + files: vec![FeatureSpec::file("foo.bib", "@article{foo,}")], + main_file: "foo.bib", + position: Position::new(0, 11), + ..FeatureSpec::default() + }, + ); + assert_eq!(hover, None); + } + + #[test] + fn test_latex() { + let hover = test_feature( + BibtexEntryTypeHoverProvider, + FeatureSpec { + files: vec![FeatureSpec::file("foo.tex", "\\foo")], + main_file: "foo.tex", + position: Position::new(0, 3), + ..FeatureSpec::default() + }, + ); + assert_eq!(hover, None); + } +} -- cgit v1.2.3