-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix indentation error when trailing quotes are not on separate line (… #13372
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,6 +5,7 @@ use ruff_python_ast::docstrings::{clean_space, leading_space}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use ruff_source_file::NewlineWithTrailingNewline; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use ruff_text_size::{Ranged, TextSize}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use ruff_text_size::{TextLen, TextRange}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use std::cmp::Ordering; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use crate::checkers::ast::Checker; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use crate::docstrings::Docstring; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -291,10 +292,28 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let line_indent = leading_space(last); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let line_indent_size = line_indent.chars().count(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if line_indent_size > docstring_indent_size { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let mut offset_len = line_indent_size; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// The trailing quotes are not on a separate line. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if line_indent.text_len() != last.text_len() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
over_indented_size = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::cmp::min(line_indent_size - docstring_indent_size, over_indented_size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match over_indented_size.cmp(&docstring_indent_size) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ordering::Equal => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+301
to
+303
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a comment here that explains when the indent sizes are equal and why we return in that case? I'm having a hard time understanding what's happening, and a comment might also be useful for future readers. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ordering::Less => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
offset_len = over_indented_size; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ordering::Greater => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
offset_len -= over_indented_size - docstring_indent_size; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+295
to
+311
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: We can avoid mutating the variable by using let
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let mut diagnostic = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Diagnostic::new(OverIndentation, TextRange::empty(last.start())); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let indent = clean_space(docstring.indentation); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let range = TextRange::at(last.start(), line_indent.text_len()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let range = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TextRange::at(last.start(), TextSize::new(offset_len.try_into().unwrap())); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of We'll need to do something similar to ruff/crates/ruff_linter/src/rules/pydocstyle/rules/indent.rs Lines 275 to 281 in 14dcae1
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let edit = if indent.is_empty() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Edit::range_deletion(range) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's avoid overriding the outer variable and instead shadow it