From 745892fbddea56040139108277e728b53fd8fc11 Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Thu, 28 May 2020 03:03:21 +0000 Subject: CTAN sync 202005280303 --- support/texlab/src/symbol/bibtex_string.rs | 115 +++++++++++++++++------------ 1 file changed, 69 insertions(+), 46 deletions(-) (limited to 'support/texlab/src/symbol/bibtex_string.rs') diff --git a/support/texlab/src/symbol/bibtex_string.rs b/support/texlab/src/symbol/bibtex_string.rs index 8f1f436048..d2283f640b 100644 --- a/support/texlab/src/symbol/bibtex_string.rs +++ b/support/texlab/src/symbol/bibtex_string.rs @@ -1,25 +1,28 @@ -use super::{LatexSymbol, LatexSymbolKind}; -use crate::syntax::*; -use crate::workspace::*; -use futures_boxed::boxed; -use lsp_types::*; +use super::types::{LatexSymbol, LatexSymbolKind}; +use crate::{ + feature::{FeatureProvider, FeatureRequest}, + protocol::DocumentSymbolParams, + syntax::SyntaxNode, + workspace::DocumentContent, +}; +use async_trait::async_trait; -#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)] pub struct BibtexStringSymbolProvider; +#[async_trait] impl FeatureProvider for BibtexStringSymbolProvider { type Params = DocumentSymbolParams; type Output = Vec; - #[boxed] - async fn execute<'a>(&'a self, request: &'a FeatureRequest) -> Self::Output { + async fn execute<'a>(&'a self, req: &'a FeatureRequest) -> Self::Output { let mut symbols = Vec::new(); - if let SyntaxTree::Bibtex(tree) = &request.document().tree { - for child in &tree.root.children { - if let BibtexDeclaration::String(string) = &child { + if let DocumentContent::Bibtex(tree) = &req.current().content { + for string_node in tree.children(tree.root) { + if let Some(string) = &tree.as_string(string_node) { if let Some(name) = &string.name { symbols.push(LatexSymbol { - name: name.text().to_owned(), + name: name.text().into(), label: None, kind: LatexSymbolKind::String, deprecated: false, @@ -38,42 +41,62 @@ impl FeatureProvider for BibtexStringSymbolProvider { #[cfg(test)] mod tests { use super::*; - use crate::range::RangeExt; + use crate::{ + feature::FeatureTester, + protocol::{Range, RangeExt}, + }; - #[test] - fn test_valid() { - let symbols = test_feature( - BibtexStringSymbolProvider, - FeatureSpec { - files: vec![FeatureSpec::file("foo.bib", "@string{key = \"value\"}")], - main_file: "foo.bib", - ..FeatureSpec::default() - }, - ); - assert_eq!( - symbols, - vec![LatexSymbol { - name: "key".into(), - label: None, - kind: LatexSymbolKind::String, - deprecated: false, - full_range: Range::new_simple(0, 0, 0, 22), - selection_range: Range::new_simple(0, 8, 0, 11), - children: Vec::new(), - }] - ); + #[tokio::test] + async fn empty_latex_document() { + let actual_symbols = FeatureTester::new() + .file("main.tex", "") + .main("main.tex") + .test_symbol(BibtexStringSymbolProvider) + .await; + + assert!(actual_symbols.is_empty()); } - #[test] - fn test_invalid() { - let symbols = test_feature( - BibtexStringSymbolProvider, - FeatureSpec { - files: vec![FeatureSpec::file("foo.bib", "@string{}")], - main_file: "foo.bib", - ..FeatureSpec::default() - }, - ); - assert_eq!(symbols, Vec::new()); + #[tokio::test] + async fn empty_bibtex_document() { + let actual_symbols = FeatureTester::new() + .file("main.bib", "") + .main("main.bib") + .test_symbol(BibtexStringSymbolProvider) + .await; + + assert!(actual_symbols.is_empty()); + } + + #[tokio::test] + async fn valid() { + let actual_symbols = FeatureTester::new() + .file("main.bib", r#"@string{key = "value"}"#) + .main("main.bib") + .test_symbol(BibtexStringSymbolProvider) + .await; + + let expected_symbols = vec![LatexSymbol { + name: "key".into(), + label: None, + kind: LatexSymbolKind::String, + deprecated: false, + full_range: Range::new_simple(0, 0, 0, 22), + selection_range: Range::new_simple(0, 8, 0, 11), + children: Vec::new(), + }]; + + assert_eq!(actual_symbols, expected_symbols); + } + + #[tokio::test] + async fn invalid() { + let actual_symbols = FeatureTester::new() + .file("main.bib", r#"@string{}"#) + .main("main.bib") + .test_symbol(BibtexStringSymbolProvider) + .await; + + assert!(actual_symbols.is_empty()); } } -- cgit v1.2.3