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/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl LanguageServer for Backend {
version: Some(server_version.to_string()),
}),
offset_encoding: None,
capabilities: capabilities.into(),
capabilities: capabilities.server_capabilities(&self.tool_builders),
})
}

Expand Down
28 changes: 14 additions & 14 deletions crates/oxc_language_server/src/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tower_lsp_server::lsp_types::{
WorkDoneProgressOptions, WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities,
};

use crate::linter::{CODE_ACTION_KIND_SOURCE_FIX_ALL_OXC, FIX_ALL_COMMAND_ID};
use crate::ToolBuilder;

#[derive(Clone, Default)]
pub struct Capabilities {
Expand Down Expand Up @@ -57,9 +57,15 @@ impl From<ClientCapabilities> for Capabilities {
}
}

impl From<Capabilities> for ServerCapabilities {
fn from(value: Capabilities) -> Self {
Self {
impl Capabilities {
pub fn server_capabilities(&self, tools: &[Box<dyn ToolBuilder>]) -> ServerCapabilities {
let code_action_kinds: Vec<CodeActionKind> =
tools.iter().flat_map(|tool| tool.provided_code_action_kinds()).collect();

let commands: Vec<String> =
tools.iter().flat_map(|tool| tool.provided_commands()).collect();

ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Options(
TextDocumentSyncOptions {
change: Some(TextDocumentSyncKind::FULL),
Expand All @@ -77,12 +83,9 @@ impl From<Capabilities> for ServerCapabilities {
}),
file_operations: None,
}),
code_action_provider: if value.code_action_provider {
code_action_provider: if self.code_action_provider && !code_action_kinds.is_empty() {
Some(CodeActionProviderCapability::Options(CodeActionOptions {
code_action_kinds: Some(vec![
CodeActionKind::QUICKFIX,
CODE_ACTION_KIND_SOURCE_FIX_ALL_OXC,
]),
code_action_kinds: Some(code_action_kinds),
work_done_progress_options: WorkDoneProgressOptions {
work_done_progress: None,
},
Expand All @@ -91,11 +94,8 @@ impl From<Capabilities> for ServerCapabilities {
} else {
None
},
execute_command_provider: if value.workspace_execute_command {
Some(ExecuteCommandOptions {
commands: vec![FIX_ALL_COMMAND_ID.to_string()],
..Default::default()
})
execute_command_provider: if self.workspace_execute_command && !commands.is_empty() {
Some(ExecuteCommandOptions { commands, ..Default::default() })
} else {
None
},
Expand Down
2 changes: 0 additions & 2 deletions crates/oxc_language_server/src/linter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ mod server_linter;
#[cfg(test)]
mod tester;

pub use code_actions::CODE_ACTION_KIND_SOURCE_FIX_ALL_OXC;
pub use commands::FIX_ALL_COMMAND_ID;
pub use server_linter::ServerLinterBuilder;

const LINT_CONFIG_FILE: &str = ".oxlintrc.json";
13 changes: 11 additions & 2 deletions crates/oxc_language_server/src/linter/server_linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ use oxc_linter::{
use crate::{
ConcurrentHashMap,
linter::{
CODE_ACTION_KIND_SOURCE_FIX_ALL_OXC, LINT_CONFIG_FILE,
code_actions::{apply_all_fix_code_action, apply_fix_code_actions, fix_all_text_edit},
LINT_CONFIG_FILE,
code_actions::{
CODE_ACTION_KIND_SOURCE_FIX_ALL_OXC, apply_all_fix_code_action, apply_fix_code_actions,
fix_all_text_edit,
},
commands::{FIX_ALL_COMMAND_ID, FixAllCommandArgs},
config_walker::ConfigWalker,
error_with_position::DiagnosticReport,
Expand Down Expand Up @@ -140,6 +143,12 @@ impl ServerLinterBuilder {
}

impl ToolBuilder for ServerLinterBuilder {
fn provided_code_action_kinds(&self) -> Vec<CodeActionKind> {
vec![CodeActionKind::QUICKFIX, CODE_ACTION_KIND_SOURCE_FIX_ALL_OXC]
}
fn provided_commands(&self) -> Vec<String> {
vec![FIX_ALL_COMMAND_ID.to_string()]
}
fn build_boxed(&self, root_uri: &Uri, options: serde_json::Value) -> Box<dyn Tool> {
Box::new(ServerLinterBuilder::build(root_uri, options))
}
Expand Down
13 changes: 13 additions & 0 deletions crates/oxc_language_server/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ use tower_lsp_server::{
};

pub trait ToolBuilder: Send + Sync {
/// Get the commands provided by this tool.
/// This will be used to register the commands with the LSP Client.
fn provided_commands(&self) -> Vec<String> {
Vec::new()
}

/// Get the code action kinds provided by this tool.
/// This will be used to register the code action kinds with the LSP Client.
fn provided_code_action_kinds(&self) -> Vec<CodeActionKind> {
Vec::new()
}

/// Build a boxed instance of the tool for the given root URI and options.
fn build_boxed(&self, root_uri: &Uri, options: serde_json::Value) -> Box<dyn Tool>;
}

Expand Down
Loading