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
120 changes: 27 additions & 93 deletions crates/oxc_language_server/src/code_actions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use tower_lsp_server::lsp_types::{
CodeAction, CodeActionKind, Position, Range, TextEdit, Uri, WorkspaceEdit,
};
use tower_lsp_server::lsp_types::{CodeAction, CodeActionKind, TextEdit, Uri, WorkspaceEdit};

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

Expand All @@ -11,6 +9,7 @@ fn fix_content_to_code_action(
fixed_content: &FixedContent,
uri: &Uri,
alternative_message: &str,
is_preferred: bool,
) -> CodeAction {
// 1) Use `fixed_content.message` if it exists
// 2) Try to parse the report diagnostic message
Expand All @@ -29,7 +28,7 @@ fn fix_content_to_code_action(
CodeAction {
title,
kind: Some(CodeActionKind::QUICKFIX),
is_preferred: Some(true),
is_preferred: Some(is_preferred),
edit: Some(WorkspaceEdit {
#[expect(clippy::disallowed_types)]
changes: Some(std::collections::HashMap::from([(
Expand All @@ -48,17 +47,31 @@ fn fix_content_to_code_action(
pub fn apply_fix_code_actions(report: &DiagnosticReport, uri: &Uri) -> Option<Vec<CodeAction>> {
match &report.fixed_content {
PossibleFixContent::None => None,
PossibleFixContent::Single(fixed_content) => {
Some(vec![fix_content_to_code_action(fixed_content, uri, &report.diagnostic.message)])
PossibleFixContent::Single(fixed_content) => Some(vec![fix_content_to_code_action(
fixed_content,
uri,
&report.diagnostic.message,
true,
)]),
PossibleFixContent::Multiple(fixed_contents) => {
// only the first code action is preferred
let mut preferred = true;
Some(
fixed_contents
.iter()
.map(|fixed_content| {
let action = fix_content_to_code_action(
fixed_content,
uri,
&report.diagnostic.message,
preferred,
);
preferred = false;
action
})
.collect(),
)
}
PossibleFixContent::Multiple(fixed_contents) => Some(
fixed_contents
.iter()
.map(|fixed_content| {
fix_content_to_code_action(fixed_content, uri, &report.diagnostic.message)
})
.collect(),
),
}
}

Expand Down Expand Up @@ -108,82 +121,3 @@ pub fn apply_all_fix_code_action<'a>(
command: None,
})
}

pub fn ignore_this_line_code_action(report: &DiagnosticReport, uri: &Uri) -> CodeAction {
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.
// If it does, it needs to be appended to instead of a completely new line inserted.
CodeAction {
title: rule_name.as_ref().map_or_else(
|| "Disable oxlint for this line".into(),
|s| format!("Disable {s} for this line"),
),
kind: Some(CodeActionKind::QUICKFIX),
is_preferred: Some(false),
edit: Some(WorkspaceEdit {
#[expect(clippy::disallowed_types)]
changes: Some(std::collections::HashMap::from([(
uri.clone(),
vec![TextEdit {
range: Range {
start: Position {
line: report.diagnostic.range.start.line,
// TODO: character should be set to match the first non-whitespace character in the source text to match the existing indentation.
character: 0,
},
end: Position {
line: report.diagnostic.range.start.line,
// TODO: character should be set to match the first non-whitespace character in the source text to match the existing indentation.
character: 0,
},
},
new_text: rule_name.as_ref().map_or_else(
|| "// oxlint-disable-next-line\n".into(),
|s| format!("// oxlint-disable-next-line {s}\n"),
),
}],
)])),
..WorkspaceEdit::default()
}),
disabled: None,
data: None,
diagnostics: None,
command: None,
}
}

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

CodeAction {
title: rule_name.as_ref().map_or_else(
|| "Disable oxlint for this file".into(),
|s| format!("Disable {s} for this file"),
),
kind: Some(CodeActionKind::QUICKFIX),
is_preferred: Some(false),
edit: Some(WorkspaceEdit {
#[expect(clippy::disallowed_types)]
changes: Some(std::collections::HashMap::from([(
uri.clone(),
vec![TextEdit {
range: Range {
start: Position { line: 0, character: 0 },
end: Position { line: 0, character: 0 },
},
new_text: rule_name.as_ref().map_or_else(
|| "// oxlint-disable\n".into(),
|s| format!("// oxlint-disable {s}\n"),
),
}],
)])),
..WorkspaceEdit::default()
}),
disabled: None,
data: None,
diagnostics: None,
command: None,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::LSP_MAX_INT;
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,6 +144,5 @@ 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 @@ -134,7 +134,6 @@ impl IsolatedLintHandler {
data: None,
},
fixed_content: PossibleFixContent::None,
rule_name: None,
});
}
}
Expand Down
Loading
Loading