mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-03 15:15:33 +00:00
Enable Quick Fix in the playground (#1395)
This commit is contained in:
parent
0d35087bc6
commit
658cb87ddd
3 changed files with 66 additions and 15 deletions
|
@ -14,7 +14,8 @@
|
||||||
],
|
],
|
||||||
"rules": {
|
"rules": {
|
||||||
// Disable some recommended rules that we don't want to enforce.
|
// Disable some recommended rules that we don't want to enforce.
|
||||||
"@typescript-eslint/no-explicit-any": "off"
|
"@typescript-eslint/no-explicit-any": "off",
|
||||||
|
"@typescript-eslint/no-empty-function": "off"
|
||||||
},
|
},
|
||||||
"settings": {
|
"settings": {
|
||||||
"react": {
|
"react": {
|
||||||
|
|
|
@ -61,17 +61,17 @@ const defaultConfig = getDefaultConfig(AVAILABLE_OPTIONS);
|
||||||
|
|
||||||
export default function App() {
|
export default function App() {
|
||||||
const monaco = useMonaco();
|
const monaco = useMonaco();
|
||||||
const [ruffInitialized, setRuffInitialized] = useState<boolean>(false);
|
const [initialized, setInitialized] = useState<boolean>(false);
|
||||||
const [config, setConfig] = useState<Config | null>(null);
|
const [config, setConfig] = useState<Config | null>(null);
|
||||||
const [source, setSource] = useState<string | null>(null);
|
const [source, setSource] = useState<string | null>(null);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
init().then(() => setRuffInitialized(true));
|
init().then(() => setInitialized(true));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (source === null && config === null && monaco) {
|
if (source == null && config == null && monaco) {
|
||||||
const [config, source] = restoreConfigAndSource();
|
const [config, source] = restoreConfigAndSource();
|
||||||
setConfig(config);
|
setConfig(config);
|
||||||
setSource(source);
|
setSource(source);
|
||||||
|
@ -87,13 +87,7 @@ export default function App() {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const editor = monaco?.editor;
|
const editor = monaco?.editor;
|
||||||
const model = editor?.getModels()[0];
|
const model = editor?.getModels()[0];
|
||||||
if (
|
if (!editor || !model || !initialized || source == null || config == null) {
|
||||||
!editor ||
|
|
||||||
!model ||
|
|
||||||
!ruffInitialized ||
|
|
||||||
source === null ||
|
|
||||||
config === null
|
|
||||||
) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,11 +112,52 @@ export default function App() {
|
||||||
severity: MarkerSeverity.Error,
|
severity: MarkerSeverity.Error,
|
||||||
}))
|
}))
|
||||||
);
|
);
|
||||||
}, [config, source, monaco, ruffInitialized]);
|
|
||||||
|
const codeActionProvider = monaco?.languages.registerCodeActionProvider(
|
||||||
|
"python",
|
||||||
|
{
|
||||||
|
// @ts-expect-error: The type definition is wrong.
|
||||||
|
provideCodeActions: function (model, position) {
|
||||||
|
const actions = checks
|
||||||
|
.filter((check) => position.startLineNumber === check.location.row)
|
||||||
|
.filter((check) => check.fix)
|
||||||
|
.map((check) => ({
|
||||||
|
title: `Fix ${check.code}`,
|
||||||
|
id: `fix-${check.code}`,
|
||||||
|
kind: "quickfix",
|
||||||
|
edit: check.fix
|
||||||
|
? {
|
||||||
|
edits: [
|
||||||
|
{
|
||||||
|
resource: model.uri,
|
||||||
|
versionId: model.getVersionId(),
|
||||||
|
edit: {
|
||||||
|
range: {
|
||||||
|
startLineNumber: check.fix.location.row,
|
||||||
|
startColumn: check.fix.location.column + 1,
|
||||||
|
endLineNumber: check.fix.end_location.row,
|
||||||
|
endColumn: check.fix.end_location.column + 1,
|
||||||
|
},
|
||||||
|
text: check.fix.content,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
|
}));
|
||||||
|
return { actions, dispose: () => {} };
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
codeActionProvider?.dispose();
|
||||||
|
};
|
||||||
|
}, [config, source, monaco, initialized]);
|
||||||
|
|
||||||
const handleEditorChange = useCallback(
|
const handleEditorChange = useCallback(
|
||||||
(value: string | undefined) => {
|
(value: string | undefined) => {
|
||||||
value && setSource(value);
|
setSource(value || "");
|
||||||
},
|
},
|
||||||
[setSource]
|
[setSource]
|
||||||
);
|
);
|
||||||
|
|
|
@ -5,6 +5,7 @@ use rustpython_parser::lexer::LexResult;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
use crate::autofix::Fix;
|
||||||
use crate::checks::CheckCode;
|
use crate::checks::CheckCode;
|
||||||
use crate::directives;
|
use crate::directives;
|
||||||
use crate::linter::check_path;
|
use crate::linter::check_path;
|
||||||
|
@ -27,6 +28,17 @@ export interface Check {
|
||||||
row: number;
|
row: number;
|
||||||
column: number;
|
column: number;
|
||||||
};
|
};
|
||||||
|
fix: {
|
||||||
|
content: string;
|
||||||
|
location: {
|
||||||
|
row: number;
|
||||||
|
column: number;
|
||||||
|
};
|
||||||
|
end_location: {
|
||||||
|
row: number;
|
||||||
|
column: number;
|
||||||
|
};
|
||||||
|
} | null;
|
||||||
};
|
};
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
|
@ -36,6 +48,7 @@ struct Message {
|
||||||
message: String,
|
message: String,
|
||||||
location: Location,
|
location: Location,
|
||||||
end_location: Location,
|
end_location: Location,
|
||||||
|
fix: Option<Fix>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen(start)]
|
#[wasm_bindgen(start)]
|
||||||
|
@ -71,7 +84,7 @@ pub fn check(contents: &str, options: JsValue) -> Result<JsValue, JsValue> {
|
||||||
&locator,
|
&locator,
|
||||||
&directives,
|
&directives,
|
||||||
&settings,
|
&settings,
|
||||||
false.into(),
|
flags::Autofix::Enabled,
|
||||||
flags::Noqa::Enabled,
|
flags::Noqa::Enabled,
|
||||||
)
|
)
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| e.to_string())?;
|
||||||
|
@ -83,6 +96,7 @@ pub fn check(contents: &str, options: JsValue) -> Result<JsValue, JsValue> {
|
||||||
message: check.kind.body(),
|
message: check.kind.body(),
|
||||||
location: check.location,
|
location: check.location,
|
||||||
end_location: check.end_location,
|
end_location: check.end_location,
|
||||||
|
fix: check.fix,
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
@ -118,7 +132,8 @@ mod test {
|
||||||
code: CheckCode::F634,
|
code: CheckCode::F634,
|
||||||
message: "If test is a tuple, which is always `True`".to_string(),
|
message: "If test is a tuple, which is always `True`".to_string(),
|
||||||
location: Location::new(1, 0),
|
location: Location::new(1, 0),
|
||||||
end_location: Location::new(1, 15)
|
end_location: Location::new(1, 15),
|
||||||
|
fix: None,
|
||||||
}]
|
}]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue