diff options
Diffstat (limited to 'support/texlab/src/reference/latex_label.rs')
-rw-r--r-- | support/texlab/src/reference/latex_label.rs | 307 |
1 files changed, 169 insertions, 138 deletions
diff --git a/support/texlab/src/reference/latex_label.rs b/support/texlab/src/reference/latex_label.rs index 424429b60e..16686f6015 100644 --- a/support/texlab/src/reference/latex_label.rs +++ b/support/texlab/src/reference/latex_label.rs @@ -1,59 +1,58 @@ -use crate::range::RangeExt; -use crate::syntax::*; -use crate::workspace::*; -use futures_boxed::boxed; -use lsp_types::{Location, ReferenceParams}; +use crate::{ + feature::{FeatureProvider, FeatureRequest}, + protocol::{Location, RangeExt, ReferenceParams}, + syntax::{latex, LatexLabelKind, SyntaxNode}, + workspace::DocumentContent, +}; +use async_trait::async_trait; -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)] pub struct LatexLabelReferenceProvider; +#[async_trait] impl FeatureProvider for LatexLabelReferenceProvider { type Params = ReferenceParams; type Output = Vec<Location>; - #[boxed] - async fn execute<'a>(&'a self, request: &'a FeatureRequest<ReferenceParams>) -> Vec<Location> { - let mut references = Vec::new(); - if let Some(definition) = Self::find_name(request) { - for document in request.related_documents() { - if let SyntaxTree::Latex(tree) = &document.tree { - tree.structure + async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output { + let mut refs = Vec::new(); + if let Some(def) = Self::find_name(req) { + for doc in req.related() { + if let DocumentContent::Latex(table) = &doc.content { + table .labels .iter() - .filter(|label| Self::is_included(request, label)) - .flat_map(LatexLabel::names) - .filter(|label| label.text() == definition) - .map(|label| Location::new(document.uri.clone().into(), label.range())) - .for_each(|location| references.push(location)) + .filter(|label| Self::is_included(req, label)) + .flat_map(|label| label.names(&table)) + .filter(|label| label.text() == def) + .map(|label| Location::new(doc.uri.clone().into(), label.range())) + .for_each(|location| refs.push(location)); } } } - references + refs } } impl LatexLabelReferenceProvider { - fn find_name(request: &FeatureRequest<ReferenceParams>) -> Option<&str> { - if let SyntaxTree::Latex(tree) = &request.document().tree { - tree.structure + fn find_name(req: &FeatureRequest<ReferenceParams>) -> Option<&str> { + let pos = req.params.text_document_position.position; + if let DocumentContent::Latex(table) = &req.current().content { + table .labels .iter() - .flat_map(LatexLabel::names) - .find(|label| { - label - .range() - .contains(request.params.text_document_position.position) - }) - .map(LatexToken::text) + .flat_map(|label| label.names(&table)) + .find(|label| label.range().contains(pos)) + .map(latex::Token::text) } else { None } } - fn is_included(request: &FeatureRequest<ReferenceParams>, label: &LatexLabel) -> bool { + fn is_included(req: &FeatureRequest<ReferenceParams>, label: &latex::Label) -> bool { match label.kind { LatexLabelKind::Reference(_) => true, - LatexLabelKind::Definition => request.params.context.include_declaration, + LatexLabelKind::Definition => req.params.context.include_declaration, } } } @@ -61,120 +60,152 @@ impl LatexLabelReferenceProvider { #[cfg(test)] mod tests { use super::*; - use crate::range::RangeExt; - use lsp_types::{Position, Range}; - - #[test] - fn test_definition() { - let references = test_feature( - LatexLabelReferenceProvider, - FeatureSpec { - files: vec![ - FeatureSpec::file("foo.tex", "\\label{foo}"), - FeatureSpec::file("bar.tex", "\\input{foo.tex}\n\\ref{foo}"), - FeatureSpec::file("baz.tex", "\\ref{foo}"), - ], - main_file: "foo.tex", - include_declaration: false, - position: Position::new(0, 8), - ..FeatureSpec::default() - }, - ); - assert_eq!( - references, - vec![Location::new( - FeatureSpec::uri("bar.tex"), - Range::new_simple(1, 5, 1, 8) - )] - ); + use crate::{feature::FeatureTester, protocol::Range}; + use indoc::indoc; + + #[tokio::test] + async fn definition() { + let actual_refs = FeatureTester::new() + .file("foo.tex", r#"\label{foo}"#) + .file( + "bar.tex", + indoc!( + r#" + \input{foo.tex} + \ref{foo} + "# + ), + ) + .file("baz.tex", r#"\ref{foo}"#) + .main("foo.tex") + .position(0, 8) + .test_reference(LatexLabelReferenceProvider) + .await; + + let expected_refs = vec![Location::new( + FeatureTester::uri("bar.tex").into(), + Range::new_simple(1, 5, 1, 8), + )]; + + assert_eq!(actual_refs, expected_refs); } - #[test] - fn test_definition_include_declaration() { - let references = test_feature( - LatexLabelReferenceProvider, - FeatureSpec { - files: vec![ - FeatureSpec::file("foo.tex", "\\label{foo}"), - FeatureSpec::file("bar.tex", "\\input{foo.tex}\n\\ref{foo}"), - FeatureSpec::file("baz.tex", "\\ref{foo}"), - ], - main_file: "foo.tex", - include_declaration: true, - position: Position::new(0, 8), - ..FeatureSpec::default() - }, - ); - assert_eq!( - references, - vec![ - Location::new(FeatureSpec::uri("foo.tex"), Range::new_simple(0, 7, 0, 10)), - Location::new(FeatureSpec::uri("bar.tex"), Range::new_simple(1, 5, 1, 8)), - ] - ); + #[tokio::test] + async fn definition_include_declaration() { + let actual_refs = FeatureTester::new() + .file("foo.tex", r#"\label{foo}"#) + .file( + "bar.tex", + indoc!( + r#" + \input{foo.tex} + \ref{foo} + "# + ), + ) + .file("baz.tex", r#"\ref{foo}"#) + .main("foo.tex") + .position(0, 8) + .include_declaration() + .test_reference(LatexLabelReferenceProvider) + .await; + + let expected_refs = vec![ + Location::new( + FeatureTester::uri("foo.tex").into(), + Range::new_simple(0, 7, 0, 10), + ), + Location::new( + FeatureTester::uri("bar.tex").into(), + Range::new_simple(1, 5, 1, 8), + ), + ]; + + assert_eq!(actual_refs, expected_refs); } - #[test] - fn test_reference() { - let references = test_feature( - LatexLabelReferenceProvider, - FeatureSpec { - files: vec![ - FeatureSpec::file("foo.tex", "\\label{foo}"), - FeatureSpec::file("bar.tex", "\\input{foo.tex}\n\\ref{foo}"), - FeatureSpec::file("baz.tex", "\\ref{foo}"), - ], - main_file: "bar.tex", - position: Position::new(1, 7), - include_declaration: false, - ..FeatureSpec::default() - }, - ); - assert_eq!( - references, - vec![Location::new( - FeatureSpec::uri("bar.tex"), - Range::new_simple(1, 5, 1, 8) - ),] - ); + #[tokio::test] + async fn reference() { + let actual_refs = FeatureTester::new() + .file("foo.tex", r#"\label{foo}"#) + .file( + "bar.tex", + indoc!( + r#" + \input{foo.tex} + \ref{foo} + "# + ), + ) + .file("baz.tex", r#"\ref{foo}"#) + .main("bar.tex") + .position(1, 7) + .test_reference(LatexLabelReferenceProvider) + .await; + + let expected_refs = vec![Location::new( + FeatureTester::uri("bar.tex").into(), + Range::new_simple(1, 5, 1, 8), + )]; + + assert_eq!(actual_refs, expected_refs); } - #[test] - fn test_reference_include_declaration() { - let references = test_feature( - LatexLabelReferenceProvider, - FeatureSpec { - files: vec![ - FeatureSpec::file("foo.tex", "\\label{foo}"), - FeatureSpec::file("bar.tex", "\\input{foo.tex}\n\\ref{foo}"), - FeatureSpec::file("baz.tex", "\\ref{foo}"), - ], - main_file: "bar.tex", - position: Position::new(1, 7), - include_declaration: true, - ..FeatureSpec::default() - }, - ); - assert_eq!( - references, - vec![ - Location::new(FeatureSpec::uri("bar.tex"), Range::new_simple(1, 5, 1, 8)), - Location::new(FeatureSpec::uri("foo.tex"), Range::new_simple(0, 7, 0, 10)), - ] - ); + #[tokio::test] + async fn reference_include_declaration() { + let actual_refs = FeatureTester::new() + .file("foo.tex", r#"\label{foo}"#) + .file( + "bar.tex", + indoc!( + r#" + \input{foo.tex} + \ref{foo} + "# + ), + ) + .file("baz.tex", r#"\ref{foo}"#) + .main("bar.tex") + .position(1, 7) + .include_declaration() + .test_reference(LatexLabelReferenceProvider) + .await; + + let expected_refs = vec![ + Location::new( + FeatureTester::uri("bar.tex").into(), + Range::new_simple(1, 5, 1, 8), + ), + Location::new( + FeatureTester::uri("foo.tex").into(), + Range::new_simple(0, 7, 0, 10), + ), + ]; + + assert_eq!(actual_refs, expected_refs); + } + + #[tokio::test] + async fn empty_latex_document() { + let actual_refs = FeatureTester::new() + .file("main.tex", "") + .main("main.tex") + .position(0, 0) + .test_reference(LatexLabelReferenceProvider) + .await; + + assert!(actual_refs.is_empty()); } - #[test] - fn test_bibtex() { - let references = test_feature( - LatexLabelReferenceProvider, - FeatureSpec { - files: vec![FeatureSpec::file("foo.bib", "")], - main_file: "foo.bib", - position: Position::new(0, 0), - ..FeatureSpec::default() - }, - ); - assert!(references.is_empty()); + #[tokio::test] + async fn empty_bibtex_document() { + let actual_refs = FeatureTester::new() + .file("main.bib", "") + .main("main.bib") + .position(0, 0) + .test_reference(LatexLabelReferenceProvider) + .await; + + assert!(actual_refs.is_empty()); } } |