дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-28 13:32:29 +02:00
Editor › Code Style › Python has indent, tab character and hard wrap, but Ruff and Black ignored them: without a configuration of their own they always formatted with 4 spaces at 88 columns, whatever the editor used. Now, when no Ruff configuration applies to the file (`ruff.toml`, `.ruff.toml` or `pyproject.toml` with `[tool.ruff]` in its folder or above, the user's Ruff configuration, or the configured Ruff configuration directory), Ruff gets the indentation and line length the editor uses, as `--config line-length / indent-width / format.indent-style`: the file's `.editorconfig` (`indent_size`, `indent_style`, `max_line_length`), then the values set on the page. Black, without `[tool.black]` in its project (found as Black finds it) and without a user configuration where Black would read one, gets the line length as `--line-length` (it has no indentation settings). Values set in neither place are not passed: the global hard wrap (120) is not Ruff's or Black's default (88), so existing projects keep formatting as before. The page says when these values apply. Also adds Black as a selectable Python formatter (#5340's commits, which this branch was stacked on). Co-authored-by: wildcatDownstairs <[email protected]>
54 рядки
2.1 KiB
Rust
54 рядки
2.1 KiB
Rust
//! `cargo test -p ide-tools --test black -- --ignored --nocapture`
|
|
|
|
use ide_core::CancellationToken;
|
|
use ide_language::FormatterConfig;
|
|
use ide_tools::{Tools, formatter};
|
|
use std::path::Path;
|
|
|
|
fn format(
|
|
tools: &Tools,
|
|
path: &Path,
|
|
text: &str,
|
|
range: Option<formatter::FormatRange>,
|
|
) -> ide_core::IdeResult<String> {
|
|
let config = FormatterConfig { tool: "ruff".into(), parser: "python".into(), range: true };
|
|
let mut cmd = tools.formatter_command("black", path, None)?;
|
|
cmd.current_dir(path.parent().unwrap());
|
|
cmd.args(formatter::arguments("black", &config, path, 4, true, range, None));
|
|
formatter::run("black", cmd, text, &CancellationToken::new())
|
|
}
|
|
|
|
#[test]
|
|
#[ignore = "requires black on PATH"]
|
|
fn real_black_reads_project_config_formats_stdin_ranges_and_exclusions() {
|
|
let temp = tempfile::tempdir().unwrap();
|
|
let tools = Tools::new(&temp.path().join("app-data"));
|
|
if tools.info("black", &[]).unwrap().status == ide_tools::ToolStatus::Missing {
|
|
eprintln!("SKIP real Black: not on PATH");
|
|
return;
|
|
}
|
|
let project = temp.path().join("project with spaces");
|
|
std::fs::create_dir_all(project.join("gen")).unwrap();
|
|
std::fs::write(project.join("pyproject.toml"), "[tool.black]\nline-length = 20\nforce-exclude = \"gen/\"\n")
|
|
.unwrap();
|
|
let path = project.join("example.py");
|
|
let output = format(&tools, &path, "x = {'a': 1, 'b': 2, 'c': 3}\n", None).unwrap();
|
|
assert_eq!(output, "x = {\n \"a\": 1,\n \"b\": 2,\n \"c\": 3,\n}\n");
|
|
assert!(!path.exists(), "formatting must never write the file");
|
|
|
|
let range = formatter::FormatRange {
|
|
start_offset: 0,
|
|
end_offset: 0,
|
|
start_line: 2,
|
|
start_col: 1,
|
|
end_line: 2,
|
|
end_col: 100,
|
|
};
|
|
let output = format(&tools, &path, "a = 1\nb = 2\n", Some(range)).unwrap();
|
|
assert_eq!(output, "a = 1\nb = 2\n");
|
|
|
|
// An excluded file comes back empty; the host keeps the document as it is.
|
|
let output = format(&tools, &project.join("gen/m.py"), "x = { 'a':1 }\n", None).unwrap();
|
|
assert_eq!(output, "");
|
|
}
|