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
19 changes: 3 additions & 16 deletions crates/oxc_language_server/src/code_actions.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
use tower_lsp_server::lsp_types::{
CodeAction, CodeActionKind, Diagnostic, NumberOrString, Position, Range, TextEdit, Uri,
WorkspaceEdit,
CodeAction, CodeActionKind, Position, Range, TextEdit, Uri, WorkspaceEdit,
};

use crate::linter::error_with_position::{DiagnosticReport, FixedContent, PossibleFixContent};

pub const CODE_ACTION_KIND_SOURCE_FIX_ALL_OXC: CodeActionKind =
CodeActionKind::new("source.fixAll.oxc");

// TODO: Would be better if we had exact rule name from the diagnostic instead of having to parse it.
fn get_rule_name(diagnostic: &Diagnostic) -> Option<String> {
if let Some(NumberOrString::String(code)) = &diagnostic.code {
let open_paren = code.chars().position(|c| c == '(')?;
let close_paren = code.chars().position(|c| c == ')')?;

return Some(code[(open_paren + 1)..close_paren].to_string());
}

None
}

fn fix_content_to_code_action(
fixed_content: &FixedContent,
uri: &Uri,
Expand Down Expand Up @@ -123,7 +110,7 @@ pub fn apply_all_fix_code_action<'a>(
}

pub fn ignore_this_line_code_action(report: &DiagnosticReport, uri: &Uri) -> CodeAction {
let rule_name = get_rule_name(&report.diagnostic);
let rule_name = report.rule_name.as_ref();

// TODO: This CodeAction doesn't support disabling multiple rules by name for a given line.
// To do that, we need to read `report.diagnostic.range.start.line` and check if a disable comment already exists.
Expand Down Expand Up @@ -168,7 +155,7 @@ pub fn ignore_this_line_code_action(report: &DiagnosticReport, uri: &Uri) -> Cod
}

pub fn ignore_this_rule_code_action(report: &DiagnosticReport, uri: &Uri) -> CodeAction {
let rule_name = get_rule_name(&report.diagnostic);
let rule_name = report.rule_name.as_ref();

CodeAction {
title: rule_name.as_ref().map_or_else(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const LSP_MAX_INT: u32 = 2u32.pow(31) - 1;
pub struct DiagnosticReport {
pub diagnostic: lsp_types::Diagnostic,
pub fixed_content: PossibleFixContent,
pub rule_name: Option<String>,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -145,5 +146,6 @@ pub fn message_with_position_to_lsp_diagnostic_report(
fixes.iter().map(fix_with_position_to_fix_content).collect(),
),
},
rule_name: message.code.number.as_ref().map(std::string::ToString::to_string),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl IsolatedLintHandler {
data: None,
},
fixed_content: PossibleFixContent::None,
rule_name: None,
});
}
}
Expand Down
Loading