summaryrefslogtreecommitdiff
path: root/support/texlab/crates/references/src/string_def.rs
blob: 9657686079c40a73da1c44d2093655005a6ecb5a (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
use rowan::ast::AstNode;
use syntax::bibtex;

use crate::{Reference, ReferenceContext, ReferenceKind};

pub(super) fn find_all(context: &mut ReferenceContext) -> Option<()> {
    let document = context.params.document;
    let data = document.data.as_bib()?;
    let root = data.root_node();
    let name = root
        .token_at_offset(context.params.offset)
        .filter(|token| token.kind() == bibtex::NAME)
        .find(|token| {
            let parent = token.parent().unwrap();
            bibtex::Value::can_cast(parent.kind()) || bibtex::StringDef::can_cast(parent.kind())
        })?;

    for string in &data.semantics.strings {
        if string.name.text == name.text() {
            context.results.push(Reference {
                document,
                range: string.name.range,
                kind: ReferenceKind::Definition,
            });
        }
    }

    for token in root
        .descendants()
        .filter_map(bibtex::Value::cast)
        .filter_map(|token| token.syntax().first_token())
        .filter(|token| token.text() == name.text())
    {
        context.results.push(Reference {
            document,
            range: token.text_range(),
            kind: ReferenceKind::Reference,
        });
    }

    Some(())
}