diff options
Diffstat (limited to 'support/texlab/crates/completion/src/providers/import.rs')
-rw-r--r-- | support/texlab/crates/completion/src/providers/import.rs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/support/texlab/crates/completion/src/providers/import.rs b/support/texlab/crates/completion/src/providers/import.rs new file mode 100644 index 0000000000..c506026a99 --- /dev/null +++ b/support/texlab/crates/completion/src/providers/import.rs @@ -0,0 +1,69 @@ +use rowan::ast::AstNode; +use rustc_hash::FxHashSet; +use syntax::latex; + +use crate::{ + util::{find_curly_group_word_list, CompletionBuilder}, + CompletionItem, CompletionItemData, CompletionParams, +}; + +pub fn complete_imports<'a>( + params: &'a CompletionParams<'a>, + builder: &mut CompletionBuilder<'a>, +) -> Option<()> { + let (cursor, group) = find_curly_group_word_list(params)?; + + let kind = group.syntax().parent()?.kind(); + let extension = match kind { + latex::PACKAGE_INCLUDE => "sty", + latex::CLASS_INCLUDE => "cls", + _ => return Some(()), + }; + + let mut file_names = FxHashSet::default(); + for file_name in completion_data::DATABASE + .iter() + .flat_map(|package| package.file_names.iter()) + .filter(|file_name| file_name.ends_with(extension)) + { + file_names.insert(file_name); + let stem = &file_name[0..file_name.len() - 4]; + if kind == latex::PACKAGE_INCLUDE { + if let Some(score) = builder.matcher.score(&stem, &cursor.text) { + let data = CompletionItemData::Package(stem); + builder + .items + .push(CompletionItem::new_simple(score, cursor.range, data)); + } + } else if let Some(score) = builder.matcher.score(&stem, &cursor.text) { + let data = CompletionItemData::DocumentClass(stem); + builder + .items + .push(CompletionItem::new_simple(score, cursor.range, data)); + } + } + + let file_name_db = ¶ms.feature.workspace.distro().file_name_db; + for file_name in file_name_db + .iter() + .map(|(file_name, _)| file_name) + .filter(|file_name| file_name.ends_with(extension) && !file_names.contains(file_name)) + { + let stem = &file_name[0..file_name.len() - 4]; + if kind == latex::PACKAGE_INCLUDE { + if let Some(score) = builder.matcher.score(&stem, &cursor.text) { + let data = CompletionItemData::Package(stem); + builder + .items + .push(CompletionItem::new_simple(score, cursor.range, data)); + } + } else if let Some(score) = builder.matcher.score(&stem, &cursor.text) { + let data = CompletionItemData::DocumentClass(stem); + builder + .items + .push(CompletionItem::new_simple(score, cursor.range, data)); + } + } + + Some(()) +} |