Fix(vscode): Respect line length ruff.toml configuration (#7306)

This commit is contained in:
Micha Reiser 2023-09-12 17:31:47 +02:00 committed by GitHub
parent ee0f1270cf
commit e561f5783b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View file

@ -73,12 +73,14 @@ pub(crate) fn format(
return None;
};
let preview = match pyproject_config.settings.lib.preview {
let resolved_settings = resolver.resolve(path, &pyproject_config);
let preview = match resolved_settings.preview {
PreviewMode::Enabled => ruff_python_formatter::PreviewMode::Enabled,
PreviewMode::Disabled => ruff_python_formatter::PreviewMode::Disabled,
};
let line_length = resolved_settings.line_length;
let line_length = resolver.resolve(path, &pyproject_config).line_length;
let options = PyFormatOptions::from_source_type(source_type)
.with_line_width(LineWidth::from(NonZeroU16::from(line_length)))
.with_preview(preview);

View file

@ -1,8 +1,11 @@
use std::io::{stdout, Write};
use std::num::NonZeroU16;
use std::path::Path;
use anyhow::Result;
use log::warn;
use ruff::settings::types::PreviewMode;
use ruff_formatter::LineWidth;
use ruff_python_formatter::{format_module, PyFormatOptions};
use ruff_workspace::resolver::python_file_at_path;
@ -35,9 +38,19 @@ pub(crate) fn format_stdin(cli: &FormatArguments, overrides: &Overrides) -> Resu
// Format the file.
let path = cli.stdin_filename.as_deref();
let preview = match pyproject_config.settings.lib.preview {
PreviewMode::Enabled => ruff_python_formatter::PreviewMode::Enabled,
PreviewMode::Disabled => ruff_python_formatter::PreviewMode::Disabled,
};
let line_length = pyproject_config.settings.lib.line_length;
let options = path
.map(PyFormatOptions::from_extension)
.unwrap_or_default();
.unwrap_or_default()
.with_line_width(LineWidth::from(NonZeroU16::from(line_length)))
.with_preview(preview);
match format_source(path, options, mode) {
Ok(result) => match mode {
FormatMode::Write => Ok(ExitStatus::Success),