1 // Copyright (c) 2019, Facebook, Inc.
2 // All rights reserved.
4 // This source code is licensed under the MIT license found in the
5 // LICENSE file in the "hack" directory of this source tree.
7 use crate::source_text::SourceText;
8 use line_break_map::LineBreakMap;
9 use oxidized::pos::Pos;
12 pub struct IndexedSourceText<'a> {
13 pub source_text: &'a SourceText<'a>,
14 offset_map: LineBreakMap,
17 impl<'a> IndexedSourceText<'a> {
18 pub fn new(source_text: &'a SourceText<'a>) -> Self {
21 offset_map: LineBreakMap::new(source_text.text()),
25 pub fn source_text(&self) -> &SourceText {
29 pub fn offset_to_position(&self, offset: isize) -> (isize, isize) {
30 self.offset_map.offset_to_position(offset)
33 pub fn relative_pos(&self, start_offset: usize, end_offset: usize) -> Pos {
34 let pos_start = self.offset_map.offset_to_file_pos_triple(start_offset);
35 let pos_end = self.offset_map.offset_to_file_pos_triple(end_offset);
36 Pos::from_lnum_bol_cnum(self.source_text.file_path().clone(), pos_start, pos_end)