Skip to content

Commit

Permalink
ruff_python_formatter: add support for docstring code "dynamic" mode
Browse files Browse the repository at this point in the history
This computes the line width to use on the nested call to the
formatter based on the current indent level, indent width and
configured global setting.

Ref #8855
  • Loading branch information
BurntSushi committed Dec 12, 2023
1 parent e94e7ae commit 6195d47
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions crates/ruff_python_formatter/src/expression/string/docstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use std::{borrow::Cow, collections::VecDeque};
use {once_cell::sync::Lazy, regex::Regex};

use {
ruff_formatter::{write, IndentStyle, Printed},
ruff_formatter::{write, FormatOptions, IndentStyle, LineWidth, Printed},
ruff_python_trivia::{is_python_whitespace, PythonWhitespace},
ruff_source_file::Locator,
ruff_text_size::{Ranged, TextLen, TextRange, TextSize},
};

use crate::{prelude::*, FormatModuleError};
use crate::{prelude::*, DocstringCodeLineWidth, FormatModuleError};

use super::{NormalizedString, QuoteChar};

Expand Down Expand Up @@ -460,11 +460,22 @@ impl<'ast, 'buf, 'fmt, 'src> DocstringLinePrinter<'ast, 'buf, 'fmt, 'src> {
.map(|line| line.code)
.collect::<Vec<&str>>()
.join("\n");
let line_width = match self.f.options().docstring_code_line_width() {
DocstringCodeLineWidth::Fixed(width) => width,
DocstringCodeLineWidth::Dynamic => {
let global_line_width = self.f.options().line_width().value();
let indent_width = self.f.options().indent_width();
let indent_level = self.f.context().indent_level();
let current_indent = indent_level.to_ascii_spaces(indent_width);
let width = std::cmp::max(1, global_line_width.saturating_sub(current_indent));
LineWidth::try_from(width).expect("width is capped at a minimum of 1")
}
};
let options = self
.f
.options()
.clone()
.with_line_width(self.f.options().docstring_code_line_width())
.with_line_width(line_width)
// It's perhaps a little odd to be hard-coding the indent
// style here, but I believe it is necessary as a result
// of the whitespace normalization otherwise done in
Expand Down

0 comments on commit 6195d47

Please sign in to comment.