lang_sparql/
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
#[macro_use]
extern crate tracing;

use bevy_ecs::prelude::*;
use chumsky::error::Simple;
use lsp_core::prelude::*;
use lsp_types::SemanticTokenType;

pub mod ecs;
use crate::ecs::{setup_completion, setup_parse};
pub mod lang;
// pub mod model;
// use crate::model::Query;
// pub mod parsing;
// pub mod tokenizer;

pub fn setup_world(world: &mut World) {
    let mut semantic_token_dict = world.resource_mut::<SemanticTokensDict>();
    [SemanticTokenType::VARIABLE].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 == "sparql" => {
                println!(" --> its sparql");
                commands
                    .entity(trigger.entity())
                    .insert(Sparql)
                    .insert(DynLang(Box::new(SparqlHelper)));
                return;
            }
            _ => {}
        }

        if trigger.event().url.as_str().ends_with(".sq") {
            println!(" --> its sparql");
            commands
                .entity(trigger.entity())
                .insert(Sparql)
                .insert(DynLang(Box::new(SparqlHelper)));
            return;
        }
    });

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

    setup_parse(world);

    setup_completion(world);
}

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

impl Lang for Sparql {
    type Token = Token;

    type TokenError = Simple<char>;

    type Element = crate::lang::model::Query;

    type ElementError = (usize, Simple<Token>);

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

    const LANG: &'static str = "sparql";
    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,
    ];
}

lazy_static::lazy_static! {
    static ref KWDS: Vec<&'static str> = {
        let mut m = Vec::new();

        // lsp_core::token::SparqlCall::ITEMS.iter().for_each(|x| m.push(x.complete()));
        SparqlKeyword::ITEMS.iter().for_each(|x| m.push(x.complete()));
        SparqlAggregate::ITEMS.iter().for_each(|x| m.push(x.complete()));

        m
    };
}

#[derive(Debug)]
pub struct SparqlHelper;

impl LangHelper for SparqlHelper {
    fn keyword(&self) -> &[&'static str] {
        &KWDS
    }
}