diff --git a/crates/ide-host/src/formatter.rs b/crates/ide-host/src/formatter.rs index c0f9b4912b..b0933ce16e 100644 --- a/crates/ide-host/src/formatter.rs +++ b/crates/ide-host/src/formatter.rs @@ -304,10 +304,21 @@ fn external_tool( let id = formatter_language(language.id.as_str()); let enabled = |tool| settings.formatter(tool).languages.iter().any(|l| l == id); if language.formatter.as_ref().is_some_and(|f| f.tool == "ruff") { - return Ok(if enabled(Black) && (settings.black.prefer || black_configured(file)) { - Some(Black) + let black = enabled(Black).then_some(Black); + let ruff = enabled(Ruff).then_some(Ruff); + // An explicit selection on either page wins over the other tool's configuration + // probe: Ruff chosen on its page formats even below a [tool.black] pyproject, + // Black chosen on its page formats without one. A tool whose languages opt the + // language out stays out. Automatic keeps the probe: a project with [tool.black] + // wants Black; both pages explicit falls back to that answer. + return Ok(if settings.black.prefer { + black.or(ruff) + } else if settings.ruff.prefer { + ruff + } else if black.is_some() && black_configured(file) { + black } else { - enabled(Ruff).then_some(Ruff) + ruff }); } let found = |names: &[&str]| names.iter().any(|name| formatter::find_up(file, Path::new(name)).is_some()); @@ -1214,6 +1225,27 @@ mod tests { assert_eq!(tool(None, "python", &svc, &settings).unwrap(), Some(Ruff)); } + #[test] + fn an_explicit_formatter_choice_beats_the_other_tools_configuration() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path(); + std::fs::create_dir_all(root.join("src")).unwrap(); + let file = root.join("src/main.py"); + let mut settings = LanguagesSettings::default(); + std::fs::write(root.join("pyproject.toml"), "[tool.black]\nline-length = 100\n").unwrap(); + assert_eq!(tool(None, "python", &file, &settings).unwrap(), Some(Black)); + // Ruff selected on its page: it formats even below [tool.black]. + settings.ruff.prefer = true; + assert_eq!(tool(None, "python", &file, &settings).unwrap(), Some(Ruff)); + // Both pages explicit falls back to the automatic answer (Black here). + settings.black.prefer = true; + assert_eq!(tool(None, "python", &file, &settings).unwrap(), Some(Black)); + // Black selected without any [tool.black]: Black still formats. + settings.ruff.prefer = false; + std::fs::remove_file(root.join("pyproject.toml")).unwrap(); + assert_eq!(tool(None, "python", &file, &settings).unwrap(), Some(Black)); + } + #[test] fn black_finds_its_configuration_like_black_does() { let dir = tempfile::tempdir().unwrap(); diff --git a/src/features/settings/languages/FormatterPage.tsx b/src/features/settings/languages/FormatterPage.tsx index ca250ffe68..d7f4447ace 100644 --- a/src/features/settings/languages/FormatterPage.tsx +++ b/src/features/settings/languages/FormatterPage.tsx @@ -199,7 +199,7 @@ export function RuffPage() { pathLabel="Ruff path" pathHint="Auto: managed installation, then PATH. An explicit path overrides auto detection." configurationDirectory - providerHint="Ruff reads the nearest pyproject.toml ([tool.ruff]), ruff.toml, or .ruff.toml. Use it before the language server when preferred." + providerHint="Ruff reads the nearest pyproject.toml ([tool.ruff]), ruff.toml, or .ruff.toml. When selected, Ruff formats Python before the language server, even below a pyproject.toml with [tool.black]." saveLabel="Format on save" saveHint="Run Ruff formatting on save for enabled languages when it is the selected formatting provider." > @@ -216,7 +216,7 @@ export function BlackPage() { pathLabel="Black path" pathHint="Auto: the project interpreter's environment, managed installation, then PATH. An explicit path overrides auto detection." autoLabel="Automatic (projects with [tool.black])" - providerHint="Black formats Python instead of Ruff below a pyproject.toml with [tool.black], or when it is the selected provider." + providerHint="Black formats Python instead of Ruff below a pyproject.toml with [tool.black]. When selected, Black formats Python without [tool.black] too, and before a selected Ruff." saveHint="Run Black on save for enabled languages when it is the selected formatting provider. Reformat code in Actions on Save also runs it." /> ); diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index 5223e8a399..5c8361050a 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -1291,7 +1291,7 @@ "Project configuration": "Project configuration", "Select Ruff Configuration Directory": "Select Ruff Configuration Directory", "Empty uses the nearest project pyproject.toml ([tool.ruff]), ruff.toml, or .ruff.toml. A configured directory must contain one of them.": "Empty uses the nearest project pyproject.toml ([tool.ruff]), ruff.toml, or .ruff.toml. A configured directory must contain one of them.", - "Ruff reads the nearest pyproject.toml ([tool.ruff]), ruff.toml, or .ruff.toml. Use it before the language server when preferred.": "Ruff reads the nearest pyproject.toml ([tool.ruff]), ruff.toml, or .ruff.toml. Use it before the language server when preferred.", + "Ruff reads the nearest pyproject.toml ([tool.ruff]), ruff.toml, or .ruff.toml. When selected, Ruff formats Python before the language server, even below a pyproject.toml with [tool.black].": "Ruff reads the nearest pyproject.toml ([tool.ruff]), ruff.toml, or .ruff.toml. When selected, Ruff formats Python before the language server, even below a pyproject.toml with [tool.black].", "Run Ruff formatting on save for enabled languages when it is the selected formatting provider.": "Run Ruff formatting on save for enabled languages when it is the selected formatting provider.", "Format on save": "Format on save", "Fix safe lint issues on save": "Fix safe lint issues on save", @@ -4237,7 +4237,7 @@ "Black formatting timed out": "Black formatting timed out", "Black path": "Black path", "Auto: the project interpreter's environment, managed installation, then PATH. An explicit path overrides auto detection.": "Auto: the project interpreter's environment, managed installation, then PATH. An explicit path overrides auto detection.", - "Black formats Python instead of Ruff below a pyproject.toml with [tool.black], or when it is the selected provider.": "Black formats Python instead of Ruff below a pyproject.toml with [tool.black], or when it is the selected provider.", + "Black formats Python instead of Ruff below a pyproject.toml with [tool.black]. When selected, Black formats Python without [tool.black] too, and before a selected Ruff.": "Black formats Python instead of Ruff below a pyproject.toml with [tool.black]. When selected, Black formats Python without [tool.black] too, and before a selected Ruff.", "Run Black on save for enabled languages when it is the selected formatting provider. Reformat code in Actions on Save also runs it.": "Run Black on save for enabled languages when it is the selected formatting provider. Reformat code in Actions on Save also runs it.", "Automatic (projects with [tool.black])": "Automatic (projects with [tool.black])", "{name} left the file unchanged: its configuration excludes it": "{name} left the file unchanged: its configuration excludes it", diff --git a/src/platform/locales/text/ko.json b/src/platform/locales/text/ko.json index 1d85851f14..30ce458ee6 100644 --- a/src/platform/locales/text/ko.json +++ b/src/platform/locales/text/ko.json @@ -1731,7 +1731,6 @@ "Run Prettier on save for enabled languages when it is the selected formatting provider.": "Prettier가 선택된 서식 지정 제공자일 때 활성화된 언어에 대해 저장 시 Prettier를 실행합니다.", "Run oxfmt on save for enabled languages when it is the selected formatting provider.": "oxfmt가 선택된 서식 지정 제공자인 경우 활성화된 언어에 대해 저장 시 oxfmt를 실행합니다.", "oxfmt formats an enabled language instead of Prettier below an oxfmt configuration file (.oxfmtrc.json, oxfmt.config.ts) or when it is the selected provider.": "oxfmt 구성 파일(.oxfmtrc.json, oxfmt.config.ts)이 있는 디렉터리 아래에 있거나 oxfmt가 선택된 서식 지정 제공자이면, 활성화된 언어를 Prettier 대신 oxfmt로 서식 지정합니다.", - "Ruff reads the nearest pyproject.toml ([tool.ruff]), ruff.toml, or .ruff.toml. Use it before the language server when preferred.": "Ruff는 가장 가까운 pyproject.toml([tool.ruff]), ruff.toml 또는 .ruff.toml을 읽습니다. 우선 사용하도록 설정하면 언어 서버보다 먼저 실행됩니다.", "Enabled languages": "활성화된 언어", "Reformat Code needs {name}. Install it via {method}?": "코드 서식 다시 지정에 {name}이(가) 필요합니다. {method}을(를) 통해 설치하시겠습니까?", "Install Missing Tool": "누락된 도구 설치", diff --git a/src/platform/locales/text/ru.json b/src/platform/locales/text/ru.json index b39a7ab620..ce38011502 100644 --- a/src/platform/locales/text/ru.json +++ b/src/platform/locales/text/ru.json @@ -2951,7 +2951,6 @@ "Run Prettier on save for enabled languages when it is the selected formatting provider.": "Запускать Prettier при сохранении для включённых языков, если он выбран в качестве поставщика форматирования.", "Run oxfmt on save for enabled languages when it is the selected formatting provider.": "Запускать oxfmt при сохранении для включённых языков, если он выбран в качестве поставщика форматирования.", "oxfmt formats an enabled language instead of Prettier below an oxfmt configuration file (.oxfmtrc.json, oxfmt.config.ts) or when it is the selected provider.": "oxfmt форматирует включённый язык вместо Prettier в каталогах с файлом конфигурации oxfmt (.oxfmtrc.json, oxfmt.config.ts) или когда он выбран в качестве поставщика.", - "Ruff reads the nearest pyproject.toml ([tool.ruff]), ruff.toml, or .ruff.toml. Use it before the language server when preferred.": "Ruff считывает ближайший pyproject.toml ([tool.ruff]), ruff.toml или .ruff.toml. Используйте его перед языковым сервером при необходимости.", "Enabled languages": "Включённые языки", "Reformat Code needs {name}. Install it via {method}?": "Для форматирования кода требуется {name}. Установить через {method}?", "{refactoring} needs {name}. Install it via {method}?": "Для «{refactoring}» требуется {name}. Установить через {method}?", @@ -10116,7 +10115,6 @@ "Black formatting timed out": "Превышено время ожидания форматирования Black", "Black path": "Путь к Black", "Auto: the project interpreter's environment, managed installation, then PATH. An explicit path overrides auto detection.": "Авто: окружение интерпретатора проекта, управляемая установка, затем PATH. Явно указанный путь переопределяет автоопределение.", - "Black formats Python instead of Ruff below a pyproject.toml with [tool.black], or when it is the selected provider.": "Black форматирует Python вместо Ruff в каталогах ниже pyproject.toml с [tool.black] или если он выбран в качестве поставщика.", "Run Black on save for enabled languages when it is the selected formatting provider. Reformat code in Actions on Save also runs it.": "Запускать Black при сохранении для включённых языков, если он выбран поставщиком форматирования. Параметр «Переформатировать код» в разделе «Действия при сохранении» также запускает его.", "Automatic (projects with [tool.black])": "Автоматически (проекты с [tool.black])", "{name} left the file unchanged: its configuration excludes it": "{name} оставил файл без изменений: он исключён его конфигурацией", diff --git a/src/platform/locales/text/tr.json b/src/platform/locales/text/tr.json index 180d6178e1..c787ed54f7 100644 --- a/src/platform/locales/text/tr.json +++ b/src/platform/locales/text/tr.json @@ -629,7 +629,6 @@ "Project configuration": "Proje yapılandırması", "Select Ruff Configuration Directory": "Ruff Yapılandırma Dizinini Seç", "Empty uses the nearest project pyproject.toml ([tool.ruff]), ruff.toml, or .ruff.toml. A configured directory must contain one of them.": "Boş bırakıldığında en yakın pyproject.toml ([tool.ruff]), ruff.toml veya .ruff.toml dosyası kullanılır. Belirtilen yapılandırma dizini bunlardan birini içermelidir.", - "Ruff reads the nearest pyproject.toml ([tool.ruff]), ruff.toml, or .ruff.toml. Use it before the language server when preferred.": "Ruff en yakın pyproject.toml ([tool.ruff]), ruff.toml veya .ruff.toml dosyasını okur. Tercih ediliyorsa dil sunucusundan önce kullanılır.", "Ruff formatting failed": "Ruff biçimlendirmesi başarısız", "Ruff formatting timed out": "Ruff biçimlendirmesi zaman aşımına uğradı", "Ruff configuration path does not exist: {path}": "Ruff yapılandırma yolu mevcut değil: {path}", diff --git a/src/platform/locales/text/zh-TW.json b/src/platform/locales/text/zh-TW.json index a48623902f..cf4989c802 100644 --- a/src/platform/locales/text/zh-TW.json +++ b/src/platform/locales/text/zh-TW.json @@ -2568,7 +2568,6 @@ "Fix Ruff safe lint issues": "修復 Ruff 安全的 lint 問題", "Runs ruff check --fix-only on Python files before composing the commit. This does not format code.": "提交前對 Python 檔案執行 ruff check --fix-only。此操作不會格式化程式碼。", "oxfmt formats an enabled language instead of Prettier below an oxfmt configuration file (.oxfmtrc.json, oxfmt.config.ts) or when it is the selected provider.": "在 oxfmt 設定檔(.oxfmtrc.json、oxfmt.config.ts)所在目錄及其子目錄中,或選擇 oxfmt 作為格式化工具時,oxfmt 將代替 Prettier 格式化已啟用的語言。", - "Ruff reads the nearest pyproject.toml ([tool.ruff]), ruff.toml, or .ruff.toml. Use it before the language server when preferred.": "Ruff 會讀取最近的 pyproject.toml([tool.ruff])、ruff.toml 或 .ruff.toml。選擇優先使用 Ruff 時,將優先於語言伺服器。", "Enabled languages": "啟用的語言", "Reformat Code needs {name}. Install it via {method}?": "重新格式化程式碼需要 {name}。是否透過 {method} 安裝?", "Install Missing Tool": "安裝缺失的工具", @@ -9741,5 +9740,7 @@ "The solution already contains a project with the same name as {names}.": "方案中已有與 {names} 同名的專案。", "Vue Language Server version": "Vue 語言伺服器版本", "Auto uses 3.0.x for Vue 2 projects and the latest release for Vue 3.": "自動選擇時,Vue 2 專案使用 3.0.x,Vue 3 專案使用最新版本。", - "Enter latest, 3.0.x, or an exact Vue Language Server version.": "請輸入 latest、3.0.x 或 Vue 語言伺服器的確切版本。" + "Enter latest, 3.0.x, or an exact Vue Language Server version.": "請輸入 latest、3.0.x 或 Vue 語言伺服器的確切版本。", + "Ruff reads the nearest pyproject.toml ([tool.ruff]), ruff.toml, or .ruff.toml. When selected, Ruff formats Python before the language server, even below a pyproject.toml with [tool.black].": "Ruff 會讀取最近的 pyproject.toml([tool.ruff])、ruff.toml 或 .ruff.toml。選擇 Ruff 後,它會先於語言伺服器格式化 Python,即使所在的 pyproject.toml 含有 [tool.black]。", + "Black formats Python instead of Ruff below a pyproject.toml with [tool.black]. When selected, Black formats Python without [tool.black] too, and before a selected Ruff.": "在含 [tool.black] 的 pyproject.toml 所在目錄及其子目錄中,Black 取代 Ruff 格式化 Python。選擇 Black 後,沒有 [tool.black] 時也由它格式化,並優先於已選擇的 Ruff。" } diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 04f61e9b05..8f9ff48e67 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -3237,7 +3237,7 @@ "Fix Ruff safe lint issues": "修复 Ruff 安全的 lint 问题", "Runs ruff check --fix-only on Python files before composing the commit. This does not format code.": "提交前对 Python 文件运行 ruff check --fix-only。此操作不会格式化代码。", "oxfmt formats an enabled language instead of Prettier below an oxfmt configuration file (.oxfmtrc.json, oxfmt.config.ts) or when it is the selected provider.": "在 oxfmt 配置文件(.oxfmtrc.json、oxfmt.config.ts)所在目录及其子目录中,或选择 oxfmt 作为格式化工具时,oxfmt 将代替 Prettier 格式化已启用的语言。", - "Ruff reads the nearest pyproject.toml ([tool.ruff]), ruff.toml, or .ruff.toml. Use it before the language server when preferred.": "Ruff 会读取最近的 pyproject.toml([tool.ruff])、ruff.toml 或 .ruff.toml。选择优先使用 Ruff 时,将优先于语言服务器。", + "Ruff reads the nearest pyproject.toml ([tool.ruff]), ruff.toml, or .ruff.toml. When selected, Ruff formats Python before the language server, even below a pyproject.toml with [tool.black].": "Ruff 会读取最近的 pyproject.toml([tool.ruff])、ruff.toml 或 .ruff.toml。选择 Ruff 后,它会先于语言服务器格式化 Python,即使所在的 pyproject.toml 含有 [tool.black]。", "Enabled languages": "启用的语言", "Reformat Code needs {name}. Install it via {method}?": "重新格式化代码需要 {name}。是否通过 {method} 安装?", "{refactoring} needs {name}. Install it via {method}?": "{refactoring}需要 {name}。是否通过 {method} 安装?", @@ -10814,7 +10814,7 @@ "Black formatting timed out": "Black 格式化超时", "Black path": "Black 路径", "Auto: the project interpreter's environment, managed installation, then PATH. An explicit path overrides auto detection.": "自动:依次查找项目解释器的环境、托管安装和 PATH。明确指定的路径优先于自动检测。", - "Black formats Python instead of Ruff below a pyproject.toml with [tool.black], or when it is the selected provider.": "在含 [tool.black] 的 pyproject.toml 所在目录及其子目录中,或选择 Black 作为格式化工具时,Black 将代替 Ruff 格式化 Python。", + "Black formats Python instead of Ruff below a pyproject.toml with [tool.black]. When selected, Black formats Python without [tool.black] too, and before a selected Ruff.": "在含 [tool.black] 的 pyproject.toml 所在目录及其子目录中,Black 代替 Ruff 格式化 Python。选择 Black 后,没有 [tool.black] 时也由它格式化,并优先于已选择的 Ruff。", "Run Black on save for enabled languages when it is the selected formatting provider. Reformat code in Actions on Save also runs it.": "对已启用的语言,在选择 Black 作为格式化工具时于保存时运行。保存时操作中的“重新格式化代码”也会运行它。", "Automatic (projects with [tool.black])": "自动(含 [tool.black] 的项目)", "{name} left the file unchanged: its configuration excludes it": "{name} 未修改文件:它的配置排除了此文件",