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/folding/latex_section.rs | 145 +++++++++++++++------------- 1 file changed, 78 insertions(+), 67 deletions(-) (limited to 'support/texlab/src/folding/latex_section.rs') diff --git a/support/texlab/src/folding/latex_section.rs b/support/texlab/src/folding/latex_section.rs index ca7b8387be..a37efe1a7a 100644 --- a/support/texlab/src/folding/latex_section.rs +++ b/support/texlab/src/folding/latex_section.rs @@ -1,36 +1,37 @@ -use crate::syntax::*; -use crate::workspace::*; -use futures_boxed::boxed; -use lsp_types::{FoldingRange, FoldingRangeKind, FoldingRangeParams}; +use crate::{ + feature::{FeatureProvider, FeatureRequest}, + protocol::{FoldingRange, FoldingRangeKind, FoldingRangeParams}, + syntax::SyntaxNode, + workspace::DocumentContent, +}; +use async_trait::async_trait; -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)] pub struct LatexSectionFoldingProvider; +#[async_trait] impl FeatureProvider for LatexSectionFoldingProvider { type Params = FoldingRangeParams; type Output = Vec; - #[boxed] - async fn execute<'a>( - &'a self, - request: &'a FeatureRequest, - ) -> Vec { + async fn execute<'a>(&'a self, req: &'a FeatureRequest) -> Self::Output { let mut foldings = Vec::new(); - if let SyntaxTree::Latex(tree) = &request.document().tree { - let sections = &tree.structure.sections; + if let DocumentContent::Latex(table) = &req.current().content { + let sections = &table.sections; for i in 0..sections.len() { let current = §ions[i]; - let next = sections + if let Some(next) = sections .iter() .skip(i + 1) - .find(|sec| current.level >= sec.level); - - if let Some(next) = next { - if next.command.start().line > 0 { + .find(|sec| current.level >= sec.level) + { + let next_node = &table[next.parent]; + if next_node.start().line > 0 { + let current_node = &table[current.parent]; let folding = FoldingRange { - start_line: current.command.end().line, - start_character: Some(current.command.end().character), - end_line: next.command.start().line - 1, + start_line: current_node.end().line, + start_character: Some(current_node.end().character), + end_line: next_node.start().line - 1, end_character: Some(0), kind: Some(FoldingRangeKind::Region), }; @@ -46,55 +47,65 @@ impl FeatureProvider for LatexSectionFoldingProvider { #[cfg(test)] mod tests { use super::*; + use crate::feature::FeatureTester; + use indoc::indoc; - #[test] - fn test_nesting() { - let foldings = test_feature( - LatexSectionFoldingProvider, - FeatureSpec { - files: vec![FeatureSpec::file("foo.tex", "\\section{Foo}\nfoo\n\\subsection{Bar}\nbar\n\\section{Baz}\nbaz\n\\section{Qux}")], - main_file: "foo.tex", - ..FeatureSpec::default() - } - ); - assert_eq!( - foldings, - vec![ - FoldingRange { - start_line: 0, - start_character: Some(13), - end_line: 3, - end_character: Some(0), - kind: Some(FoldingRangeKind::Region), - }, - FoldingRange { - start_line: 2, - start_character: Some(16), - end_line: 3, - end_character: Some(0), - kind: Some(FoldingRangeKind::Region), - }, - FoldingRange { - start_line: 4, - start_character: Some(13), - end_line: 5, - end_character: Some(0), - kind: Some(FoldingRangeKind::Region), - } - ] - ); - } + #[tokio::test] + async fn nested() { + let actual_foldings = FeatureTester::new() + .file( + "main.tex", + indoc!( + r#" + \section{Foo} + foo + \subsection{Bar} + bar + \section{Baz} + baz + \section{Qux} + "# + ), + ) + .main("main.tex") + .test_folding(LatexSectionFoldingProvider) + .await; - #[test] - fn test_bibtex() { - let foldings = test_feature( - LatexSectionFoldingProvider, - FeatureSpec { - files: vec![FeatureSpec::file("foo.bib", "@article{foo, bar = baz}")], - main_file: "foo.bib", - ..FeatureSpec::default() + let expected_foldings = vec![ + FoldingRange { + start_line: 0, + start_character: Some(13), + end_line: 3, + end_character: Some(0), + kind: Some(FoldingRangeKind::Region), + }, + FoldingRange { + start_line: 2, + start_character: Some(16), + end_line: 3, + end_character: Some(0), + kind: Some(FoldingRangeKind::Region), }, - ); - assert!(foldings.is_empty()); + FoldingRange { + start_line: 4, + start_character: Some(13), + end_line: 5, + end_character: Some(0), + kind: Some(FoldingRangeKind::Region), + }, + ]; + + assert_eq!(actual_foldings, expected_foldings); + } + + #[tokio::test] + async fn bibtex() { + let actual_foldings = FeatureTester::new() + .file("main.bib", "") + .main("main.bib") + .test_folding(LatexSectionFoldingProvider) + .await; + + assert!(actual_foldings.is_empty()); } } -- cgit v1.2.3