Skip to main content

swls_lang_turtle/
config.rs

1use std::{borrow::Cow, collections::HashMap};
2
3use bevy_ecs::{
4    resource::Resource,
5    system::Res,
6    world::{CommandQueue, World},
7};
8use swls_core::{lsp_types::TextDocumentItem, prelude::*, systems::ConfiguredOntology};
9
10use crate::prefix::{find, prefix_from_source, prefix_from_url};
11
12pub fn extract_known_shapes_from_config<C: Client + ClientSync + Resource + Clone>(
13    config: Res<ServerConfig>,
14    client: Res<C>,
15    fs: Res<Fs>,
16    sender: Res<CommandSender>,
17) {
18    for on in config.config.local.shapes.iter() {
19        // Owned: it moves into the spawned future.
20        let on = on.clone();
21        let c = client.clone();
22        let fs = fs.clone();
23        let sender = sender.0.clone();
24
25        let fut = async move {
26            tracing::debug!("A FUTURE IS STARTING");
27            let Some(files) = find(&on, &fs, &c).await else {
28                return;
29            };
30
31            for (content, url) in files {
32                let mut command_queue = CommandQueue::default();
33                let item = TextDocumentItem {
34                    version: 1,
35                    uri: url.clone(),
36                    language_id: String::from("turtle"),
37                    text: String::new(),
38                };
39
40                let spawn = spawn_background(
41                    url.clone(),
42                    (
43                        RopeC(LineIndex::new(&content)),
44                        Source(content.clone()),
45                        Label(url.clone()),
46                        Wrapped(item),
47                        Types(HashMap::new()),
48                    ),
49                    Some("turtle".into()),
50                    (Global,),
51                );
52
53                command_queue.push(move |world: &mut World| {
54                    let span = tracing::span!(tracing::Level::INFO, "span shapes");
55                    let _enter = span.enter();
56                    spawn(world);
57                    world.run_schedule(ParseLabel);
58                });
59
60                let _ = sender.unbounded_send(command_queue);
61            }
62        };
63
64        client.spawn(fut);
65    }
66}
67
68pub fn extract_known_prefixes_from_config<C: Client + ClientSync + Resource + Clone>(
69    config: Res<ServerConfig>,
70    client: Res<C>,
71    fs: Res<Fs>,
72    sender: Res<CommandSender>,
73) {
74    for on in config.config.local.ontologies.iter() {
75        // Owned: it moves into the spawned future.
76        let on = on.clone();
77        let c = client.clone();
78        let fs = fs.clone();
79        let sender = sender.0.clone();
80
81        let fut = async move {
82            tracing::debug!("A FUTURE IS STARTING");
83            let Some(files) = find(&on, &fs, &c).await else {
84                return;
85            };
86
87            let mut queue = CommandQueue::default();
88            for (content, location) in files {
89                let Some((prefix, url)) =
90                    prefix_from_source(&location, &content).or_else(|| prefix_from_url(&location))
91                else {
92                    continue;
93                };
94
95                tracing::debug!(
96                    "Adding local prefix from ontologies config location {} prefix {}",
97                    url,
98                    prefix
99                );
100
101                let lov = swls_lov::LocalPrefix {
102                    location: Cow::Owned(location.to_string()),
103                    namespace: Cow::Owned(url.to_string()),
104                    content: Cow::Owned(content),
105                    name: prefix.clone(),
106                    title: prefix,
107                    rank: 0,
108                };
109
110                queue.push(move |world: &mut World| {
111                    // Loading the configuration again (it changed) must not
112                    // add an ontology twice.
113                    let known = world
114                        .query_filtered::<&swls_lov::LocalPrefix, bevy_ecs::query::With<ConfiguredOntology>>()
115                        .iter(world)
116                        .any(|l| l.location == lov.location);
117                    if !known {
118                        world.spawn((lov, ConfiguredOntology));
119                        world.resource_mut::<Revisions>().sources += 1;
120                    }
121                });
122            }
123
124            // Let open documents pick up the new ontologies (links, fetches).
125            queue.push(|world: &mut World| world.run_schedule(ParseLabel));
126            let _ = sender.unbounded_send(queue);
127        };
128
129        client.spawn(fut);
130    }
131}