Skip to content

Commit f391c08

Browse files
committed
refactor(langauge_server): introdude LSPFileSystem
1 parent f70cdae commit f391c08

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::sync::Arc;
2+
3+
use tower_lsp_server::lsp_types::Uri;
4+
5+
use crate::ConcurrentHashMap;
6+
7+
pub struct LSPFileSystem {
8+
files: Arc<ConcurrentHashMap<Uri, String>>,
9+
}
10+
11+
impl LSPFileSystem {
12+
pub fn new() -> Self {
13+
Self { files: Arc::new(ConcurrentHashMap::default()) }
14+
}
15+
16+
pub fn clear(&self) {
17+
self.files.pin().clear();
18+
}
19+
20+
pub fn set(&self, uri: &Uri, content: String) {
21+
self.files.pin().insert(uri.clone(), content);
22+
}
23+
24+
#[expect(dead_code)] // used for the oxc_formatter in the future
25+
pub fn get(&self, uri: &Uri) -> Option<String> {
26+
self.files.pin().get(uri).cloned()
27+
}
28+
29+
pub fn remove(&self, uri: &Uri) {
30+
self.files.pin().remove(uri);
31+
}
32+
}

crates/oxc_language_server/src/main.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use tower_lsp_server::{
2222
mod capabilities;
2323
mod code_actions;
2424
mod commands;
25+
mod file_system;
2526
mod formatter;
2627
mod linter;
2728
mod options;
@@ -36,6 +37,8 @@ use linter::server_linter::ServerLinterRun;
3637
use options::{Options, WorkspaceOption};
3738
use worker::WorkspaceWorker;
3839

40+
use crate::file_system::LSPFileSystem;
41+
3942
type ConcurrentHashMap<K, V> = papaya::HashMap<K, V, FxBuildHasher>;
4043

4144
const OXC_CONFIG_FILE: &str = ".oxlintrc.json";
@@ -51,6 +54,7 @@ struct Backend {
5154
// 2. `workspace/didChangeWorkspaceFolders` request
5255
workspace_workers: Arc<RwLock<Vec<WorkspaceWorker>>>,
5356
capabilities: OnceCell<Capabilities>,
57+
file_system: Arc<RwLock<LSPFileSystem>>,
5458
}
5559

5660
impl LanguageServer for Backend {
@@ -203,6 +207,7 @@ impl LanguageServer for Backend {
203207

204208
async fn shutdown(&self) -> Result<()> {
205209
self.clear_all_diagnostics().await;
210+
self.file_system.write().await.clear();
206211
Ok(())
207212
}
208213

@@ -444,6 +449,10 @@ impl LanguageServer for Backend {
444449
let Some(worker) = workers.iter().find(|worker| worker.is_responsible_for_uri(uri)) else {
445450
return;
446451
};
452+
453+
// saving the file means we can read again from the file system
454+
self.file_system.write().await.remove(uri);
455+
447456
if let Some(diagnostics) = worker.lint_file(uri, None, ServerLinterRun::OnSave).await {
448457
self.client
449458
.publish_diagnostics(
@@ -464,6 +473,11 @@ impl LanguageServer for Backend {
464473
return;
465474
};
466475
let content = params.content_changes.first().map(|c| c.text.clone());
476+
477+
if let Some(content) = &content {
478+
self.file_system.write().await.set(uri, content.to_string());
479+
}
480+
467481
if let Some(diagnostics) = worker.lint_file(uri, content, ServerLinterRun::OnType).await {
468482
self.client
469483
.publish_diagnostics(
@@ -483,6 +497,9 @@ impl LanguageServer for Backend {
483497
};
484498

485499
let content = params.text_document.text;
500+
501+
self.file_system.write().await.set(uri, content.to_string());
502+
486503
if let Some(diagnostics) =
487504
worker.lint_file(uri, Some(content), ServerLinterRun::Always).await
488505
{
@@ -502,6 +519,7 @@ impl LanguageServer for Backend {
502519
let Some(worker) = workers.iter().find(|worker| worker.is_responsible_for_uri(uri)) else {
503520
return;
504521
};
522+
self.file_system.write().await.remove(uri);
505523
worker.remove_diagnostics(&params.text_document.uri).await;
506524
}
507525

@@ -626,6 +644,7 @@ async fn main() {
626644
client,
627645
workspace_workers: Arc::new(RwLock::new(vec![])),
628646
capabilities: OnceCell::new(),
647+
file_system: Arc::new(RwLock::new(LSPFileSystem::new())),
629648
})
630649
.finish();
631650

0 commit comments

Comments
 (0)