Skip to content

Commit a7fd005

Browse files
committed
refactor(language_server): store serde_json::Value for WorkspaceWorker option
1 parent 26955eb commit a7fd005

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

crates/oxc_language_server/src/worker.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ pub struct WorkspaceWorker {
3030
root_uri: Uri,
3131
server_linter: RwLock<Option<ServerLinter>>,
3232
server_formatter: RwLock<Option<ServerFormatter>>,
33-
options: Mutex<Option<Options>>,
33+
// Initialized options from the client
34+
// If None, the worker has not been initialized yet
35+
options: Mutex<Option<serde_json::Value>>,
3436
}
3537

3638
impl WorkspaceWorker {
@@ -69,8 +71,8 @@ impl WorkspaceWorker {
6971
/// Start all programs (linter, formatter) for the worker.
7072
/// This should be called after the client has sent the workspace configuration.
7173
pub async fn start_worker(&self, options: &serde_json::Value) {
72-
let options = serde_json::from_value::<Options>(options.clone()).unwrap_or_default();
7374
*self.options.lock().await = Some(options.clone());
75+
let options = serde_json::from_value::<Options>(options.clone()).unwrap_or_default();
7476

7577
*self.server_linter.write().await = Some(ServerLinter::new(&self.root_uri, &options.lint));
7678
if options.format.experimental {
@@ -92,20 +94,20 @@ impl WorkspaceWorker {
9294

9395
// clone the options to avoid locking the mutex
9496
let options = self.options.lock().await;
95-
let lint_options = options.as_ref().map(|o| o.lint.clone()).unwrap_or_default();
96-
let format_options = options.as_ref().map(|o| o.format.clone()).unwrap_or_default();
97+
let options = serde_json::from_value::<Options>(options.clone().unwrap_or_default())
98+
.unwrap_or_default();
9799
let lint_patterns = self
98100
.server_linter
99101
.read()
100102
.await
101103
.as_ref()
102-
.map(|linter| linter.get_watch_patterns(&lint_options, root_path));
104+
.map(|linter| linter.get_watch_patterns(&options.lint, root_path));
103105
let format_patterns = self
104106
.server_formatter
105107
.read()
106108
.await
107109
.as_ref()
108-
.map(|formatter| formatter.get_watcher_patterns(&format_options));
110+
.map(|formatter| formatter.get_watcher_patterns(&options.format));
109111

110112
if let Some(lint_patterns) = lint_patterns {
111113
registrations.push(Registration {
@@ -126,7 +128,7 @@ impl WorkspaceWorker {
126128
});
127129
}
128130

129-
if format_options.experimental
131+
if options.format.experimental
130132
&& let Some(format_patterns) = format_patterns
131133
{
132134
registrations.push(Registration {
@@ -189,7 +191,10 @@ impl WorkspaceWorker {
189191
/// Check if the linter should run for the given run type
190192
/// This compares the current run level from the options with the given run type
191193
pub async fn should_lint_on_run_type(&self, current_run: Run) -> bool {
192-
let run_level = { self.options.lock().await.as_ref().map(|o| o.lint.run) };
194+
let options = self.options.lock().await;
195+
let run_level = options.as_ref().and_then(|o| {
196+
serde_json::from_value::<Options>(o.clone()).ok().map(|opts| opts.lint.run)
197+
});
193198

194199
run_level == Some(current_run)
195200
}
@@ -331,16 +336,16 @@ impl WorkspaceWorker {
331336
server_linter.get_cached_files_of_diagnostics()
332337
};
333338
let options = self.options.lock().await;
334-
let format_options = options.as_ref().map(|o| o.format.clone()).unwrap_or_default();
335-
let lint_options = options.as_ref().map(|o| o.lint.clone()).unwrap_or_default();
339+
let options = serde_json::from_value::<Options>(options.clone().unwrap_or_default())
340+
.unwrap_or_default();
336341

337-
if format_options.experimental {
342+
if options.format.experimental {
338343
tokio::join!(
339-
self.refresh_server_formatter(&format_options),
340-
self.refresh_server_linter(&lint_options)
344+
self.refresh_server_formatter(&options.format),
345+
self.refresh_server_linter(&options.lint)
341346
);
342347
} else {
343-
self.refresh_server_linter(&lint_options).await;
348+
self.refresh_server_linter(&options.lint).await;
344349
}
345350

346351
Some(self.revalidate_diagnostics(files).await)
@@ -368,9 +373,9 @@ impl WorkspaceWorker {
368373
// Scope the first lock so it is dropped before the second lock
369374
let current_option = {
370375
let options_guard = self.options.lock().await;
371-
options_guard.clone()
372-
}
373-
.unwrap_or_default();
376+
let current_value = options_guard.clone().unwrap_or_default();
377+
serde_json::from_value::<Options>(current_value).unwrap_or_default()
378+
};
374379

375380
debug!(
376381
"
@@ -382,7 +387,7 @@ impl WorkspaceWorker {
382387

383388
{
384389
let mut options_guard = self.options.lock().await;
385-
*options_guard = Some(changed_options.clone());
390+
*options_guard = Some(serde_json::to_value(&changed_options).unwrap_or_default());
386391
}
387392

388393
let mut formatting = false;

0 commit comments

Comments
 (0)