Skip to content

Commit

Permalink
more optimal expand_indent
Browse files Browse the repository at this point in the history
  • Loading branch information
augustelalande committed May 16, 2024
1 parent 50ba417 commit 9ec463a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
17 changes: 17 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pycodestyle/E122.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,20 @@ def target(
[21,
21,
21]


# OK
def _is_temporal_supported(self, opname, pa_dtype):
return (
(
opname in ("__add__", "__radd__")
or (
opname
in ("__truediv__", "__rtruediv__", "__floordiv__", "__rfloordiv__")
and not pa_version_under14p0
)
)
and pa.types.is_duration(pa_dtype)
or opname in ("__sub__", "__rsub__")
and pa.types.is_temporal(pa_dtype)
)
2 changes: 0 additions & 2 deletions crates/ruff_linter/src/checkers/logical_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ use crate::settings::LinterSettings;

/// Return the amount of indentation, expanding tabs to the next multiple of the settings' tab size.
pub(crate) fn expand_indent(line: &str, indent_width: IndentWidth) -> usize {
let line = line.trim_end_matches(['\n', '\r']);

let mut indent = 0;
let tab_size = indent_width.as_usize();
for c in line.bytes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,12 @@ fn continuation_line_end(
/// Return the amount of indentation of the given line.
/// Tabs are expanded to the next multiple of 8.
fn expand_indent(line: &str) -> i64 {
if !line.contains('\t') {
// If there are no tabs in the line, return the leading space count
return i64::try_from(line.len() - line.trim_start().len())
.expect("Line length to be relatively small.");
}
let mut indent = 0;

for ch in line.chars() {
if ch == '\t' {
indent = indent / 8 * 8 + 8;
} else if ch == ' ' {
indent += 1;
} else {
break;
for c in line.bytes() {
match c {
b'\t' => indent = (indent / 8) * 8 + 8,
b' ' => indent += 1,
_ => break,
}
}

Expand Down

0 comments on commit 9ec463a

Please sign in to comment.