swls-lang-jsonld 0.1.5

JSON-LD language support for the Semantic Web Language Server
Documentation
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#![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"
)]
pub mod cjs;

use std::{borrow::Cow, ops::Range};

use bevy_ecs::{
    component::Component,
    observer::On,
    query::With,
    resource::Resource,
    schedule::IntoScheduleConfigs,
    system::{Commands, Query, Res, RunSystemOnce},
    world::{CommandQueue, World},
};
use components_rs::{
    components::registry::{resolve_iri_to_url, ComponentRegistry},
    module_state::ModuleState,
};
use oxigraph::model::{GraphName, Literal, NamedNode, Quad};
use swls_core::{
    lang::{Lang, LangHelper},
    lsp_types::{SemanticTokenType, Url},
    prelude::{goto_definition::GotoDefinitionRequest, *},
    util::resolve_iri,
    Started,
};
use swls_lang_rdf_base::register_rdf_lang;
use swls_lang_turtle::lang::parser::TurtleParseError;

pub mod ecs;
use crate::{
    ecs::{
        derive_jsonld_triples, format_jsonld_system, setup_completion, setup_parsing, ContextCache,
        JsonLdActiveContext,
    },
    fs::build_registry,
};

#[derive(Component, Default)]
pub struct JsonLdLang;

#[derive(Debug, Default)]
pub struct JsonLdHelper;

impl LangHelper for JsonLdHelper {
    fn keyword(&self) -> &[&'static str] {
        &[
            "@context",
            "@id",
            "@type",
            "@graph",
            "@base",
            "@vocab",
            "@language",
            "@value",
            "@list",
            "@set",
            "@reverse",
            "@index",
            "@container",
        ]
    }

    fn default_position(&self) -> TripleTarget {
        TripleTarget::Predicate
    }

    fn unquote<'a>(&self, text: &'a str) -> &'a str {
        let s = text.strip_prefix('"').unwrap_or(text);
        s.strip_suffix('"').unwrap_or(s)
    }
    fn quote(&self, inp: &str) -> String {
        format!("\"{}\"", inp)
    }
    fn handles_prefix_completion(&self) -> bool {
        true
    }

    fn inlay_types_hint(
        &self,
        subject: &Range<usize>,
        rope: &ropey::Rope,
        last_type: Option<&Range<usize>>,
        types: Vec<Cow<'_, str>>,
    ) -> Option<swls_core::lsp_types::InlayHint> {
        let (label, position) = if let Some(lt) = last_type {
            if let Some(pos) = offset_to_position(lt.end, &rope) {
                let label = format!(", {}", types.join(", "));
                (label, pos)
            } else {
                return None;
            }
        } else {
            let offset = if rope.get_char(subject.start) == Some('[') {
                subject.start + 1
            } else {
                subject.end
            };

            if let Some(pos) = offset_to_position(offset + 1, &rope) {
                let label = if types.len() == 1 {
                    format!(r#" "@type": "{}";"#, types[0])
                } else {
                    format!(
                        r#" "@type": [ {} ],"#,
                        types
                            .into_iter()
                            .map(|x| format!("\"{}\"", x))
                            .collect::<Vec<_>>()
                            .join(", ")
                    )
                };
                (label, pos)
            } else {
                return None;
            }
        };

        return Some(swls_core::lsp_types::InlayHint {
            position,
            label: swls_core::lsp_types::InlayHintLabel::String(label),
            kind: None,
            text_edits: None,
            tooltip: None,
            padding_left: None,
            padding_right: None,
            data: None,
        });
    }
}

pub fn setup_world<C: Client + ClientSync + Resource + Clone>(world: &mut World) {
    register_rdf_lang::<JsonLdLang, JsonLdHelper>(world, &["jsonld"], &[".jsonld"]);

    // For .json files and the "json" language ID, only activate JSON-LD when the
    // source actually contains "@context" — plain JSON files should be left alone.
    world.add_observer(
        |trigger: On<CreateEvent>, mut commands: Commands, query: Query<&Source>| {
            let e = trigger.event();
            let is_json = trigger
                .language_id
                .as_ref()
                .map(|l| l == "json")
                .unwrap_or_default()
                || e.url.as_str().ends_with(".json");
            if !is_json {
                return;
            }
            let entity = e.entity;
            if let Ok(source) = query.get(entity) {
                if source.0.contains("\"@context\"") {
                    commands
                        .entity(entity)
                        .insert(JsonLdLang::default())
                        .insert(DynLang(Box::new(JsonLdHelper::default())));
                }
            }
        },
    );
    world.insert_resource(ContextCache::default());
    world.insert_resource(Registry::empty());
    setup_parsing::<C>(world);
    setup_completion(world);

    world.schedule_scope(FormatLabel, |_, schedule| {
        schedule.add_systems(format_jsonld_system);
    });

    world.schedule_scope(Started, |_, schedule| {
        schedule.add_systems((start_jsonld::<C>,));
    });

    world.schedule_scope(GotoDefinitionLabel, |_, schedule| {
        schedule.add_systems(goto_cjs.after(get_current_triple));
    });
}

/// Convert a byte offset to an LSP `Position` (line + character).
fn byte_offset_to_position(source: &str, offset: usize) -> swls_core::lsp_types::Position {
    let offset = offset.min(source.len());
    let before = &source[..offset];
    let line = before.matches('\n').count() as u32;
    let col = before.rfind('\n').map(|p| offset - p - 1).unwrap_or(offset) as u32;
    swls_core::lsp_types::Position::new(line, col)
}

/// Convert a byte-range span to an LSP `Range` using the file's source text.
fn span_to_lsp_range(source: &str, span: &std::ops::Range<usize>) -> swls_core::lsp_types::Range {
    swls_core::lsp_types::Range::new(
        byte_offset_to_position(source, span.start),
        byte_offset_to_position(source, span.end),
    )
}

/// Expand a JSON-LD compact IRI or bare term name using the document's active context.
///
/// Handles chains like `css:dist/...` → `npmd:@solid/...dist/...` → `https://...dist/...`
/// and bare term names like `BearerWebIdExtractor` → `css:dist/...BearerWebIdExtractor`.
fn expand_iri_with_context(
    active: &rdf_parsers::jsonld::convert::ActiveContext,
    value: &str,
) -> String {
    expand_iri_inner(active, value, 0)
}

fn expand_iri_inner(
    active: &rdf_parsers::jsonld::convert::ActiveContext,
    value: &str,
    depth: usize,
) -> String {
    if depth > 10 || value.is_empty() || value.starts_with('@') {
        return value.to_string();
    }
    // Already absolute — well-known schemes only, so we don't mistake `css:` for absolute.
    if value.starts_with("https://")
        || value.starts_with("http://")
        || value.starts_with("file://")
        || value.starts_with("urn:")
    {
        return value.to_string();
    }
    // Bare term lookup (e.g. "BearerWebIdExtractor")
    if let Some(def) = active.terms.get(value) {
        if let Some(iri) = &def.iri {
            if iri != value {
                return expand_iri_inner(active, iri, depth + 1);
            }
        }
    }
    // Compact IRI like "prefix:suffix"
    if let Some(colon_pos) = value.find(':') {
        if colon_pos > 0 {
            let prefix = &value[..colon_pos];
            let suffix = &value[colon_pos + 1..];
            if let Some(def) = active.terms.get(prefix) {
                if let Some(iri) = &def.iri {
                    let expanded_prefix = expand_iri_inner(active, iri, depth + 1);
                    return format!("{}{}", expanded_prefix, suffix);
                }
            }
        }
    }
    value.to_string()
}

#[tracing::instrument(skip(query, res))]
fn goto_cjs(
    mut query: Query<
        (
            &TokenComponent,
            Option<&TripleComponent>,
            &Label,
            &mut GotoDefinitionRequest,
            Option<&JsonLdActiveContext>,
        ),
        With<JsonLdLang>,
    >,
    res: Res<Registry>,
) {
    use swls_core::lsp_types::{Location, Range};

    for (token, triple, label, mut req, active_ctx) in &mut query {
        // Only use the expanded IRI from the TripleComponent if the cursor token
        // actually overlaps the matched term's span.  get_current_triple is lenient
        // and may fall back to a nearby triple (e.g. the first triple in the
        // document) when the cursor is on @context or other non-triple content.
        let triple_term_str = triple.and_then(|tc| {
            let term_span = match tc.target {
                TripleTarget::Subject => &tc.triple.subject.span,
                TripleTarget::Predicate => &tc.triple.predicate.span,
                TripleTarget::Object => &tc.triple.object.span,
                TripleTarget::Graph => return None,
            };
            let cursor = token.source_span.start;
            if term_span.start <= cursor && cursor <= term_span.end {
                tc.term().map(|t| t.as_str())
            } else {
                None
            }
        });
        let raw_token = token.text.as_str().trim_matches('"');
        // When no triple term is available (e.g. cursor in @context), expand compact
        // IRIs like `css:dist/...` using the document's active context so that
        // resolve_iri_to_url can match them against import_paths.
        let context_expanded = if triple_term_str.is_none() {
            active_ctx.map(|ctx| expand_iri_with_context(&ctx.0, raw_token))
        } else {
            None
        };
        let st: &str = triple_term_str
            .as_deref()
            .or(context_expanded.as_deref())
            .unwrap_or(raw_token);

        tracing::debug!("Goto definition {:?} {}", triple_term_str, st,);

        // Components: navigate to the component's own source file at the exact @id span.
        let found_target = if let Some(component) = res.0.components.get(st) {
            Some((component.source_file.as_str(), component.iri_span.clone()))
        } else if let Some(module) = res.0.modules.get(st) {
            Some((module.source_file.as_str(), module.iri_span.clone()))
        } else if let Some((file, span)) = res.0.parameters.get(st) {
            Some((file.as_str(), span.clone()))
        } else {
            None
        };

        tracing::debug!(
            "CJS from {:?} {:?}",
            found_target,
            resolve_iri_to_url(st, &res.1.import_paths),
        );
        if let Some((file, span)) = found_target {
            if let Ok(uri) = swls_core::lsp_types::Url::parse(file) {
                let range = res
                    .0
                    .file_sources
                    .get(file)
                    .map(|src| span_to_lsp_range(src, &span))
                    .unwrap_or_default();
                req.0.push(Location { uri, range });
                continue;
            }
        }

        let iri_no_fragment = st.split('#').next().unwrap_or(st);
        let resolved = resolve_iri_to_url(iri_no_fragment, &res.1.import_paths)
            .or_else(|| res.1.context_urls.get(iri_no_fragment).cloned());
        if let Some(t) = resolved {
            tracing::debug!("target {}", t.as_str());
            req.0.push(Location {
                uri: t,
                range: Range::default(),
            });
            continue;
        }

        if triple_term_str.is_none() {
            // Import IRIs: strip any fragment, then resolve to a local file path.
            let target = resolve_iri(&label.as_str(), st);
            if let Ok(uri) = swls_core::lsp_types::Url::parse(&target) {
                req.0.push(Location {
                    uri,
                    range: Range::default(),
                });
                continue;
            }
        }

        tracing::debug!("goto_cjs: no definition found for '{}'", st);
    }
}

mod fs {
    use components_rs::{
        error::{ComponentsJsError, Result},
        fs::FsDirEntry,
    };
    use swls_core::{lsp_types::Url, prelude::Fs};

    use crate::Registry;

    pub struct LocalFs(Fs);

    #[async_trait::async_trait]
    impl components_rs::fs::Fs for LocalFs {
        /// Read the entire contents of a file as a UTF-8 string.
        async fn read_to_string(&self, url: &Url) -> Result<String> {
            self.0
                 .0
                .read_file(&url)
                .await
                .ok_or(ComponentsJsError::General(format!(
                    "Failed to read file {}",
                    url.as_str()
                )))
        }

        /// List the immediate children of a directory.
        async fn read_dir(&self, path: &Url) -> Result<Vec<components_rs::fs::FsDirEntry>> {
            let entries = self
                .0
                 .0
                .read_dir(path)
                .await
                .ok_or(ComponentsJsError::General(format!(
                    "Failed to read dir {:?}",
                    path.as_str()
                )))?;
            Ok(entries
                .into_iter()
                .map(|entry| components_rs::fs::FsDirEntry {
                    name: entry.name,
                    path: entry.path,
                    is_dir: entry.is_dir,
                })
                .collect())
        }

        /// Check whether `path` is a file.
        async fn is_file(&self, path: &Url) -> bool {
            self.0 .0.is_file(path).await
        }

        /// Check whether `path` is a directory.
        async fn is_dir(&self, path: &Url) -> bool {
            self.0 .0.is_dir(path).await
        }

        async fn glob(&self, base: &Url, pattern: &str) -> Result<Vec<FsDirEntry>> {
            let entries = self
                .0
                 .0
                .glob(base, pattern)
                .await
                .ok_or(ComponentsJsError::General(format!(
                    "Failed to read dir {:?} {}",
                    base.as_str(),
                    pattern
                )))?;
            Ok(entries
                .into_iter()
                .map(|entry| components_rs::fs::FsDirEntry {
                    name: entry.name,
                    path: entry.path,
                    is_dir: entry.is_dir,
                })
                .collect())
        }
    }

    pub async fn build_registry(fs: &Fs, path: &Url) -> Result<Registry> {
        use components_rs::components::registry::ComponentRegistry;
        use components_rs::module_state::ModuleState;

        let fs = LocalFs(fs.clone());
        let state = ModuleState::build(&fs, path).await?;

        let mut registry = ComponentRegistry::new();
        registry.register_available_modules(&fs, &state).await?;
        registry.finalize();

        Ok(Registry(registry, state))
    }
}

fn build_cjs_quads(registry: &ComponentRegistry) -> Vec<Quad> {
    let rdf_type = NamedNode::new_unchecked("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
    let rdfs_class = NamedNode::new_unchecked("http://www.w3.org/2000/01/rdf-schema#Class");
    let rdfs_subclass_of =
        NamedNode::new_unchecked("http://www.w3.org/2000/01/rdf-schema#subClassOf");
    let rdfs_comment = NamedNode::new_unchecked("http://www.w3.org/2000/01/rdf-schema#comment");
    let rdf_property =
        NamedNode::new_unchecked("http://www.w3.org/1999/02/22-rdf-syntax-ns#Property");
    let rdfs_domain = NamedNode::new_unchecked("http://www.w3.org/2000/01/rdf-schema#domain");
    let rdfs_range = NamedNode::new_unchecked("http://www.w3.org/2000/01/rdf-schema#range");

    let graph = GraphName::DefaultGraph;
    let mut quads = Vec::new();

    for comp in registry.components.values() {
        let iri = NamedNode::new_unchecked(&comp.iri);

        quads.push(Quad::new(
            iri.clone(),
            rdf_type.clone(),
            rdfs_class.clone(),
            graph.clone(),
        ));

        if let Some(comment) = &comp.comment {
            quads.push(Quad::new(
                iri.clone(),
                rdfs_comment.clone(),
                Literal::new_simple_literal(comment.as_str()),
                graph.clone(),
            ));
        }

        for parent in &comp.extends {
            let parent_node = NamedNode::new_unchecked(parent);
            quads.push(Quad::new(
                iri.clone(),
                rdfs_subclass_of.clone(),
                parent_node,
                graph.clone(),
            ));
        }

        for param in &comp.parameters {
            let param_iri = NamedNode::new_unchecked(&param.iri);

            quads.push(Quad::new(
                param_iri.clone(),
                rdf_type.clone(),
                rdf_property.clone(),
                graph.clone(),
            ));
            quads.push(Quad::new(
                param_iri.clone(),
                rdfs_domain.clone(),
                iri.clone(),
                graph.clone(),
            ));

            if let Some(range) = &param.range {
                let range_node = NamedNode::new_unchecked(range);
                quads.push(Quad::new(
                    param_iri.clone(),
                    rdfs_range.clone(),
                    range_node,
                    graph.clone(),
                ));
            }

            if let Some(comment) = &param.comment {
                quads.push(Quad::new(
                    param_iri.clone(),
                    rdfs_comment.clone(),
                    Literal::new_simple_literal(comment.as_str()),
                    graph.clone(),
                ));
            }
        }
    }

    quads
}

#[derive(Resource)]
pub struct Registry(pub ComponentRegistry, pub ModuleState);
impl Registry {
    pub fn empty() -> Self {
        Self(ComponentRegistry::new(), ModuleState::empty())
    }
}

#[tracing::instrument(skip(fs, client, config, commands))]
fn start_jsonld<C: Client + Resource + Clone>(
    fs: Res<Fs>,
    client: Res<C>,
    config: Res<ServerConfig>,
    commands: Res<CommandSender>,
) {
    if !config.config.jsonld.unwrap_or(true) {
        return;
    }
    let fs = fs.clone();
    tracing::debug!("loading CJS registry, config: {:?}", config);
    if let Some(ws) = config.workspaces.first().and_then(|x| {
        if x.uri.as_str().ends_with('/') {
            Some(x.uri.clone())
        } else {
            Url::parse(&format!("{}/", x.uri.as_str())).ok()
        }
    }) {
        tracing::debug!("CJS workspace root: {:?}", ws.as_str());
        let commands = commands.clone();
        let thing = async move {
            tracing::debug!("Starting CJS registry build for {:?}", ws.as_str());
            if let Ok(reg) = build_registry(&fs, &ws).await {
                let mut command_queue = CommandQueue::default();
                command_queue.push(move |world: &mut World| {
                    let quads = build_cjs_quads(&reg.0);
                    world.insert_resource(reg);

                    let store_clone = world.get_resource::<swls_core::store::Store>();
                    //
                    if let Some(store) = store_clone {
                        tracing::debug!("Derive store found adding {} triples", quads.len());
                        let mut loader = store.0.bulk_loader();
                        let _ = loader.load_quads(quads.into_iter());
                        let _ = loader.commit();
                    }

                    let _ = world.run_system_once(derive_jsonld_triples::<C>);
                });
                let _ = commands.unbounded_send(command_queue);
            }
            ()
        };
        client.spawn(thing);
    } else {
        tracing::warn!("No workspace root found, skipping CJS registry build");
    }
}

impl Lang for JsonLdLang {
    type Element = rdf_parsers::model::Turtle;
    type ElementError = TurtleParseError;

    const LANG: &'static str = "jsonld";
    const TRIGGERS: &'static [&'static str] = &["\"@", "\""];
    const CODE_ACTION: bool = false;
    const HOVER: bool = true;
    const PATTERN: Option<&'static str> = None;

    const LEGEND_TYPES: &'static [SemanticTokenType] = &[
        semantic_token::BOOLEAN,
        SemanticTokenType::COMMENT,
        SemanticTokenType::ENUM_MEMBER,
        SemanticTokenType::KEYWORD,
        SemanticTokenType::NAMESPACE,
        SemanticTokenType::NUMBER,
        SemanticTokenType::PROPERTY,
        SemanticTokenType::STRING,
    ];

    fn semantic_token_type(kind: rowan::SyntaxKind) -> Option<SemanticTokenType> {
        use rdf_parsers::jsonld::parser::SyntaxKind as SK;
        let k = kind.0;
        if k == SK::Comment as u16 {
            Some(SemanticTokenType::COMMENT)
        } else if k == SK::StringToken as u16 {
            Some(SemanticTokenType::STRING)
        } else if k == SK::JsonNumber as u16 {
            Some(SemanticTokenType::NUMBER)
        } else if k == SK::TrueLit as u16 || k == SK::FalseLit as u16 || k == SK::NullLit as u16 {
            Some(semantic_token::BOOLEAN)
        } else {
            None
        }
    }

    fn semantic_token_spans(
        kind: rowan::SyntaxKind,
        span: std::ops::Range<usize>,
        text: &str,
    ) -> Vec<(SemanticTokenType, std::ops::Range<usize>)> {
        if text.get(span.start + 1..span.start + 2) == Some("@") {
            return vec![(SemanticTokenType::KEYWORD, span)];
        }
        if text.get(span.end..span.end + 1) == Some(":") {
            return vec![(SemanticTokenType::NAMESPACE, span)];
        }
        Self::semantic_token_type(kind)
            .map(|t| vec![(t, span)])
            .unwrap_or_default()
    }
}