mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 22:31:23 +00:00
Update dedent_to
to support blocks that are composed of comments (#13572)
While looking into https://github.com/astral-sh/ruff/issues/13545 I noticed that we return `None` here if you pass a block of comments. This is annoying because it causes `adjust_indentation` to fall back to LibCST which panics when it cannot find a statement.
This commit is contained in:
parent
45f01e7872
commit
3af3f74c66
1 changed files with 32 additions and 2 deletions
|
@ -130,6 +130,8 @@ pub fn dedent(text: &str) -> Cow<'_, str> {
|
||||||
/// current indentation, then removes whitespace from each line to
|
/// current indentation, then removes whitespace from each line to
|
||||||
/// match the provided indentation.
|
/// match the provided indentation.
|
||||||
///
|
///
|
||||||
|
/// Leading comments are ignored unless the block is only composed of comments.
|
||||||
|
///
|
||||||
/// Lines that are indented by _less_ than the indent of the first line
|
/// Lines that are indented by _less_ than the indent of the first line
|
||||||
/// are left unchanged.
|
/// are left unchanged.
|
||||||
///
|
///
|
||||||
|
@ -139,17 +141,21 @@ pub fn dedent(text: &str) -> Cow<'_, str> {
|
||||||
/// If the first line is indented by less than the provided indent.
|
/// If the first line is indented by less than the provided indent.
|
||||||
pub fn dedent_to(text: &str, indent: &str) -> Option<String> {
|
pub fn dedent_to(text: &str, indent: &str) -> Option<String> {
|
||||||
// Look at the indentation of the first non-empty line, to determine the "baseline" indentation.
|
// Look at the indentation of the first non-empty line, to determine the "baseline" indentation.
|
||||||
|
let mut first_comment = None;
|
||||||
let existing_indent_len = text
|
let existing_indent_len = text
|
||||||
.universal_newlines()
|
.universal_newlines()
|
||||||
.find_map(|line| {
|
.find_map(|line| {
|
||||||
let trimmed = line.trim_whitespace_start();
|
let trimmed = line.trim_whitespace_start();
|
||||||
if trimmed.is_empty() || trimmed.starts_with('#') {
|
if trimmed.is_empty() {
|
||||||
|
None
|
||||||
|
} else if trimmed.starts_with('#') && first_comment.is_none() {
|
||||||
|
first_comment = Some(line.len() - trimmed.len());
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(line.len() - trimmed.len())
|
Some(line.len() - trimmed.len())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.unwrap_or_default();
|
.unwrap_or(first_comment.unwrap_or_default());
|
||||||
|
|
||||||
if existing_indent_len < indent.len() {
|
if existing_indent_len < indent.len() {
|
||||||
return None;
|
return None;
|
||||||
|
@ -431,5 +437,29 @@ mod tests {
|
||||||
"baz"
|
"baz"
|
||||||
].join("\n");
|
].join("\n");
|
||||||
assert_eq!(dedent_to(&x, ""), Some(y));
|
assert_eq!(dedent_to(&x, ""), Some(y));
|
||||||
|
|
||||||
|
let x = [
|
||||||
|
" # foo",
|
||||||
|
" # bar",
|
||||||
|
"# baz"
|
||||||
|
].join("\n");
|
||||||
|
let y = [
|
||||||
|
" # foo",
|
||||||
|
" # bar",
|
||||||
|
"# baz"
|
||||||
|
].join("\n");
|
||||||
|
assert_eq!(dedent_to(&x, " "), Some(y));
|
||||||
|
|
||||||
|
let x = [
|
||||||
|
" # foo",
|
||||||
|
" bar",
|
||||||
|
" baz"
|
||||||
|
].join("\n");
|
||||||
|
let y = [
|
||||||
|
" # foo",
|
||||||
|
" bar",
|
||||||
|
" baz"
|
||||||
|
].join("\n");
|
||||||
|
assert_eq!(dedent_to(&x, " "), Some(y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue