forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
42 lines (35 loc) · 1.09 KB
/
lib.rs
File metadata and controls
42 lines (35 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
pub use ruff_source_file::{LineIndex, OneIndexed as LineNumber, SourceLocation};
use ruff_text_size::TextRange;
pub use ruff_text_size::TextSize;
#[derive(Clone)]
pub struct SourceCode<'src> {
pub path: &'src str,
pub text: &'src str,
pub index: LineIndex,
}
impl<'src> SourceCode<'src> {
pub fn new(path: &'src str, text: &'src str) -> Self {
let index = LineIndex::from_source_text(text);
Self { path, text, index }
}
pub fn line_index(&self, offset: TextSize) -> LineNumber {
self.index.line_index(offset)
}
pub fn source_location(&self, offset: TextSize) -> SourceLocation {
self.index.source_location(offset, self.text)
}
pub fn get_range(&'src self, range: TextRange) -> &'src str {
&self.text[range.start().to_usize()..range.end().to_usize()]
}
}
pub struct SourceCodeOwned {
pub path: String,
pub text: String,
pub index: LineIndex,
}
impl SourceCodeOwned {
pub fn new(path: String, text: String) -> Self {
let index = LineIndex::from_source_text(&text);
Self { path, text, index }
}
}