lsp_core/systems/
links.rs

1use bevy_ecs::prelude::*;
2use lsp_types::Url;
3use tracing::instrument;
4
5use crate::{prelude::*, util::ns::owl};
6
7pub fn derive_prefix_links(
8    mut query: Query<(Entity, &Prefixes, Option<&mut DocumentLinks>), Changed<Prefixes>>,
9    mut commands: Commands,
10    // helper: Res<LovHelper>,
11    fs: Res<Fs>,
12) {
13    const SOURCE: &'static str = "prefix import";
14    for (e, prefixes, mut links) in &mut query {
15        let mut new_links = Vec::new();
16        for u in prefixes.0.iter() {
17            let url: Url =
18                fs.0.lov_url(u.url.as_str(), &u.prefix)
19                    .unwrap_or(u.url.clone());
20            tracing::debug!(
21                "Mapping prefix {}: {} to {}",
22                u.prefix,
23                u.url.as_str(),
24                url.as_str()
25            );
26
27            new_links.push((url.clone(), SOURCE));
28        }
29        if let Some(links) = links.as_mut() {
30            links.retain(|e| e.1 != SOURCE);
31        }
32        // let new_links: Vec<_> = turtle.0.iter().map(|u| (u.url.clone(), SOURCE)).collect();
33        match (new_links.is_empty(), links) {
34            (false, None) => {
35                commands.entity(e).insert(DocumentLinks(new_links));
36            }
37            (false, Some(mut links)) => {
38                links.extend(new_links);
39            }
40            _ => {}
41        }
42    }
43}
44
45#[instrument(skip(query, commands))]
46pub fn derive_owl_imports_links(
47    mut query: Query<(Entity, &Label, &Triples, Option<&mut DocumentLinks>), Changed<Triples>>,
48    mut commands: Commands,
49) {
50    const SOURCE: &'static str = "owl:imports";
51    for (e, label, triples, mut links) in &mut query {
52        if let Some(links) = links.as_mut() {
53            links.retain(|e| e.1 != SOURCE);
54        }
55
56        let new_links: Vec<_> = triples
57            .0
58            .iter()
59            .filter(|t| t.predicate.as_str() == owl::imports.iriref().as_str())
60            .flat_map(|t| Url::parse(t.object.as_str()))
61            .map(|obj| (obj, SOURCE))
62            .collect();
63
64        for (u, _) in &new_links {
65            tracing::debug!("owl:imports {} to {}", label.as_str(), u);
66        }
67
68        if !new_links.is_empty() {
69            match links {
70                Some(mut links) => {
71                    links.extend(new_links);
72                }
73                None => {
74                    commands.entity(e).insert(DocumentLinks(new_links));
75                }
76            }
77        }
78    }
79}