lang_jsonld/ecs/
highlight.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
use bevy_ecs::prelude::*;
use lsp_core::{components::Element, feature::semantic::TokenTypesComponent, prelude::*};
use lsp_types::SemanticTokenType;
// use semantic::TokenTypesComponent;
use sophia_api::term::{Term, TermKind};

use crate::{lang::parser, JsonLd};

fn walk_json(json: &Spanned<parser::Json>, ttc: &mut Vec<Spanned<SemanticTokenType>>) {
    let check_token =
        |token: &Spanned<Token>, ttc: &mut Vec<Spanned<SemanticTokenType>>| match token.value() {
            Token::Str(x, _) if x.starts_with("@") => {
                ttc.push(Spanned(SemanticTokenType::KEYWORD, token.span().clone()));
            }
            _ => {}
        };

    match json.value() {
        parser::Json::Token(token) => check_token(&Spanned(token.clone(), json.1.clone()), ttc),
        parser::Json::Array(vec) => {
            vec.iter().for_each(|json| walk_json(json, ttc));
        }
        parser::Json::Object(vec) => {
            for o in vec.iter() {
                let v = match o.value() {
                    parser::ObjectMember::Full(k, v) => {
                        check_token(k, ttc);
                        v
                    }
                    parser::ObjectMember::Partial(k, _, mv) => {
                        check_token(k, ttc);
                        if let Some(v) = mv {
                            v
                        } else {
                            continue;
                        }
                    }
                };
                walk_json(&v, ttc);
            }
        }
        _ => {}
    }
}

pub fn highlight_named_nodes(
    mut query: Query<
        (&Triples, &mut TokenTypesComponent),
        (With<HighlightRequest>, With<Element<JsonLd>>),
    >,
) {
    for (triples, mut ttc) in &mut query {
        for MyQuad {
            subject,
            predicate,
            object,
            ..
        } in triples.iter()
        {
            for t in [subject, predicate, object] {
                if t.kind() == TermKind::Iri {
                    let s = &t.span;
                    ttc.push(Spanned(SemanticTokenType::PROPERTY, s.start - 1..s.end + 1));
                }
            }
        }
    }
}

pub fn keyword_highlight(
    mut query: Query<(&Element<JsonLd>, &mut TokenTypesComponent), With<HighlightRequest>>,
) {
    for (json, mut ttc) in &mut query {
        walk_json(&json.0, &mut ttc.0);
    }
}