lang_turtle/
lib.rs

1#![doc(
2    html_logo_url = "https://ajuvercr.github.io/semantic-web-lsp/assets/icons/favicon.png",
3    html_favicon_url = "https://ajuvercr.github.io/semantic-web-lsp/assets/icons/favicon.ico"
4)]
5use bevy_ecs::{component::Component, observer::Trigger, system::Commands, world::World};
6use chumsky::prelude::Simple;
7use lsp_core::{
8    feature::diagnostics::publish_diagnostics,
9    lang::{Lang, LangHelper},
10    prelude::*,
11    CreateEvent,
12};
13use lsp_types::SemanticTokenType;
14
15pub mod ecs;
16pub mod lang;
17
18use crate::ecs::{setup_completion, setup_formatting, setup_parsing};
19
20#[derive(Component)]
21pub struct TurtleLang;
22
23#[derive(Debug)]
24pub struct TurtleHelper;
25impl LangHelper for TurtleHelper {
26    fn keyword(&self) -> &[&'static str] {
27        &["@prefix", "@base", "a"]
28    }
29}
30
31pub fn setup_world(world: &mut World) {
32    let mut semantic_token_dict = world.resource_mut::<SemanticTokensDict>();
33    TurtleLang::LEGEND_TYPES.iter().for_each(|lt| {
34        if !semantic_token_dict.contains_key(lt) {
35            let l = semantic_token_dict.0.len();
36            semantic_token_dict.insert(lt.clone(), l);
37        }
38    });
39
40    world.observe(|trigger: Trigger<CreateEvent>, mut commands: Commands| {
41        match &trigger.event().language_id {
42            Some(x) if x == "turtle" => {
43                commands
44                    .entity(trigger.entity())
45                    .insert((TurtleLang, DynLang(Box::new(TurtleHelper))));
46                return;
47            }
48            _ => {}
49        }
50        // pass
51        if trigger.event().url.as_str().ends_with(".ttl") {
52            commands
53                .entity(trigger.entity())
54                .insert((TurtleLang, DynLang(Box::new(TurtleHelper))));
55            return;
56        }
57    });
58
59    world.schedule_scope(lsp_core::feature::DiagnosticsLabel, |_, schedule| {
60        schedule.add_systems(publish_diagnostics::<TurtleLang>);
61    });
62
63    setup_parsing(world);
64    setup_completion(world);
65    setup_formatting(world);
66}
67
68impl Lang for TurtleLang {
69    type Token = Token;
70
71    type TokenError = Simple<char>;
72
73    type Element = crate::lang::model::Turtle;
74
75    type ElementError = Simple<Token>;
76
77    const LANG: &'static str = "turtle";
78
79    const TRIGGERS: &'static [&'static str] = &[":"];
80    const CODE_ACTION: bool = true;
81    const HOVER: bool = true;
82
83    const LEGEND_TYPES: &'static [lsp_types::SemanticTokenType] = &[
84        semantic_token::BOOLEAN,
85        semantic_token::LANG_TAG,
86        SemanticTokenType::COMMENT,
87        SemanticTokenType::ENUM_MEMBER,
88        SemanticTokenType::ENUM,
89        SemanticTokenType::KEYWORD,
90        SemanticTokenType::NAMESPACE,
91        SemanticTokenType::NUMBER,
92        SemanticTokenType::PROPERTY,
93        SemanticTokenType::STRING,
94        SemanticTokenType::VARIABLE,
95    ];
96
97    const PATTERN: Option<&'static str> = None;
98}