lsp_core/
lang.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
use std::{hash::Hash, ops::Range};

use lsp_types::SemanticTokenType;
use ropey::Rope;

use crate::prelude::*;

pub fn head() -> lsp_types::Range {
    let start = lsp_types::Position {
        line: 0,
        character: 0,
    };
    lsp_types::Range {
        end: start.clone(),
        start,
    }
}

pub trait TokenTrait: Sized {
    fn token(&self) -> Option<SemanticTokenType>;

    fn span_tokens(Spanned(this, span): &Spanned<Self>) -> Vec<(SemanticTokenType, Range<usize>)> {
        if let Some(x) = this.token() {
            vec![(x, span.clone())]
        } else {
            Vec::new()
        }
    }
}

pub trait Lang: 'static {
    /// Type of tokens after tokenization
    type Token: PartialEq + Hash + Clone + Send + Sync + TokenTrait;
    type TokenError: Into<SimpleDiagnostic> + Send + Sync + std::fmt::Debug;

    /// Type of Element inside a ParentingSystem
    type Element: Send + Sync;
    type ElementError: Into<SimpleDiagnostic> + Send + Sync + std::fmt::Debug;

    const CODE_ACTION: bool;
    const HOVER: bool;
    const LANG: &'static str;
    const TRIGGERS: &'static [&'static str];
    const LEGEND_TYPES: &'static [SemanticTokenType];
    const PATTERN: Option<&'static str>;
}

pub trait LangHelper: std::fmt::Debug {
    fn _get_relevant_text(&self, token: &Spanned<Token>, rope: &Rope) -> String {
        rope.slice(token.span().clone()).to_string()
    }
    fn get_relevant_text(
        &self,
        token: &Spanned<Token>,
        rope: &Rope,
    ) -> (String, std::ops::Range<usize>) {
        (self._get_relevant_text(token, rope), token.span().clone())
    }
    fn keyword(&self) -> &[&'static str];
}