Skip to main content

swls_core/systems/
mod.rs

1use bevy_ecs::prelude::*;
2
3use crate::prelude::*;
4
5mod shapes;
6use completion::{CompletionRequest, SimpleCompletion};
7pub use shapes::*;
8mod typed;
9pub use typed::*;
10mod links;
11pub use links::*;
12pub(crate) mod imports;
13pub use imports::{import_url, open_imports};
14pub mod prefix;
15mod prefix_diagnostics;
16pub use prefix_diagnostics::{
17    add_missing_prefix_code_action, prefix_diagnostic_helper, prefix_diagnostics,
18};
19mod prefix_hover;
20pub use prefix_hover::{get_current_prefix, goto_prefix, hover_prefix, PrefixComponent};
21mod namespace_properties;
22use crate::lsp_types::CompletionItemKind;
23pub use namespace_properties::{
24    unknown_property_code_action, validate_namespace_properties, ALLOW_PROPERTY_COMMAND,
25};
26mod properties;
27pub use properties::{
28    complete_class, complete_properties, derive_ontologies, hover_class, hover_excluded_property,
29    hover_property, DefinedClass, DefinedClasses, DefinedProperties, DefinedProperty,
30};
31mod lov;
32pub use lov::{
33    check_added_ontology_extract, configured_url, fetch_lov_properties, init_ontology_extractor,
34    namespace_sources, populate_known_ontologies, spawn_document, ConfiguredOntology, FromPrefix,
35    NamespaceSource, OntologyExtractor, PreferredPrefixes, PrefixEntry,
36};
37use tracing::instrument;
38
39pub fn spawn_or_insert(
40    url: crate::lsp_types::Url,
41    bundle: impl Bundle,
42    language_id: Option<String>,
43    extra: impl Bundle,
44) -> impl (FnOnce(&mut World) -> Entity) + 'static + Send + Sync {
45    move |world: &mut World| {
46        let out = if let Some(entity) = find_document(world, &url) {
47            world.entity_mut(entity).insert(bundle).insert(extra);
48            entity
49        } else {
50            let entity = world.spawn(bundle).insert(extra).id();
51            world.trigger(CreateEvent {
52                url,
53                language_id,
54                entity,
55            });
56            entity
57        };
58
59        // Several systems add links (prefixes, owl:imports). If the component
60        // were missing, each would insert its own through deferred commands in
61        // the same pass, and only the last one would survive.
62        if !world.entity(out).contains::<DocumentLinks>() {
63            world.entity_mut(out).insert(DocumentLinks(Vec::new()));
64        }
65
66        // Callers parse once they have added everything (`ParseLabel`); parsing
67        // here as well made every new document parse twice.
68        world.flush();
69        out
70    }
71}
72
73/// Like [`spawn_or_insert`], for documents the server loads in the background
74/// (LOV ontologies, `owl:imports`, configured ontologies and shapes).
75///
76/// If the user has a document with the same URL [`Open`], their buffer is the
77/// source of truth: `content` is dropped and only `extra` is added. Without this
78/// guard a late fetch, or an `owl:imports` of a file being edited, would replace
79/// the editor's text with the copy on disk.
80pub fn spawn_background(
81    url: crate::lsp_types::Url,
82    content: impl Bundle,
83    language_id: Option<String>,
84    extra: impl Bundle,
85) -> impl (FnOnce(&mut World) -> Entity) + 'static + Send + Sync {
86    move |world: &mut World| {
87        let open = find_document(world, &url).filter(|&e| world.entity(e).contains::<Open>());
88        if let Some(entity) = open {
89            tracing::debug!(
90                "{} is open in the editor, keeping the editor's content",
91                url
92            );
93            world.entity_mut(entity).insert((extra, Background));
94            return entity;
95        }
96        spawn_or_insert(url, content, language_id, (extra, Background))(world)
97    }
98}
99
100pub fn handle_tasks(mut commands: Commands, mut receiver: ResMut<CommandReceiver>) {
101    while let Ok(mut com) = receiver.0.try_recv() {
102        commands.append(&mut com);
103    }
104}
105
106#[instrument(skip(query, config))]
107pub fn keyword_complete(
108    mut query: Query<(
109        Option<&TokenComponent>,
110        &PositionComponent,
111        &DynLang,
112        &mut CompletionRequest,
113    )>,
114    config: Res<ServerConfig>,
115) {
116    if config.config.local.is_disabled(Disabled::CompletionKeyword) {
117        return;
118    }
119    tracing::debug!("Keyword complete!");
120    for (m_token, position, helper, mut req) in &mut query {
121        let range = if let Some(ct) = m_token {
122            ct.range
123        } else {
124            crate::lsp_types::Range {
125                start: position.0,
126                end: position.0,
127            }
128        };
129
130        for kwd in helper.keyword() {
131            let completion = SimpleCompletion::new(
132                CompletionItemKind::KEYWORD,
133                kwd.to_string(),
134                crate::lsp_types::TextEdit {
135                    range,
136                    new_text: kwd.to_string(),
137                },
138            );
139            req.push(completion);
140        }
141    }
142}