diff options
author | Norbert Preining <norbert@preining.info> | 2020-05-28 03:03:21 +0000 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2020-05-28 03:03:21 +0000 |
commit | 745892fbddea56040139108277e728b53fd8fc11 (patch) | |
tree | 15e55e299b38fccdabce8364f1fbf4f02ef37994 /support/texlab/src/link/latex_include.rs | |
parent | 195ca7e0c377d83455867bdd8e409d4e1cf024ea (diff) |
CTAN sync 202005280303
Diffstat (limited to 'support/texlab/src/link/latex_include.rs')
-rw-r--r-- | support/texlab/src/link/latex_include.rs | 137 |
1 files changed, 64 insertions, 73 deletions
diff --git a/support/texlab/src/link/latex_include.rs b/support/texlab/src/link/latex_include.rs index 0cc6c36364..5f4f592f14 100644 --- a/support/texlab/src/link/latex_include.rs +++ b/support/texlab/src/link/latex_include.rs @@ -1,49 +1,49 @@ -use crate::syntax::*; -use crate::workspace::*; -use futures_boxed::boxed; -use lsp_types::{DocumentLink, DocumentLinkParams}; +use crate::{ + feature::{FeatureProvider, FeatureRequest}, + protocol::{DocumentLink, DocumentLinkParams}, + syntax::{latex, SyntaxNode}, + workspace::DocumentContent, +}; +use async_trait::async_trait; -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)] pub struct LatexIncludeLinkProvider; +#[async_trait] impl FeatureProvider for LatexIncludeLinkProvider { type Params = DocumentLinkParams; type Output = Vec<DocumentLink>; - #[boxed] - async fn execute<'a>( - &'a self, - request: &'a FeatureRequest<DocumentLinkParams>, - ) -> Vec<DocumentLink> { - if let SyntaxTree::Latex(tree) = &request.document().tree { - return tree + async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output { + if let DocumentContent::Latex(table) = &req.current().content { + table .includes .iter() - .flat_map(|include| Self::resolve(request, include)) - .collect(); + .flat_map(|include| Self::resolve(req, table, include)) + .collect() + } else { + Vec::new() } - Vec::new() } } impl LatexIncludeLinkProvider { fn resolve( - request: &FeatureRequest<DocumentLinkParams>, - include: &LatexInclude, + req: &FeatureRequest<DocumentLinkParams>, + table: &latex::SymbolTable, + include: &latex::Include, ) -> Vec<DocumentLink> { let mut links = Vec::new(); - let paths = include.paths(); + let paths = include.paths(&table); for (i, targets) in include.all_targets.iter().enumerate() { for target in targets { - if let Some(link) = request - .workspace() - .find(target) - .map(|document| DocumentLink { - range: paths[i].range(), - target: document.uri.clone().into(), - }) - { + if let Some(link) = req.snapshot().find(target).map(|doc| DocumentLink { + range: paths[i].range(), + target: doc.uri.clone().into(), + tooltip: None, + }) { links.push(link); + break; } } } @@ -54,57 +54,48 @@ impl LatexIncludeLinkProvider { #[cfg(test)] mod tests { use super::*; - use crate::range::RangeExt; - use lsp_types::{Position, Range}; + use crate::{ + feature::FeatureTester, + protocol::{Range, RangeExt}, + }; + + #[tokio::test] + async fn empty_latex_document_command() { + let actual_links = FeatureTester::new() + .file("main.tex", "") + .main("main.tex") + .test_link(LatexIncludeLinkProvider) + .await; - #[test] - fn test_has_links() { - let links = test_feature( - LatexIncludeLinkProvider, - FeatureSpec { - files: vec![ - FeatureSpec::file("foo.tex", "\\input{bar.tex}"), - FeatureSpec::file("bar.tex", ""), - ], - main_file: "foo.tex", - position: Position::new(0, 15), - ..FeatureSpec::default() - }, - ); - assert_eq!( - links, - vec![DocumentLink { - range: Range::new_simple(0, 7, 0, 14), - target: FeatureSpec::uri("bar.tex"), - }] - ); + assert!(actual_links.is_empty()); } - #[test] - fn test_no_links_latex() { - let links = test_feature( - LatexIncludeLinkProvider, - FeatureSpec { - files: vec![FeatureSpec::file("foo.tex", "")], - main_file: "foo.tex", - position: Position::new(0, 15), - ..FeatureSpec::default() - }, - ); - assert!(links.is_empty()); + #[tokio::test] + async fn empty_bibtex_document_command() { + let actual_links = FeatureTester::new() + .file("main.bib", "") + .main("main.bib") + .test_link(LatexIncludeLinkProvider) + .await; + + assert!(actual_links.is_empty()); } - #[test] - fn test_no_links_bibtex() { - let links = test_feature( - LatexIncludeLinkProvider, - FeatureSpec { - files: vec![FeatureSpec::file("foo.bib", "")], - main_file: "foo.bib", - position: Position::new(0, 15), - ..FeatureSpec::default() - }, - ); - assert!(links.is_empty()); + #[tokio::test] + async fn has_links() { + let actual_links = FeatureTester::new() + .file("foo.tex", r#"\input{bar.tex}"#) + .file("bar.tex", r#""#) + .main("foo.tex") + .test_link(LatexIncludeLinkProvider) + .await; + + let expected_links = vec![DocumentLink { + range: Range::new_simple(0, 7, 0, 14), + target: FeatureTester::uri("bar.tex").into(), + tooltip: None, + }]; + + assert_eq!(actual_links, expected_links); } } |