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/completion/latex/color.rs | 139 +++++++++++++++------------ 1 file changed, 78 insertions(+), 61 deletions(-) (limited to 'support/texlab/src/completion/latex/color.rs') diff --git a/support/texlab/src/completion/latex/color.rs b/support/texlab/src/completion/latex/color.rs index b2d390b7d8..93b06b178e 100644 --- a/support/texlab/src/completion/latex/color.rs +++ b/support/texlab/src/completion/latex/color.rs @@ -1,74 +1,91 @@ use super::combinators::{self, Parameter}; -use crate::completion::factory; -use crate::syntax::*; -use crate::workspace::*; -use futures_boxed::boxed; -use lsp_types::{CompletionItem, CompletionParams, TextEdit}; +use crate::{ + completion::types::{Item, ItemData}, + feature::FeatureRequest, + protocol::CompletionParams, + syntax::LANGUAGE_DATA, +}; -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub struct LatexColorCompletionProvider; +pub async fn complete_latex_colors<'a>( + req: &'a FeatureRequest, + items: &mut Vec>, +) { + let parameters = LANGUAGE_DATA.color_commands.iter().map(|cmd| Parameter { + name: &cmd.name[1..], + index: cmd.index, + }); -impl FeatureProvider for LatexColorCompletionProvider { - type Params = CompletionParams; - type Output = Vec; - - #[boxed] - async fn execute<'a>(&'a self, request: &'a FeatureRequest) -> Self::Output { - let parameters = LANGUAGE_DATA - .color_commands - .iter() - .map(|cmd| Parameter::new(&cmd.name, cmd.index)); - - combinators::argument(request, parameters, |context| { - async move { - let mut items = Vec::new(); - for name in &LANGUAGE_DATA.colors { - let text_edit = TextEdit::new(context.range, name.into()); - let item = factory::color(request, name, text_edit); - items.push(item); - } - items - } - }) - .await - } + combinators::argument(req, parameters, |ctx| async move { + for name in &LANGUAGE_DATA.colors { + let item = Item::new(ctx.range, ItemData::Color { name }); + items.push(item); + } + }) + .await; } #[cfg(test)] mod tests { use super::*; - use crate::range::RangeExt; - use lsp_types::{Position, Range}; + use crate::{ + feature::FeatureTester, + protocol::{Range, RangeExt}, + }; - #[test] - fn test_inside_color() { - let items = test_feature( - LatexColorCompletionProvider, - FeatureSpec { - files: vec![FeatureSpec::file("foo.tex", "\\color{}")], - main_file: "foo.tex", - position: Position::new(0, 7), - ..FeatureSpec::default() - }, - ); - assert!(!items.is_empty()); - assert_eq!( - items[0].text_edit.as_ref().map(|edit| edit.range), - Some(Range::new_simple(0, 7, 0, 7)) - ); + #[tokio::test] + async fn empty_latex_document() { + let req = FeatureTester::new() + .file("main.tex", "") + .main("main.tex") + .position(0, 0) + .test_completion_request() + .await; + let mut actual_items = Vec::new(); + complete_latex_colors(&req, &mut actual_items).await; + + assert!(actual_items.is_empty()); } - #[test] - fn test_outside_color() { - let items = test_feature( - LatexColorCompletionProvider, - FeatureSpec { - files: vec![FeatureSpec::file("foo.tex", "\\color{}")], - main_file: "foo.tex", - position: Position::new(0, 8), - ..FeatureSpec::default() - }, - ); - assert!(items.is_empty()); + #[tokio::test] + async fn empty_bibtex_document() { + let req = FeatureTester::new() + .file("main.bib", "") + .main("main.bib") + .position(0, 0) + .test_completion_request() + .await; + let mut actual_items = Vec::new(); + complete_latex_colors(&req, &mut actual_items).await; + + assert!(actual_items.is_empty()); + } + + #[tokio::test] + async fn inside_color() { + let req = FeatureTester::new() + .file("main.tex", r#"\color{}"#) + .main("main.tex") + .position(0, 7) + .test_completion_request() + .await; + let mut actual_items = Vec::new(); + complete_latex_colors(&req, &mut actual_items).await; + + assert!(!actual_items.is_empty()); + assert_eq!(actual_items[0].range, Range::new_simple(0, 7, 0, 7)); + } + + #[tokio::test] + async fn inside_define_color_set() { + let req = FeatureTester::new() + .file("main.tex", r#"\color{}"#) + .main("main.tex") + .position(0, 8) + .test_completion_request() + .await; + let mut actual_items = Vec::new(); + complete_latex_colors(&req, &mut actual_items).await; + + assert!(actual_items.is_empty()); } } -- cgit v1.2.3