summaryrefslogtreecommitdiff
path: root/support/texlab/crates/completion/src/providers/import.rs
blob: c0f608f9d26f09e216f02e9ae7d57459e43327fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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 = &params.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(())
}