Skip to main content

swls_core/systems/
mod.rs

1use bevy_ecs::prelude::*;
2
3use crate::prelude::*;
4
5#[cfg(feature = "shapes")]
6mod shapes;
7use completion::{CompletionRequest, SimpleCompletion};
8#[cfg(feature = "shapes")]
9pub use shapes::*;
10mod typed;
11pub use typed::*;
12mod links;
13pub use links::*;
14pub mod prefix;
15use crate::lsp_types::CompletionItemKind;
16mod properties;
17pub use properties::{
18    complete_class, complete_properties, derive_ontologies, hover_class, hover_property,
19    DefinedClass, DefinedClasses, DefinedProperties, DefinedProperty,
20};
21mod lov;
22pub use lov::{
23    check_added_ontology_extract, fetch_lov_properties, init_ontology_extractor, open_imports,
24    populate_known_ontologies, FromPrefix, OntologyExtractor, PrefixEntry,
25};
26use tracing::instrument;
27
28pub fn spawn_or_insert(
29    url: crate::lsp_types::Url,
30    bundle: impl Bundle,
31    language_id: Option<String>,
32    extra: impl Bundle,
33) -> impl (FnOnce(&mut World) -> Entity) + 'static + Send + Sync {
34    move |world: &mut World| {
35        let out = if let Some(entity) = world
36            .query::<(Entity, &Label)>()
37            .iter(&world)
38            .find(|x| x.1 .0 == url)
39            .map(|x| x.0)
40        {
41            world.entity_mut(entity).insert(bundle).insert(extra);
42            entity
43        } else {
44            let entity = world.spawn(bundle).insert(extra).id();
45            world.trigger(CreateEvent {
46                url,
47                language_id,
48                entity,
49            });
50            entity
51        };
52
53        world.flush();
54        world.run_schedule(ParseLabel);
55        out
56    }
57}
58
59pub fn handle_tasks(mut commands: Commands, mut receiver: ResMut<CommandReceiver>) {
60    while let Ok(Some(mut com)) = receiver.0.try_next() {
61        commands.append(&mut com);
62    }
63}
64
65#[instrument(skip(query))]
66pub fn keyword_complete(
67    mut query: Query<(
68        Option<&TokenComponent>,
69        &PositionComponent,
70        &DynLang,
71        &mut CompletionRequest,
72    )>,
73) {
74    tracing::debug!("Keyword complete!");
75    for (m_token, position, helper, mut req) in &mut query {
76        let range = if let Some(ct) = m_token {
77            ct.range
78        } else {
79            crate::lsp_types::Range {
80                start: position.0,
81                end: position.0,
82            }
83        };
84
85        for kwd in helper.keyword() {
86            let completion = SimpleCompletion::new(
87                CompletionItemKind::KEYWORD,
88                kwd.to_string(),
89                crate::lsp_types::TextEdit {
90                    range: range.clone(),
91                    new_text: kwd.to_string(),
92                },
93            );
94            req.push(completion);
95        }
96    }
97}