mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 21:35:58 +00:00
Use locator.slice(range)
over locator.contents()[range]
(#7759)
**Summary** Refactoring inspired by https://github.com/astral-sh/ruff/pull/7741#discussion_r1342168033
This commit is contained in:
parent
f70e8a7524
commit
13748dd27c
4 changed files with 30 additions and 6 deletions
|
@ -230,7 +230,13 @@ fn delete_noqa(range: TextRange, locator: &Locator) -> Edit {
|
|||
Edit::deletion(range.start() - leading_space_len, line_range.end())
|
||||
}
|
||||
// Ex) `x = 1 # noqa # type: ignore`
|
||||
else if locator.contents()[usize::from(range.end() + trailing_space_len)..].starts_with('#') {
|
||||
else if locator
|
||||
.slice(TextRange::new(
|
||||
range.end() + trailing_space_len,
|
||||
line_range.end(),
|
||||
))
|
||||
.starts_with('#')
|
||||
{
|
||||
Edit::deletion(range.start(), range.end() + trailing_space_len)
|
||||
}
|
||||
// Ex) `x = 1 # noqa here`
|
||||
|
|
|
@ -661,7 +661,11 @@ pub(crate) fn use_ternary_operator(checker: &mut Checker, stmt: &Stmt) {
|
|||
// Don't flag if the resulting expression would exceed the maximum line length.
|
||||
let line_start = checker.locator().line_start(stmt.start());
|
||||
if LineWidthBuilder::new(checker.settings.tab_size)
|
||||
.add_str(&checker.locator().contents()[TextRange::new(line_start, stmt.start())])
|
||||
.add_str(
|
||||
checker
|
||||
.locator()
|
||||
.slice(TextRange::new(line_start, stmt.start())),
|
||||
)
|
||||
.add_str(&contents)
|
||||
> checker.settings.line_length
|
||||
{
|
||||
|
@ -965,7 +969,11 @@ pub(crate) fn use_dict_get_with_default(checker: &mut Checker, stmt_if: &ast::St
|
|||
// Don't flag if the resulting expression would exceed the maximum line length.
|
||||
let line_start = checker.locator().line_start(stmt_if.start());
|
||||
if LineWidthBuilder::new(checker.settings.tab_size)
|
||||
.add_str(&checker.locator().contents()[TextRange::new(line_start, stmt_if.start())])
|
||||
.add_str(
|
||||
checker
|
||||
.locator()
|
||||
.slice(TextRange::new(line_start, stmt_if.start())),
|
||||
)
|
||||
.add_str(&contents)
|
||||
> checker.settings.line_length
|
||||
{
|
||||
|
|
|
@ -99,7 +99,11 @@ pub(crate) fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt) {
|
|||
// Don't flag if the resulting expression would exceed the maximum line length.
|
||||
let line_start = checker.locator().line_start(stmt.start());
|
||||
if LineWidthBuilder::new(checker.settings.tab_size)
|
||||
.add_str(&checker.locator().contents()[TextRange::new(line_start, stmt.start())])
|
||||
.add_str(
|
||||
checker
|
||||
.locator()
|
||||
.slice(TextRange::new(line_start, stmt.start())),
|
||||
)
|
||||
.add_str(&contents)
|
||||
> checker.settings.line_length
|
||||
{
|
||||
|
@ -181,7 +185,11 @@ pub(crate) fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt) {
|
|||
// Don't flag if the resulting expression would exceed the maximum line length.
|
||||
let line_start = checker.locator().line_start(stmt.start());
|
||||
if LineWidthBuilder::new(checker.settings.tab_size)
|
||||
.add_str(&checker.locator().contents()[TextRange::new(line_start, stmt.start())])
|
||||
.add_str(
|
||||
checker
|
||||
.locator()
|
||||
.slice(TextRange::new(line_start, stmt.start())),
|
||||
)
|
||||
.add_str(&contents)
|
||||
> checker.settings.line_length
|
||||
{
|
||||
|
|
|
@ -298,7 +298,9 @@ fn fix_always_false_branch(
|
|||
..
|
||||
}) => {
|
||||
debug_assert!(
|
||||
&checker.locator().contents()[TextRange::at(range.start(), "elif".text_len())]
|
||||
checker
|
||||
.locator()
|
||||
.slice(TextRange::at(range.start(), "elif".text_len()))
|
||||
== "elif"
|
||||
);
|
||||
let end_location = range.start() + ("elif".text_len() - "if".text_len());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue