tinymist_query/
workspace_label.rs

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
70
71
72
73
74
75
76
77
78
use crate::{
    prelude::*,
    syntax::{
        get_lexical_hierarchy, LexicalHierarchy, LexicalKind, LexicalScopeKind, LexicalVarKind,
    },
    SemanticRequest,
};

/// The `workspace/label` request resembles [`workspace/symbol`] request but is
/// extended for typst cases.
///
/// [`workspace/symbol`]: https://microsoft.github.io/language-server-protocol/specification#workspace_symbol
#[derive(Debug, Clone)]
pub struct WorkspaceLabelRequest {}

impl SemanticRequest for WorkspaceLabelRequest {
    type Response = Vec<SymbolInformation>;

    fn request(self, ctx: &mut LocalContext) -> Option<Self::Response> {
        // todo: let typst.ts expose source

        let mut symbols = vec![];

        for fid in ctx.source_files().clone() {
            let Ok(source) = ctx.source_by_id(fid) else {
                continue;
            };
            let Ok(uri) = ctx.uri_for_id(fid) else {
                continue;
            };
            let res = get_lexical_hierarchy(&source, LexicalScopeKind::Symbol).map(|hierarchy| {
                filter_document_labels(&hierarchy, &source, &uri, ctx.position_encoding())
            });

            if let Some(mut res) = res {
                symbols.append(&mut res)
            }
        }

        Some(symbols)
    }
}

#[allow(deprecated)]
fn filter_document_labels(
    hierarchy: &[LexicalHierarchy],
    source: &Source,
    uri: &Url,
    position_encoding: PositionEncoding,
) -> Vec<SymbolInformation> {
    hierarchy
        .iter()
        .flat_map(|hierarchy| {
            [hierarchy]
                .into_iter()
                .chain(hierarchy.children.as_deref().into_iter().flatten())
        })
        .flat_map(|hierarchy| {
            if !matches!(hierarchy.info.kind, LexicalKind::Var(LexicalVarKind::Label)) {
                return None;
            }

            let rng = to_lsp_range(hierarchy.info.range.clone(), source, position_encoding);

            Some(SymbolInformation {
                name: hierarchy.info.name.to_string(),
                kind: hierarchy.info.kind.clone().try_into().unwrap(),
                tags: None,
                deprecated: None,
                location: LspLocation {
                    uri: uri.clone(),
                    range: rng,
                },
                container_name: None,
            })
        })
        .collect()
}