mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-30 15:17:40 +00:00
Render code frame with context (#3901)
This commit is contained in:
parent
381203c084
commit
056c212975
15 changed files with 130 additions and 70 deletions
|
@ -100,6 +100,20 @@ impl LineIndex {
|
|||
}
|
||||
}
|
||||
|
||||
/// Returns the [byte offset](TextSize) of the `line`'s end.
|
||||
/// The offset is the end of the line, up to and including the newline character ending the line (if any).
|
||||
pub(crate) fn line_end(&self, line: OneIndexed, contents: &str) -> TextSize {
|
||||
let row_index = line.to_zero_indexed();
|
||||
let starts = self.line_starts();
|
||||
|
||||
// If start-of-line position after last line
|
||||
if row_index.saturating_add(1) >= starts.len() {
|
||||
contents.text_len()
|
||||
} else {
|
||||
starts[row_index + 1]
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the [`TextRange`] of the `line` with the given index.
|
||||
/// The start points to the first character's [byte offset](TextSize), the end up to, and including
|
||||
/// the newline character ending the line (if any).
|
||||
|
|
|
@ -78,6 +78,10 @@ impl<'src, 'index> SourceCode<'src, 'index> {
|
|||
self.index.line_start(line, self.text)
|
||||
}
|
||||
|
||||
pub fn line_end(&self, line: OneIndexed) -> TextSize {
|
||||
self.index.line_end(line, self.text)
|
||||
}
|
||||
|
||||
pub fn line_range(&self, line: OneIndexed) -> TextRange {
|
||||
self.index.line_range(line, self.text)
|
||||
}
|
||||
|
@ -198,6 +202,11 @@ impl SourceCodeBuf {
|
|||
self.as_source_code().offset(location)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn line_end(&self, line: OneIndexed) -> TextSize {
|
||||
self.as_source_code().line_end(line)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn line_start(&self, line: OneIndexed) -> TextSize {
|
||||
self.as_source_code().line_start(line)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue