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
    Given = 8,
56
} // to be extended
57

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

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

            
75
468
    diagnostics.extend(detect_errors(source, cst));
76

            
77
468
    diagnostics
78
468
}
79

            
80
// get document symbols for semantic highlighting