Add --range option to ruff format (#9733)

Co-authored-by: T-256 <132141463+T-256@users.noreply.github.com>
This commit is contained in:
Micha Reiser 2024-02-05 20:21:45 +01:00 committed by GitHub
parent e708c08b64
commit b3dc565473
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 652 additions and 20 deletions

View file

@ -215,6 +215,34 @@ impl LineIndex {
}
}
/// Returns the [byte offset](TextSize) at `line` and `column`.
pub fn offset(&self, line: OneIndexed, column: OneIndexed, contents: &str) -> TextSize {
// If start-of-line position after last line
if line.to_zero_indexed() > self.line_starts().len() {
return contents.text_len();
}
let line_range = self.line_range(line, contents);
match self.kind() {
IndexKind::Ascii => {
line_range.start()
+ TextSize::try_from(column.get())
.unwrap_or(line_range.len())
.clamp(TextSize::new(0), line_range.len())
}
IndexKind::Utf8 => {
let rest = &contents[line_range];
let column_offset: TextSize = rest
.chars()
.take(column.get())
.map(ruff_text_size::TextLen::text_len)
.sum();
line_range.start() + column_offset
}
}
}
/// Returns the [byte offsets](TextSize) for every line
pub fn line_starts(&self) -> &[TextSize] {
&self.inner.line_starts