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
2 changes: 1 addition & 1 deletion crates/oxc_language_server/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ mod server_formatter;
#[cfg(test)]
mod tester;

pub use server_formatter::{ServerFormatter, ServerFormatterBuilder};
pub use server_formatter::ServerFormatterBuilder;

const FORMAT_CONFIG_FILES: &[&str; 2] = &[".oxfmtrc.json", ".oxfmtrc.jsonc"];
11 changes: 7 additions & 4 deletions crates/oxc_language_server/src/formatter/server_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,17 @@ pub struct ServerFormatter {
}

impl Tool for ServerFormatter {
fn name(&self) -> &'static str {
"formatter"
}
/// # Panics
/// Panics if the root URI cannot be converted to a file path.
fn handle_configuration_change(
&self,
root_uri: &Uri,
old_options_json: &serde_json::Value,
new_options_json: serde_json::Value,
) -> ToolRestartChanges<ServerFormatter> {
) -> ToolRestartChanges {
let old_option = match serde_json::from_value::<LSPFormatOptions>(old_options_json.clone())
{
Ok(opts) => opts,
Expand Down Expand Up @@ -145,7 +148,7 @@ impl Tool for ServerFormatter {
ServerFormatterBuilder::new(root_uri.clone(), new_options_json.clone()).build();
let watch_patterns = new_formatter.get_watcher_patterns(new_options_json);
ToolRestartChanges {
tool: Some(new_formatter),
tool: Some(Box::new(new_formatter)),
diagnostic_reports: None,
watch_patterns: Some(watch_patterns),
}
Expand Down Expand Up @@ -178,7 +181,7 @@ impl Tool for ServerFormatter {
_changed_uri: &Uri,
root_uri: &Uri,
options: serde_json::Value,
) -> ToolRestartChanges<Self> {
) -> ToolRestartChanges {
if !self.should_run {
return ToolRestartChanges {
tool: None,
Expand All @@ -192,7 +195,7 @@ impl Tool for ServerFormatter {
let new_formatter = ServerFormatterBuilder::new(root_uri.clone(), options).build();

ToolRestartChanges {
tool: Some(new_formatter),
tool: Some(Box::new(new_formatter)),
diagnostic_reports: None,
// TODO: update watch patterns if config_path changed
watch_patterns: None,
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_language_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod utils;
mod worker;

pub use crate::backend::Backend;
pub use crate::linter::ServerLinter;
pub use crate::worker::WorkspaceWorker;
pub use crate::tool::{Tool, ToolBuilder, ToolRestartChanges, ToolShutdownChanges};
pub use crate::worker::WorkspaceWorker; // TODO: remove pub use, `Backend` docs need it for now

pub type ConcurrentHashMap<K, V> = papaya::HashMap<K, V, FxBuildHasher>;
2 changes: 1 addition & 1 deletion crates/oxc_language_server/src/linter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ mod tester;

pub use code_actions::CODE_ACTION_KIND_SOURCE_FIX_ALL_OXC;
pub use commands::FIX_ALL_COMMAND_ID;
pub use server_linter::{ServerLinter, ServerLinterBuilder};
pub use server_linter::ServerLinterBuilder;

const LINT_CONFIG_FILE: &str = ".oxlintrc.json";
15 changes: 9 additions & 6 deletions crates/oxc_language_server/src/linter/server_linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use oxc_linter::{
LintIgnoreMatcher, LintOptions, Oxlintrc,
};

use crate::tool::{Tool, ToolBuilder, ToolShutdownChanges};
use crate::{
ConcurrentHashMap,
linter::{
Expand All @@ -30,7 +29,7 @@ use crate::{
isolated_lint_handler::{IsolatedLintHandler, IsolatedLintHandlerOptions},
options::{LintOptions as LSPLintOptions, Run, UnusedDisableDirectives},
},
tool::ToolRestartChanges,
tool::{Tool, ToolBuilder, ToolRestartChanges, ToolShutdownChanges},
utils::normalize_path,
};

Expand Down Expand Up @@ -244,6 +243,10 @@ pub struct ServerLinter {
}

impl Tool for ServerLinter {
fn name(&self) -> &'static str {
"linter"
}

fn shutdown(&self) -> ToolShutdownChanges {
ToolShutdownChanges {
uris_to_clear_diagnostics: Some(self.get_cached_files_of_diagnostics()),
Expand All @@ -257,7 +260,7 @@ impl Tool for ServerLinter {
root_uri: &Uri,
old_options_json: &serde_json::Value,
new_options_json: serde_json::Value,
) -> ToolRestartChanges<ServerLinter> {
) -> ToolRestartChanges {
let old_option = match serde_json::from_value::<LSPLintOptions>(old_options_json.clone()) {
Ok(opts) => opts,
Err(e) => {
Expand Down Expand Up @@ -303,7 +306,7 @@ impl Tool for ServerLinter {
};

ToolRestartChanges {
tool: Some(new_linter),
tool: Some(Box::new(new_linter)),
diagnostic_reports: diagnostics,
watch_patterns: patterns,
}
Expand Down Expand Up @@ -341,7 +344,7 @@ impl Tool for ServerLinter {
_changed_uri: &Uri,
root_uri: &Uri,
options: serde_json::Value,
) -> ToolRestartChanges<Self> {
) -> ToolRestartChanges {
// TODO: Check if the changed file is actually a config file (including extended paths)
let new_linter = ServerLinterBuilder::new(root_uri.clone(), options).build();

Expand All @@ -350,7 +353,7 @@ impl Tool for ServerLinter {
let diagnostics = Some(new_linter.revalidate_diagnostics(cached_files));

ToolRestartChanges {
tool: Some(new_linter),
tool: Some(Box::new(new_linter)),
diagnostic_reports: diagnostics,
// TODO: update watch patterns if config_path changed, or the extended paths changed
watch_patterns: None,
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_language_server/src/linter/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tower_lsp_server::{
};

use crate::{
linter::{ServerLinter, ServerLinterBuilder},
linter::{ServerLinterBuilder, server_linter::ServerLinter},
tool::{Tool, ToolBuilder},
};

Expand Down
16 changes: 11 additions & 5 deletions crates/oxc_language_server/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ pub trait ToolBuilder<T: Tool> {
fn build(&self) -> T;
}

pub trait Tool: Sized {
pub trait Tool: Send + Sync {
/// Get the name of the tool.
fn name(&self) -> &'static str;

/// The Server has new configuration changes.
/// Returns a [ToolRestartChanges] indicating what changes were made for the Tool.
fn handle_configuration_change(
&self,
root_uri: &Uri,
old_options_json: &serde_json::Value,
new_options_json: serde_json::Value,
) -> ToolRestartChanges<Self>;
) -> ToolRestartChanges;

/// Get the file watcher patterns for this tool based on the provided options.
/// These patterns will be used to watch for file changes relevant to the tool.
Expand All @@ -33,7 +36,7 @@ pub trait Tool: Sized {
changed_uri: &Uri,
root_uri: &Uri,
options: serde_json::Value,
) -> ToolRestartChanges<Self>;
) -> ToolRestartChanges;

/// Check if this tool is responsible for handling the given command.
fn is_responsible_for_command(&self, _command: &str) -> bool {
Expand All @@ -45,6 +48,9 @@ pub trait Tool: Sized {
/// If the command is recognized and executed it can return:
/// - `Ok(Some(WorkspaceEdit))` if the command was executed successfully and produced a workspace edit.
/// - `Ok(None)` if the command was executed successfully but did not produce any workspace edit.
///
/// # Errors
/// If there was an error executing the command, returns an `Err(ErrorCode)`.
fn execute_command(
&self,
_command: &str,
Expand Down Expand Up @@ -116,10 +122,10 @@ pub trait Tool: Sized {
}
}

pub struct ToolRestartChanges<T> {
pub struct ToolRestartChanges {
/// The tool that was restarted (linter, formatter).
/// If None, no tool was restarted.
pub tool: Option<T>,
pub tool: Option<Box<dyn Tool>>,
/// The diagnostic reports that need to be revalidated after the tool restart
pub diagnostic_reports: Option<Vec<(String, Vec<Diagnostic>)>>,
/// The patterns that were added during the tool restart
Expand Down
Loading
Loading