lang_jsonld/
lib.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#![doc(
    html_logo_url = "https://ajuvercr.github.io/semantic-web-lsp/assets/icons/favicon.png",
    html_favicon_url = "https://ajuvercr.github.io/semantic-web-lsp/assets/icons/favicon.ico"
)]
use bevy_ecs::prelude::*;
use chumsky::prelude::Simple;
use lsp_core::{
    components::DynLang,
    lang::{Lang, LangHelper},
    prelude::*,
    CreateEvent,
};
use lsp_types::SemanticTokenType;
use ropey::Rope;

pub mod ecs;
pub mod lang;
use crate::{
    ecs::{highlight_named_nodes, keyword_highlight, setup_parse},
    lang::parser::Json,
};

pub fn setup_world(world: &mut World) {
    let mut semantic_token_dict = world.resource_mut::<SemanticTokensDict>();
    JsonLd::LEGEND_TYPES.iter().for_each(|lt| {
        if !semantic_token_dict.contains_key(lt) {
            let l = semantic_token_dict.0.len();
            semantic_token_dict.insert(lt.clone(), l);
        }
    });
    world.observe(|trigger: Trigger<CreateEvent>, mut commands: Commands| {
        println!("Got create event");
        match &trigger.event().language_id {
            Some(x) if x == "jsonld" => {
                println!(" --> its jsonld");
                commands
                    .entity(trigger.entity())
                    .insert(JsonLd)
                    .insert(DynLang(Box::new(JsonLdHelper)));
                return;
            }
            _ => {}
        }
        // pass
        if trigger.event().url.as_str().ends_with(".jsonld") {
            println!(" --> its jsonld");
            commands
                .entity(trigger.entity())
                .insert(JsonLd)
                .insert(DynLang(Box::new(JsonLdHelper)));
            return;
        }
    });

    world.schedule_scope(SemanticLabel, |_, schedule| {
        use semantic::*;
        schedule.add_systems((
            highlight_named_nodes
                .before(keyword_highlight)
                .after(basic_semantic_tokens),
            keyword_highlight
                .before(semantic_tokens_system)
                .after(basic_semantic_tokens),
        ));
    });

    world.schedule_scope(DiagnosticsLabel, |_, schedule| {
        use diagnostics::*;
        schedule.add_systems(publish_diagnostics::<JsonLd>);
    });

    setup_parse(world);
}

#[derive(Debug, Component)]
pub struct JsonLd;

impl Lang for JsonLd {
    type Token = Token;

    type TokenError = Simple<char>;

    type Element = Json;

    type ElementError = Simple<Token>;

    const PATTERN: Option<&'static str> = None;

    const LANG: &'static str = "jsonld";
    const CODE_ACTION: bool = false;
    const HOVER: bool = true;

    const TRIGGERS: &'static [&'static str] = &["@", "\""];
    const LEGEND_TYPES: &'static [SemanticTokenType] = &[
        SemanticTokenType::VARIABLE,
        SemanticTokenType::STRING,
        SemanticTokenType::NUMBER,
        SemanticTokenType::KEYWORD,
        SemanticTokenType::PROPERTY,
        SemanticTokenType::ENUM_MEMBER,
    ];
}

#[derive(Debug)]
pub struct JsonLdHelper;
impl LangHelper for JsonLdHelper {
    fn get_relevant_text(
        &self,
        token: &Spanned<Token>,
        rope: &Rope,
    ) -> (String, std::ops::Range<usize>) {
        let r = token.span();
        match token.value() {
            Token::Str(st, _) => (st.clone(), r.start + 1..r.end - 1),
            _ => (self._get_relevant_text(token, rope), r.clone()),
        }
    }

    fn keyword(&self) -> &[&'static str] {
        &[]
    }
}