swls-core 0.1.0

Core LSP infrastructure for the Semantic Web Language Server
Documentation

Core and common implementation for the semantic web language server.

Proivdes the backbone for the semantic web lsp binary and semantic web lsp wasm.

With the language server protocol, each different request is handled by an ECS schedule, combining different systems together. A system can generate new data and attach it to an entity, a document, or use this data to respond to requests.

Language specific implementations that handle things like tokenizing and parsing are implemented in separate crates. The binary currently supports Turtle, JSON-LD and SPARQL. The goal is that each language at least generates [Tokens], [Triples] and [Prefixes]. These components are then used to derive properties for autcompletion but also derive [TokenComponent] and [TripleComponent] enabling completion.

The different schedules can be found at [prelude::feature].

Example add completion for all subjects that start with a

use bevy_ecs::prelude::*;
use swls_core::prelude::*;
# use sophia_api::dataset::Dataset;
# use sophia_api::prelude::Quad;

// Define the extra data struct
#[derive(Component)]
struct MyCompletions {
    subjects: Vec<String>,
}

// Define the generating system
// Don't forget to add it to the ecs later
fn generate_my_completion(
  // Only derive the completions when the document is parsed fully, aka is not Dirty
  query: Query<(Entity, &Triples), (Changed<Triples>, Without<Dirty>)>,
  mut commands: Commands,
) {
  for (e, triples) in &query {
    let mut subjects = Vec::new();
    for q in triples.quads().flatten() {
      if q.s().as_str().starts_with('a') {
        subjects.push(q.s().as_str().to_string());
      }
    }
    commands.entity(e).insert(MyCompletions { subjects });
  }
}

// Define a system that adds these completions to the completion request
fn complete_my_completion(
  mut query: Query<(
    &TokenComponent, &TripleComponent, &MyCompletions, &mut CompletionRequest
  )>,
) {
  for (token, this_triple, completions, mut request) in &mut query {
    if this_triple.target == TripleTarget::Subject {
      for my_completion in &completions.subjects {
        request.push(
          SimpleCompletion::new(
            swls_core::lsp_types::CompletionItemKind::FIELD,
            my_completion.clone(),
            swls_core::lsp_types::TextEdit {
              range: token.range.clone(),
              new_text: my_completion.clone(),
            }
          )
        )
      }
    }
  }
}

Note that [Prefixes] can help expand and shorten iri's in a document.