diff --git a/crates/ruff_wasm/src/lib.rs b/crates/ruff_wasm/src/lib.rs index b113491b44..1fe7aa89d6 100644 --- a/crates/ruff_wasm/src/lib.rs +++ b/crates/ruff_wasm/src/lib.rs @@ -18,7 +18,6 @@ use ruff::rules::{ use ruff::settings::configuration::Configuration; use ruff::settings::options::Options; use ruff::settings::{defaults, flags, Settings}; -use ruff_diagnostics::Edit; use ruff_python_ast::source_code::{Indexer, Locator, SourceLocation, Stylist}; #[wasm_bindgen(typescript_custom_section)] @@ -37,7 +36,7 @@ export interface Diagnostic { fix: { message: string | null; edits: { - content: string; + content: string | null; location: { row: number; column: number; @@ -51,12 +50,6 @@ export interface Diagnostic { }; "#; -#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)] -pub struct ExpandedFix { - message: Option, - edits: Vec, -} - #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)] pub struct ExpandedMessage { pub code: String, @@ -66,6 +59,19 @@ pub struct ExpandedMessage { pub fix: Option, } +#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)] +pub struct ExpandedFix { + message: Option, + edits: Vec, +} + +#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)] +struct ExpandedEdit { + location: SourceLocation, + end_location: SourceLocation, + content: Option, +} + #[wasm_bindgen(start)] pub fn run() { use log::Level; @@ -220,7 +226,15 @@ pub fn check(contents: &str, options: JsValue) -> Result { end_location, fix: message.fix.map(|fix| ExpandedFix { message: message.kind.suggestion, - edits: fix.into_edits(), + edits: fix + .into_edits() + .into_iter() + .map(|edit| ExpandedEdit { + location: source_code.source_location(edit.start()), + end_location: source_code.source_location(edit.end()), + content: edit.content().map(ToString::to_string), + }) + .collect(), }), } }) diff --git a/playground/src/Editor/SourceEditor.tsx b/playground/src/Editor/SourceEditor.tsx index c039cbf302..492b0836cc 100644 --- a/playground/src/Editor/SourceEditor.tsx +++ b/playground/src/Editor/SourceEditor.tsx @@ -54,7 +54,7 @@ export default function SourceEditor({ provideCodeActions: function (model, position) { const actions = diagnostics .filter((check) => position.startLineNumber === check.location.row) - .filter((check) => check.fix) + .filter(({ fix }) => fix) .map((check) => ({ title: check.fix ? check.fix.message @@ -71,11 +71,11 @@ export default function SourceEditor({ edit: { range: { startLineNumber: edit.location.row, - startColumn: edit.location.column + 1, + startColumn: edit.location.column, endLineNumber: edit.end_location.row, - endColumn: edit.end_location.column + 1, + endColumn: edit.end_location.column, }, - text: edit.content, + text: edit.content || "", }, })), }