summaryrefslogtreecommitdiff
path: root/support/texlab/crates/texlab/src/features/folding.rs
blob: 6de4ef0d221c9af483ae2ceaf6232bed001b57b6 (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
use base_db::Workspace;
use folding::FoldingRangeKind;
use lsp_types::{ClientCapabilities, Url};

use crate::util::line_index_ext::LineIndexExt;

pub fn find_all(
    workspace: &Workspace,
    uri: &Url,
    capabilities: &ClientCapabilities,
) -> Option<Vec<serde_json::Value>> {
    let custom_kinds = capabilities
        .text_document
        .as_ref()
        .and_then(|cap| cap.folding_range.as_ref())
        .and_then(|cap| cap.folding_range_kind.as_ref())
        .and_then(|cap| cap.value_set.as_ref())
        .is_some();

    let document = workspace.lookup(uri)?;
    let foldings = folding::find_all(document).into_iter().map(|folding| {
        let range = document.line_index.line_col_lsp_range(folding.range);

        let kind = if custom_kinds {
            Some(match folding.kind {
                FoldingRangeKind::Section => "section",
                FoldingRangeKind::Environment => "environment",
                FoldingRangeKind::Entry => "entry",
            })
        } else {
            None
        };

        serde_json::json!({
            "startLine": range.start.line,
            "startCharacter": range.start.character,
            "endLine": range.end.line,
            "endCharacter": range.end.character,
            "kind": kind,
        })
    });

    Some(foldings.collect())
}