1
use serde::{Deserialize, Serialize};
2

            
3
use crate::diagnostics::error_detection::collect_errors::detect_errors;
4
use tree_sitter::Tree;
5
// structs for lsp stuff
6

            
7
// position / range
8
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
9
#[serde(rename_all = "camelCase")]
10
pub struct Position {
11
    pub line: u32,
12
    pub character: u32,
13
}
14

            
15
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
16
#[serde(rename_all = "camelCase")]
17
pub struct Range {
18
    pub start: Position,
19
    pub end: Position,
20
}
21

            
22
// the actual values can be chnaged later, if needed
23
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
24
#[serde(rename_all = "camelCase")]
25
pub enum Severity {
26
    Error = 1,
27
    Warn = 2,
28
    Info = 3,
29
    Hint = 4,
30
}
31

            
32
// the actual diagnostic struct
33
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
34
#[serde(rename_all = "camelCase")]
35
pub struct Diagnostic {
36
    pub range: Range,
37
    pub severity: Severity,
38
    pub message: String,
39
    pub source: &'static str,
40
}
41

            
42
// document symbol struct is used to denote a single token / node
43
// this will be used for syntax highlighting and hovering
44
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
45
#[serde(rename_all = "camelCase")]
46
pub enum SymbolKind {
47
    Integer = 0,
48
    Decimal = 1,
49
    Function = 2,
50
    Letting = 3,
51
    Find = 4,
52
    Variable = 5,
53
    Constant = 6,
54
    Domain = 7,
55
    FindVar = 8,
56
    LettingVar = 9,
57
    Given = 10,
58
    GivenVar = 11,
59
} // to be extended
60

            
61
// each type of token / symbol in the essence grammar will be
62
// assigned an integer, which would be mapped to a colour
63
#[derive(Debug, Clone, Serialize, Deserialize)]
64
#[serde(rename_all = "camelCase")]
65
pub struct DocumentSymbol {
66
    pub name: String,
67
    pub detail: Option<String>,
68
    pub kind: SymbolKind,
69
    pub range: Range,
70
    #[serde(skip_serializing_if = "Option::is_none")]
71
    pub children: Option<Vec<DocumentSymbol>>,
72
}
73

            
74
// getting the actual diagnostic
75
468
pub fn get_diagnostics(source: &str, cst: &Tree) -> Vec<Diagnostic> {
76
468
    let mut diagnostics = Vec::new();
77

            
78
468
    diagnostics.extend(detect_errors(source, cst));
79

            
80
468
    diagnostics
81
468
}
82

            
83
// get document symbols for semantic highlighting