Skip to content

Commit

Permalink
Provide completion for argument symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed May 16, 2019
1 parent 2fbb70f commit 8922c39
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 37 deletions.
47 changes: 10 additions & 37 deletions src/completion/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ pub fn create_command_symbol(
}
}

pub fn create_argument_symbol(name: &'static str, image: &str) -> CompletionItem {
CompletionItem {
label: Cow::from(name),
kind: Some(CompletionItemKind::Field),
data: Some(CompletionItemData::ArgumentSymbol.into()),
documentation: Some(Documentation::MarkupContent(create_image(name, image))),
..CompletionItem::default()
}
}

fn create_image(name: &str, image: &str) -> MarkupContent {
return MarkupContent {
kind: MarkupKind::Markdown,
Expand All @@ -201,43 +211,6 @@ fn create_image(name: &str, image: &str) -> MarkupContent {
};
}

/*
export function createCommandSymbol(
name: string,
component: string | undefined,
image: string,
): CompletionItem {
const detail = getDetail(component);
return {
label: name,
detail,
kind: LspCompletionItemKind.Function,
data: { kind: CompletionItemKind.CommandSymbol },
documentation: createImage(name, image),
};
}
export function createArgumentSymbol(
name: string,
image: string,
): CompletionItem {
return {
label: name,
kind: LspCompletionItemKind.Field,
data: { kind: CompletionItemKind.ArgumentSymbol },
documentation: createImage(name, image),
};
}
function createImage(name: string, image: string): MarkupContent {
return {
kind: MarkupKind.Markdown,
value: `![${name}](data:image/png;base64,${image}|width=48,height=48)`,
};
}
*/

pub fn create_entry_type(ty: &'static BibtexEntryType) -> CompletionItem {
CompletionItem {
label: Cow::from(&ty.name),
Expand Down
72 changes: 72 additions & 0 deletions src/completion/latex/argument_symbol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use crate::completion::factory;
use crate::completion::latex::combinators::LatexCombinators;
use crate::data::symbols::DATABASE;
use crate::feature::FeatureRequest;
use lsp_types::{CompletionItem, CompletionParams};

pub struct LatexArgumentSymbolCompletionProvider;

impl LatexArgumentSymbolCompletionProvider {
pub async fn execute(request: &FeatureRequest<CompletionParams>) -> Vec<CompletionItem> {
let mut items = Vec::new();
for group in &DATABASE.arguments {
let command = format!("\\{}", group.command);
let command_names = &[command.as_ref()];
items.append(&mut await!(LatexCombinators::argument(
&request,
command_names,
group.index,
async move |_| {
let mut items = Vec::new();
for symbol in &group.arguments {
items.push(factory::create_argument_symbol(
&symbol.argument,
&symbol.image,
));
}
items
}
)));
}
items
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::completion::latex::data::types::LatexComponentDatabase;
use crate::feature::FeatureSpec;
use crate::test_feature;
use lsp_types::Position;

#[test]
fn test_inside_mathbb() {
let items = test_feature!(
LatexArgumentSymbolCompletionProvider,
FeatureSpec {
files: vec![FeatureSpec::file("foo.tex", "\\mathbb{}")],
main_file: "foo.tex",
position: Position::new(0, 8),
new_name: "",
component_database: LatexComponentDatabase::default(),
}
);
assert_eq!(true, items.len() > 0);
}

#[test]
fn test_outside_mathbb() {
let items = test_feature!(
LatexArgumentSymbolCompletionProvider,
FeatureSpec {
files: vec![FeatureSpec::file("foo.tex", "\\mathbb{}")],
main_file: "foo.tex",
position: Position::new(0, 9),
new_name: "",
component_database: LatexComponentDatabase::default(),
}
);
assert_eq!(true, items.len() == 0);
}
}
1 change: 1 addition & 0 deletions src/completion/latex/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod argument_symbol;
pub mod begin_command;
pub mod citation;
pub mod color;
Expand Down
2 changes: 2 additions & 0 deletions src/completion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod quality;
use self::bibtex::entry_type::BibtexEntryTypeCompletionProvider;
use self::bibtex::field_name::BibtexFieldNameCompletionProvider;
use self::bibtex::kernel_command::BibtexKernelCommandCompletionProvider;
use self::latex::argument_symbol::LatexArgumentSymbolCompletionProvider;
use self::latex::begin_command::LatexBeginCommandCompletionProvider;
use self::latex::citation::LatexCitationCompletionProvider;
use self::latex::color::LatexColorCompletionProvider;
Expand Down Expand Up @@ -40,6 +41,7 @@ impl CompletionProvider {
BibtexFieldNameCompletionProvider,
BibtexKernelCommandCompletionProvider,
LatexKernelEnvironmentCompletionProvider,
LatexArgumentSymbolCompletionProvider,
LatexPgfLibraryCompletionProvider,
LatexTikzLibraryCompletionProvider,
LatexColorCompletionProvider,
Expand Down

0 comments on commit 8922c39

Please sign in to comment.