summaryrefslogtreecommitdiff
path: root/support/texlab/crates/texlab/src/features/highlight.rs
blob: 10c5d95fb12a8c42cbba59c6552707eb638e0030 (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
use base_db::{FeatureParams, Workspace};
use highlights::{HighlightKind, HighlightParams};

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

pub fn find_all(
    workspace: &Workspace,
    params: &lsp_types::DocumentHighlightParams,
) -> Option<Vec<lsp_types::DocumentHighlight>> {
    let uri = &params.text_document_position_params.text_document.uri;
    let document = workspace.lookup(uri)?;
    let position = params.text_document_position_params.position;
    let offset = document.line_index.offset_lsp(position)?;
    let feature = FeatureParams::new(workspace, document);
    let params = HighlightParams { feature, offset };
    let results = highlights::find_all(params);
    let results = results.into_iter().filter_map(|result| {
        let range = document.line_index.line_col_lsp_range(result.range)?;
        let kind = Some(match result.kind {
            HighlightKind::Write => lsp_types::DocumentHighlightKind::WRITE,
            HighlightKind::Read => lsp_types::DocumentHighlightKind::READ,
        });

        Some(lsp_types::DocumentHighlight { range, kind })
    });

    Some(results.collect())
}