Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions crates/oxc_language_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl LanguageServer for Backend {

// ToDo: add support for multiple workspace folders
// maybe fallback when the client does not support it
let root_worker = WorkspaceWorker::new(&params.root_uri.unwrap());
let root_worker = WorkspaceWorker::new(params.root_uri.unwrap());

// When the client did not send our custom `initialization_options`,
// or the client does not support `workspace/configuration` request,
Expand Down Expand Up @@ -144,7 +144,7 @@ impl LanguageServer for Backend {
let needed_configurations = needed_configurations.pin_owned();
for worker in workers {
if worker.needs_init_linter().await {
needed_configurations.insert(worker.get_root_uri().unwrap(), worker);
needed_configurations.insert(worker.get_root_uri().clone(), worker);
}
}

Expand Down Expand Up @@ -200,12 +200,7 @@ impl LanguageServer for Backend {
{
let configs = self
.request_workspace_configuration(
workers
.iter()
.map(|worker| worker.get_root_uri().unwrap())
.collect::<Vec<_>>()
.iter()
.collect(),
workers.iter().map(worker::WorkspaceWorker::get_root_uri).collect(),
)
.await;

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_language_server/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl Tester<'_> {
.expect("could not get current dir")
.join(self.relative_root_dir);
let uri = Uri::from_file_path(absolute_path).expect("could not convert current dir to uri");
let worker = WorkspaceWorker::new(&uri);
let worker = WorkspaceWorker::new(uri);
worker.init_linter(&self.options.clone().unwrap_or_default()).await;

worker
Expand Down
32 changes: 13 additions & 19 deletions crates/oxc_language_server/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{str::FromStr, vec};

use log::debug;
use rustc_hash::FxBuildHasher;
use tokio::sync::{Mutex, OnceCell, RwLock};
use tokio::sync::{Mutex, RwLock};
use tower_lsp_server::{
UriExt,
lsp_types::{CodeActionOrCommand, Diagnostic, FileEvent, Range, TextEdit, Uri},
Expand All @@ -18,42 +18,36 @@ use crate::{
};

pub struct WorkspaceWorker {
root_uri: OnceCell<Uri>,
root_uri: Uri,
server_linter: RwLock<Option<ServerLinter>>,
diagnostics_report_map: RwLock<ConcurrentHashMap<String, Vec<DiagnosticReport>>>,
options: Mutex<Options>,
}

impl WorkspaceWorker {
pub fn new(root_uri: &Uri) -> Self {
let root_uri_cell = OnceCell::new();
root_uri_cell.set(root_uri.clone()).unwrap();

pub fn new(root_uri: Uri) -> Self {
Self {
root_uri: root_uri_cell,
root_uri,
server_linter: RwLock::new(None),
diagnostics_report_map: RwLock::new(ConcurrentHashMap::default()),
options: Mutex::new(Options::default()),
}
}

pub fn get_root_uri(&self) -> Option<Uri> {
self.root_uri.get().cloned()
pub fn get_root_uri(&self) -> &Uri {
&self.root_uri
}

pub fn is_responsible_for_uri(&self, uri: &Uri) -> bool {
if let Some(root_uri) = self.root_uri.get() {
if let Some(path) = uri.to_file_path() {
return path.starts_with(root_uri.to_file_path().unwrap());
}
if let Some(path) = uri.to_file_path() {
return path.starts_with(self.root_uri.to_file_path().unwrap());
}
false
}

pub async fn init_linter(&self, options: &Options) {
*self.options.lock().await = options.clone();
*self.server_linter.write().await =
Some(ServerLinter::new(self.root_uri.get().unwrap(), options));
*self.server_linter.write().await = Some(ServerLinter::new(&self.root_uri, options));
}

pub async fn needs_init_linter(&self) -> bool {
Expand All @@ -66,7 +60,7 @@ impl WorkspaceWorker {

async fn refresh_server_linter(&self) {
let options = self.options.lock().await;
let server_linter = ServerLinter::new(self.root_uri.get().unwrap(), &options);
let server_linter = ServerLinter::new(&self.root_uri, &options);

*self.server_linter.write().await = Some(server_linter);
}
Expand Down Expand Up @@ -266,14 +260,14 @@ mod tests {

#[test]
fn test_get_root_uri() {
let worker = WorkspaceWorker::new(&Uri::from_str("file:///root/").unwrap());
let worker = WorkspaceWorker::new(Uri::from_str("file:///root/").unwrap());

assert_eq!(worker.get_root_uri(), Some(Uri::from_str("file:///root/").unwrap()));
assert_eq!(worker.get_root_uri(), &Uri::from_str("file:///root/").unwrap());
}

#[test]
fn test_is_responsible() {
let worker = WorkspaceWorker::new(&Uri::from_str("file:///path/to/root").unwrap());
let worker = WorkspaceWorker::new(Uri::from_str("file:///path/to/root").unwrap());

assert!(
worker.is_responsible_for_uri(&Uri::from_str("file:///path/to/root/file.js").unwrap())
Expand Down
Loading