diff options
Diffstat (limited to 'support/texlab/src/completion/latex/color.rs')
-rw-r--r-- | support/texlab/src/completion/latex/color.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/support/texlab/src/completion/latex/color.rs b/support/texlab/src/completion/latex/color.rs new file mode 100644 index 0000000000..b2d390b7d8 --- /dev/null +++ b/support/texlab/src/completion/latex/color.rs @@ -0,0 +1,74 @@ +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}; + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub struct LatexColorCompletionProvider; + +impl FeatureProvider for LatexColorCompletionProvider { + type Params = CompletionParams; + type Output = Vec<CompletionItem>; + + #[boxed] + async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> 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 + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::range::RangeExt; + use lsp_types::{Position, Range}; + + #[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)) + ); + } + + #[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()); + } +} |