1
use crate::server::Backend;
2
use conjure_cp_essence_parser::diagnostics::semantic_tokens::encode_semantic_tokens;
3
use tower_lsp::{jsonrpc::Error, lsp_types::*};
4

            
5
impl Backend {
6
    pub async fn handle_semantic_highlighting(
7
        &self,
8
        params: SemanticTokensParams,
9
    ) -> Result<Option<SemanticTokensResult>, Error> {
10
        self.client
11
            .log_message(MessageType::INFO, "semantic highlighting")
12
            .await;
13
        let uri = params.text_document.uri.clone();
14

            
15
        let lsp_cache = &self.lsp_cache;
16

            
17
        let cache_conts = match lsp_cache.get(&uri).await {
18
            Some(conts) => conts,
19
            None => {
20
                self.client
21
                    .log_message(MessageType::WARNING, "Document not found in cache")
22
                    .await;
23
                return Ok(None);
24
            }
25
        };
26

            
27
        let source_map = match &cache_conts.sourcemap {
28
            Some(map) => map,
29
            None => {
30
                self.client
31
                    .log_message(MessageType::WARNING, "No source map found in cache")
32
                    .await;
33
                return Ok(None);
34
            }
35
        };
36

            
37
        let data = encode_semantic_tokens(source_map, &cache_conts.contents)
38
            .chunks(5)
39
            .map(|c| SemanticToken {
40
                delta_line: c[0],
41
                delta_start: c[1],
42
                length: c[2],
43
                token_type: c[3],
44
                token_modifiers_bitset: c[4],
45
            })
46
            .collect();
47

            
48
        Ok(Some(SemanticTokensResult::Tokens(SemanticTokens {
49
            result_id: None,
50
            data,
51
        })))
52
    }
53
}