1use bevy_ecs::prelude::*;
11
12use crate::{
13 lsp_types::Url,
14 prelude::*,
15 systems::{imports::unsupported_syntax, spawn_document},
16};
17
18pub const WORKSPACE_EXTENSIONS: &[&str] = &[".ttl", ".nt", ".owl", ".trig", ".n3"];
21
22pub const MAX_WORKSPACE_FILES: usize = 500;
24
25pub const MAX_WORKSPACE_FILE_BYTES: usize = 5 * 1024 * 1024;
27
28pub fn is_workspace_file(url: &Url) -> bool {
30 WORKSPACE_EXTENSIONS
31 .iter()
32 .any(|ext| url.path().ends_with(ext))
33}
34
35pub 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
49pub 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 spawn_document(url, content, None, &sender.0, |_, _| {});
65}
66
67pub 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}