Skip to main content

swls_core/
workspace.rs

1//! The workspace's RDF files, loaded in the background.
2//!
3//! At startup ([`index_workspace`]) every RDF file in the workspace folders is
4//! loaded as a [`Background`] document, so its classes, properties and shapes
5//! are known without opening it. Files git ignores (`.gitignore`, excludes,
6//! `.ignore`), hidden folders, `node_modules` and `target` are skipped, also
7//! for files created later. File changes keep them current (see
8//! [`crate::document`]). Disable with `"disabled": ["workspace_index"]`.
9
10use bevy_ecs::prelude::*;
11
12use crate::{
13    lsp_types::Url,
14    prelude::*,
15    systems::{imports::unsupported_syntax, spawn_document},
16};
17
18/// Extensions of the files the workspace index loads. RDF/XML (`.rdf`) and
19/// JSON-LD are left out; `.owl` files that turn out to be RDF/XML are skipped.
20pub const WORKSPACE_EXTENSIONS: &[&str] = &[".ttl", ".nt", ".owl", ".trig", ".n3"];
21
22/// At most this many files are loaded per workspace folder.
23pub const MAX_WORKSPACE_FILES: usize = 500;
24
25/// Larger files are skipped (generated data dumps, not vocabularies).
26pub const MAX_WORKSPACE_FILE_BYTES: usize = 5 * 1024 * 1024;
27
28/// Whether `url` is a file the workspace index loads.
29pub fn is_workspace_file(url: &Url) -> bool {
30    WORKSPACE_EXTENSIONS
31        .iter()
32        .any(|ext| url.path().ends_with(ext))
33}
34
35/// The workspace folder `url` is inside, if any.
36pub fn workspace_root(config: &ServerConfig, url: &Url) -> Option<Url> {
37    config
38        .workspaces
39        .iter()
40        .find(|w| {
41            let root = w.uri.as_str().trim_end_matches('/');
42            url.as_str()
43                .strip_prefix(root)
44                .is_some_and(|rest| rest.starts_with('/'))
45        })
46        .map(|w| w.uri.clone())
47}
48
49/// Load a workspace file from disk as a background document (unless it is
50/// too large or not in a syntax swls reads).
51pub async fn load_workspace_file(fs: &Fs, url: Url, sender: &CommandSender) {
52    let Some(content) = fs.0.read_file(&url).await else {
53        return;
54    };
55    if content.len() > MAX_WORKSPACE_FILE_BYTES {
56        tracing::info!("Not indexing {} ({} bytes)", url, content.len());
57        return;
58    }
59    if let Some(syntax) = unsupported_syntax(&content) {
60        tracing::info!("Not indexing {}: {}", url, syntax);
61        return;
62    }
63    // The language follows the extension.
64    spawn_document(url, content, None, &sender.0, |_, _| {});
65}
66
67/// Load the RDF files of every workspace folder in the background.
68pub fn index_workspace<C: Client + Resource>(
69    config: Res<ServerConfig>,
70    fs: Res<Fs>,
71    sender: Res<CommandSender>,
72    client: Res<C>,
73) {
74    if config.config.local.is_disabled(Disabled::WorkspaceIndex) {
75        return;
76    }
77    for folder in &config.workspaces {
78        let root = folder.uri.clone();
79        let fs = fs.clone();
80        let sender = sender.clone();
81        client.spawn(async move {
82            let files =
83                fs.0.walk(&root, WORKSPACE_EXTENSIONS, MAX_WORKSPACE_FILES)
84                    .await;
85            tracing::info!("Indexing {} files in {}", files.len(), root);
86            for url in files {
87                load_workspace_file(&fs, url, &sender).await;
88            }
89        });
90    }
91}