diff options
Diffstat (limited to 'support/texlab/src/highlight/latex_label.rs')
-rw-r--r-- | support/texlab/src/highlight/latex_label.rs | 144 |
1 files changed, 73 insertions, 71 deletions
diff --git a/support/texlab/src/highlight/latex_label.rs b/support/texlab/src/highlight/latex_label.rs index a586a7a1bf..c6d4aab6ee 100644 --- a/support/texlab/src/highlight/latex_label.rs +++ b/support/texlab/src/highlight/latex_label.rs @@ -1,40 +1,40 @@ -use crate::range::RangeExt; -use crate::syntax::*; -use crate::workspace::*; -use futures_boxed::boxed; -use lsp_types::{DocumentHighlight, DocumentHighlightKind, TextDocumentPositionParams}; +use crate::{ + feature::{FeatureProvider, FeatureRequest}, + protocol::{DocumentHighlight, DocumentHighlightKind, RangeExt, TextDocumentPositionParams}, + 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 LatexLabelHighlightProvider; +#[async_trait] impl FeatureProvider for LatexLabelHighlightProvider { type Params = TextDocumentPositionParams; type Output = Vec<DocumentHighlight>; - #[boxed] - async fn execute<'a>( - &'a self, - request: &'a FeatureRequest<TextDocumentPositionParams>, - ) -> Vec<DocumentHighlight> { + async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output { let mut highlights = Vec::new(); - if let SyntaxTree::Latex(tree) = &request.document().tree { - if let Some(name) = tree - .structure + if let DocumentContent::Latex(table) = &req.current().content { + if let Some(name) = table .labels .iter() - .flat_map(LatexLabel::names) - .find(|label| label.range().contains(request.params.position)) - .map(|label| label.text()) + .flat_map(|label| label.names(&table)) + .find(|label| label.range().contains(req.params.position)) + .map(latex::Token::text) { - for label_group in &tree.structure.labels { - for label in label_group.names() { + for label_group in &table.labels { + for label in label_group.names(&table) { if label.text() == name { + let kind = match label_group.kind { + LatexLabelKind::Definition => DocumentHighlightKind::Write, + LatexLabelKind::Reference(_) => DocumentHighlightKind::Read, + }; + let highlight = DocumentHighlight { range: label.range(), - kind: Some(match label_group.kind { - LatexLabelKind::Definition => DocumentHighlightKind::Write, - LatexLabelKind::Reference(_) => DocumentHighlightKind::Read, - }), + kind: Some(kind), }; highlights.push(highlight); } @@ -49,59 +49,61 @@ impl FeatureProvider for LatexLabelHighlightProvider { #[cfg(test)] mod tests { use super::*; - use lsp_types::{Position, Range}; + use crate::{feature::FeatureTester, protocol::Range}; + use indoc::indoc; + + #[tokio::test] + async fn has_label() { + let actual_highlights = FeatureTester::new() + .file( + "foo.tex", + indoc!( + r#" + \label{foo} + \ref{foo} + "# + ), + ) + .main("foo.tex") + .position(0, 7) + .test_position(LatexLabelHighlightProvider) + .await; - #[test] - fn test_has_label() { - let highlights = test_feature( - LatexLabelHighlightProvider, - FeatureSpec { - files: vec![FeatureSpec::file("foo.tex", "\\label{foo}\n\\ref{foo}")], - main_file: "foo.tex", - position: Position::new(0, 7), - ..FeatureSpec::default() + let expected_highlights = vec![ + DocumentHighlight { + range: Range::new_simple(0, 7, 0, 10), + kind: Some(DocumentHighlightKind::Write), }, - ); - assert_eq!( - highlights, - vec![ - DocumentHighlight { - range: Range::new_simple(0, 7, 0, 10), - kind: Some(DocumentHighlightKind::Write), - }, - DocumentHighlight { - range: Range::new_simple(1, 5, 1, 8), - kind: Some(DocumentHighlightKind::Read), - } - ] - ); + DocumentHighlight { + range: Range::new_simple(1, 5, 1, 8), + kind: Some(DocumentHighlightKind::Read), + }, + ]; + + assert_eq!(actual_highlights, expected_highlights); } - #[test] - fn test_no_label_latex() { - let highlights = test_feature( - LatexLabelHighlightProvider, - FeatureSpec { - files: vec![FeatureSpec::file("foo.tex", "")], - main_file: "foo.tex", - position: Position::new(0, 0), - ..FeatureSpec::default() - }, - ); - assert!(highlights.is_empty()); + #[tokio::test] + async fn no_label_latex() { + let actual_highlights = FeatureTester::new() + .file("foo.tex", "") + .main("foo.tex") + .position(0, 0) + .test_position(LatexLabelHighlightProvider) + .await; + + assert!(actual_highlights.is_empty()); } - #[test] - fn test_no_label_bibtex() { - let highlights = test_feature( - LatexLabelHighlightProvider, - FeatureSpec { - files: vec![FeatureSpec::file("foo.bib", "")], - main_file: "foo.bib", - position: Position::new(0, 0), - ..FeatureSpec::default() - }, - ); - assert!(highlights.is_empty()); + #[tokio::test] + async fn no_label_bibtex() { + let actual_highlights = FeatureTester::new() + .file("foo.bib", "") + .main("foo.bib") + .position(0, 0) + .test_position(LatexLabelHighlightProvider) + .await; + + assert!(actual_highlights.is_empty()); } } |