Skip to main content

swls_core/
lib.rs

1#![doc(
2    html_logo_url = "https://ajuvercr.github.io/semantic-web-lsp/assets/icons/favicon.png",
3    html_favicon_url = "https://ajuvercr.github.io/semantic-web-lsp/assets/icons/favicon.ico"
4)]
5//! Core and common implementation for the semantic web language server.
6//!
7//! Proivdes the backbone for the [semantic web lsp binary](../lsp_bin/index.html) and [semantic web
8//! lsp wasm](../lsp_web/index.html).
9//!
10//! With the language server protocol, each different request is handled by an ECS schedule,
11//! combining different systems together.
12//! A system can generate new data and attach it to an entity, a document, or use this data to
13//! respond to requests.
14//!
15//! Language specific implementations that handle things like tokenizing and parsing are
16//! implemented in separate crates. The binary currently supports [Turtle](../swls_lang_turtle/index.html), [JSON-LD](../swls_lang_jsonld/index.html) and [SPARQL](../swls_lang_sparql/index.html).
17//! The goal is that each language at least generates [`Tokens`], [`Triples`] and
18//! [`Prefixes`].
19//! These components are then used to derive properties for autcompletion but also derive
20//! [`TokenComponent`] and [`TripleComponent`] enabling completion.
21//!
22//! The different schedules can be found at [`prelude::feature`].
23//!
24//! ## Example add completion for all subjects that start with `a`
25//! ```
26//! use bevy_ecs::prelude::*;
27//! use swls_core::prelude::*;
28//! # use sophia_api::dataset::Dataset;
29//! # use sophia_api::prelude::Quad;
30//!
31//! // Define the extra data struct
32//! #[derive(Component)]
33//! struct MyCompletions {
34//!     subjects: Vec<String>,
35//! }
36//!
37//! // Define the generating system
38//! // Don't forget to add it to the ecs later
39//! fn generate_my_completion(
40//!   // Only derive the completions when the document is parsed fully, aka is not Dirty
41//!   query: Query<(Entity, &Triples), (Changed<Triples>, Without<Dirty>)>,
42//!   mut commands: Commands,
43//! ) {
44//!   for (e, triples) in &query {
45//!     let mut subjects = Vec::new();
46//!     for q in triples.quads().flatten() {
47//!       if q.s().as_str().starts_with('a') {
48//!         subjects.push(q.s().as_str().to_string());
49//!       }
50//!     }
51//!     commands.entity(e).insert(MyCompletions { subjects });
52//!   }
53//! }
54//!
55//! // Define a system that adds these completions to the completion request
56//! fn complete_my_completion(
57//!   mut query: Query<(
58//!     &TokenComponent, &TripleComponent, &MyCompletions, &mut CompletionRequest
59//!   )>,
60//! ) {
61//!   for (token, this_triple, completions, mut request) in &mut query {
62//!     if this_triple.target == TripleTarget::Subject {
63//!       for my_completion in &completions.subjects {
64//!         request.push(
65//!           SimpleCompletion::new(
66//!             swls_core::lsp_types::CompletionItemKind::FIELD,
67//!             my_completion.clone(),
68//!             swls_core::lsp_types::TextEdit {
69//!               range: token.range.clone(),
70//!               new_text: my_completion.clone(),
71//!             }
72//!           )
73//!         )
74//!       }
75//!     }
76//!   }
77//! }
78//! ```
79//! Note that [`Prefixes`] can help expand and shorten iri's in a document.
80//!
81//!
82
83use bevy_ecs::{prelude::*, schedule::ScheduleLabel};
84use prelude::SemanticTokensDict;
85use systems::{init_ontology_extractor, populate_known_ontologies, OntologyExtractor};
86pub use tower_lsp::lsp_types;
87
88use crate::prelude::*;
89
90/// Main language tower_lsp server implementation.
91///
92/// [`Backend`](struct@backend::Backend) implements [`LanguageServer`](tower_lsp::LanguageServer).
93/// Each incoming request a schedule is ran on the main [`World`].
94pub mod backend;
95
96/// Handle platform specific implementations for fetching and spawning tasks.
97pub mod client;
98pub mod store;
99/// Common utils
100///
101/// Includes range transformations between [`std::ops::Range`] and [`swls_core::lsp_types::Range`].
102/// And commonly used [`Spanned`].
103pub mod util;
104
105/// Defines all common [`Component`]s and [`Resource`]s
106///
107/// In this [`World`], [Entity]s are documents and [`Components`](`Component`) are derived from these documents.
108/// Different [`System`]s derive new [`Components`](`Component`) from existing [`Components`](`Component`), that are added to
109/// the [`Entity`].
110/// For example, if [`Triples`] are defined, [systems::derive_classes] will
111/// derive [`DefinedClass`](struct@systems::DefinedClass) from them and add them to the [`Entity`].
112pub mod components;
113/// Hosts all common features of the semantic language server.
114pub mod feature;
115/// Defines common language traits
116pub mod lang;
117pub mod prelude;
118pub mod systems;
119
120/// Initializes a [`World`], including [`Resources`](`Resource`) and [`Schedules`].
121/// All systems defined in [`crate`] are added to the [`World`].
122pub fn setup_schedule_labels<C: Client + Resource>(world: &mut World) {
123    world.init_resource::<SemanticTokensDict>();
124    world.init_resource::<TypeHierarchy>();
125    world.init_resource::<Ontologies>();
126    world.insert_resource(OntologyExtractor::new());
127
128    parse::setup_schedule::<C>(world);
129    hover::setup_schedule(world);
130    completion::setup_schedule(world);
131    rename::setup_schedules(world);
132    diagnostics::setup_schedule(world);
133    save::setup_schedule(world);
134    format::setup_schedule(world);
135    references::setup_schedule(world);
136    inlay::setup_schedule(world);
137    goto_definition::setup_schedule(world);
138    goto_type::setup_schedule(world);
139    code_action::setup_schedule(world);
140
141    semantic::setup_world(world);
142
143    world.add_schedule(Schedule::new(Tasks));
144
145    let mut schedule = Schedule::new(Startup);
146    schedule.add_systems((
147        init_ontology_extractor,
148        populate_known_ontologies,
149        // extract_known_prefixes_from_config::<C>,
150    ));
151
152    world.add_schedule(schedule);
153    world.add_schedule(Schedule::new(Started));
154}
155
156/// Event triggers when a document is opened
157///
158/// Example
159/// ```rust
160/// # use swls_core::components::DynLang;
161/// # use swls_core::CreateEvent;
162/// # use swls_core::lang::LangHelper;
163/// # use bevy_ecs::event::EntityEvent;
164/// # use bevy_ecs::prelude::{Commands, On, World, Component};
165///
166/// #[derive(Component)]
167/// pub struct TurtleLang;
168///
169/// #[derive(Debug)]
170/// pub struct TurtleHelper;
171/// impl LangHelper for TurtleHelper {
172///     fn keyword(&self) -> &[&'static str] {
173///         &[
174///             "@prefix",
175///             "@base",
176///             "a",
177///         ]
178///     }
179/// }
180///
181/// let mut world = World::new();
182/// // This example tells the ECS system that the document is Turtle,
183/// // adding Turtle specific components
184/// world.add_observer(|trigger: On<CreateEvent>, mut commands: Commands| {
185///     match &trigger.event().language_id {
186///         Some(x) if x == "turtle" => {
187///             commands
188///                 .entity(trigger.event_target())
189///                 .insert((TurtleLang, DynLang(Box::new(TurtleHelper))));
190///             return;
191///         }
192///         _ => {}
193///     }
194///     if trigger.event().url.as_str().ends_with(".ttl") {
195///         commands
196///             .entity(trigger.event_target())
197///             .insert((TurtleLang, DynLang(Box::new(TurtleHelper))));
198///         return;
199///     }
200/// });
201/// ```
202///
203#[derive(EntityEvent)]
204pub struct CreateEvent {
205    pub url: crate::lsp_types::Url,
206    pub language_id: Option<String>,
207    #[event_target]
208    pub entity: Entity,
209}
210
211/// [`ScheduleLabel`] related to the Tasks schedule
212/// This schedule is used for async tasks, things that should be done at some point.
213///
214/// For example [`systems::handle_tasks`] spawns command queues sent with
215/// [`CommandSender`]
216#[derive(ScheduleLabel, Clone, Eq, PartialEq, Debug, Hash)]
217pub struct Tasks;
218
219#[derive(ScheduleLabel, Clone, Eq, PartialEq, Debug, Hash)]
220pub struct Startup;
221
222#[derive(ScheduleLabel, Clone, Eq, PartialEq, Debug, Hash)]
223pub struct Started;