Extract LineIndex
independent methods from Locator
#13938
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
The
Locator
provides useful methods to extract a line's range or text given an offsetor the full range or text of all lines falling into a text range.
These methods are foundational and used across many crates.
However,
Locator
also exposes methods that require computing and memoizing aLineIndex.
The formatter doesn't use any of those methods and "cheats" by always creating an ad-hoc
Locator
instead of storing an instance on
PyFormatContext
. This leaks theLocator
abstractionbecause it depends on the knowledge of which
Locator
methods require the extraLineIndex
state.This PR splits the "stateless" methods from
Locator
into a new trait and implements that traitfor
str
(string slices). It also replaces allLocator
usages with&str
in all crates other thanruff_linter
.It also moves
Locator
toruff_linter
to clarify that this is linter-specific infrastructure.I decided to keep
Locator
forruff_linter
because:Locator
communicates that this is the source of the entire fileLineIndex
dependentLocator
methods where the lazy index computation is useful.We could consider changing more
Linter
methods to take&str
instead ofLocator
but we can do this in separate PRs.Renamed methods
I had to rename
lines
tolines_str
str.lines
conflicts with the stdlines
builtin (returns an iterator over the lines). I decided to add the_str
postfix to all other methods that return astr
for consistency.Test Plan
cargo test