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 mod prefix;
13use lsp_types::CompletionItemKind;
14mod properties;
15pub use properties::{
16 complete_class, complete_properties, derive_classes, derive_properties, hover_class,
17 hover_property, DefinedClass, DefinedClasses, DefinedProperties, DefinedProperty,
18};
19mod lov;
20pub use lov::{
21 check_added_ontology_extract, fetch_lov_properties, init_onology_extractor, open_imports,
22 FromPrefix, OntologyExtractor,
23};
24use tracing::instrument;
25
26pub fn spawn_or_insert(
27 url: lsp_types::Url,
28 bundle: impl Bundle,
29 language_id: Option<String>,
30 extra: impl Bundle,
31) -> impl (FnOnce(&mut World) -> Entity) + 'static + Send + Sync {
32 move |world: &mut World| {
33 let out = if let Some(entity) = world
34 .query::<(Entity, &Label)>()
35 .iter(&world)
36 .find(|x| x.1 .0 == url)
37 .map(|x| x.0)
38 {
39 world.entity_mut(entity).insert(bundle).insert(extra);
40 entity
41 } else {
42 let entity = world.spawn(bundle).insert(extra).id();
43 world.trigger_targets(CreateEvent { url, language_id }, entity);
44 entity
45 };
46
47 world.flush_commands();
48 world.run_schedule(ParseLabel);
49 out
50 }
51}
52
53pub fn handle_tasks(mut commands: Commands, mut receiver: ResMut<CommandReceiver>) {
54 while let Ok(Some(mut com)) = receiver.0.try_next() {
55 commands.append(&mut com);
56 }
57}
58
59#[instrument(skip(query))]
60pub fn keyword_complete(
61 mut query: Query<(
62 Option<&TokenComponent>,
63 &PositionComponent,
64 &DynLang,
65 &mut CompletionRequest,
66 )>,
67) {
68 tracing::debug!("Keyword complete!");
69 for (m_token, position, helper, mut req) in &mut query {
70 let range = if let Some(ct) = m_token {
71 ct.range
72 } else {
73 lsp_types::Range {
74 start: position.0,
75 end: position.0,
76 }
77 };
78
79 for kwd in helper.keyword() {
80 let completion = SimpleCompletion::new(
81 CompletionItemKind::KEYWORD,
82 kwd.to_string(),
83 lsp_types::TextEdit {
84 range: range.clone(),
85 new_text: kwd.to_string(),
86 },
87 );
88 req.push(completion);
89 }
90 }
91}
92
93#[instrument(skip(query))]
94pub fn inlay_triples(mut query: Query<(&Triples, &RopeC, &mut InlayRequest)>) {
95 for (triples, rope, mut req) in &mut query {
96 let mut out = Vec::new();
97 for t in triples.iter() {
98 let Some(position) = offset_to_position(t.span.end, &rope) else {
99 continue;
100 };
101 out.push(lsp_types::InlayHint {
102 position,
103 label: lsp_types::InlayHintLabel::String(format!("{}", t)),
104 kind: None,
105 text_edits: None,
106 tooltip: None,
107 padding_left: None,
108 padding_right: None,
109 data: None,
110 });
111 }
112 req.0 = Some(out);
113 }
114}