dprint_swc_ext/common/
text_info.rs

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use std::sync::Arc;

use swc_common::input::StringInput;
pub use text_lines::LineAndColumnIndex;
pub use text_lines::TextLines;

use super::pos::*;
use super::text_encoding::strip_bom_mut;
use super::text_encoding::BOM_CHAR;
use super::LineAndColumnDisplay;

/// Stores the source text along with other data such as where all the lines
/// occur in the text.
///
/// Note: This struct is cheap to clone.
#[derive(Clone)]
pub struct SourceTextInfo {
  // keep this struct cheap to clone
  start_pos: StartSourcePos,
  text: Arc<str>,
  text_lines: Arc<TextLines>,
}

impl SourceTextInfo {
  /// Creates a new `SourceTextInfo` from the provided source text.
  pub fn new(text: Arc<str>) -> Self {
    Self::new_with_pos(StartSourcePos::START_SOURCE_POS.as_source_pos(), text)
  }

  /// Creates a new `SourceTextInfo` from the provided source start position
  /// and source text.
  ///
  /// Note: When bundling swc will keep increasing the start position for
  /// each source file.
  pub fn new_with_pos(start_pos: SourcePos, text: Arc<str>) -> Self {
    // The BOM should be stripped before it gets passed here
    // because it's a text encoding concern that should be
    // stripped when the file is read.
    // todo(dsherret): re-enable after removing the below
    // assert!(!text.starts_with(BOM_CHAR), "BOM should be stripped before creating a SourceTextInfo.");

    // todo(dsherret): remove this once handled downstream
    let text = if text.starts_with(BOM_CHAR) {
      let mut text = text.to_string();
      strip_bom_mut(&mut text);
      text.into()
    } else {
      text
    };

    Self::new_with_indent_width(start_pos, text, 2)
  }

  /// Creates a new `SourceTextInfo` from the provided start position,
  /// source text, and indentation width.
  ///
  /// The indentation width determines the number of columns to use
  /// when going over a tab character. For example, an indent width
  /// of 2 will mean each tab character will represent 2 columns.
  /// The default indentation width used in the other methods is `2`
  /// to match the default indentation used by `deno fmt`.
  pub fn new_with_indent_width(start_pos: SourcePos, text: Arc<str>, indent_width: usize) -> Self {
    Self {
      start_pos: StartSourcePos(start_pos),
      text_lines: Arc::new(TextLines::with_indent_width(&text, indent_width)),
      text,
    }
  }

  /// Creates a new `SourceTextInfo` from the provided source text.
  ///
  /// Generally, prefer using `SourceTextInfo::new` to provide a
  /// string already in an `std::sync::Arc`.
  pub fn from_string(text: String) -> Self {
    Self::new(text.into())
  }

  /// Gets an swc `StringInput` for this text information that can be
  /// used with parsing.
  pub fn as_string_input(&self) -> StringInput {
    let range = self.range();
    StringInput::new(self.text_str(), range.start.as_byte_pos(), range.end.as_byte_pos())
  }

  /// Gets the source text.
  pub fn text(&self) -> Arc<str> {
    self.text.clone()
  }

  /// Gets a reference to the source text.
  pub fn text_str(&self) -> &str {
    &self.text
  }

  /// Gets the range—start and end byte position—of the source text.
  pub fn range(&self) -> SourceRange<StartSourcePos> {
    SourceRange::new(self.start_pos, self.start_pos + self.text.len())
  }

  /// Gets the number of lines in the source text.
  pub fn lines_count(&self) -> usize {
    self.text_lines.lines_count()
  }

  /// Gets the 0-indexed line index at the provided byte position.
  ///
  /// Note that this will panic when providing a byte position outside
  /// the range of the source text.
  pub fn line_index(&self, pos: SourcePos) -> usize {
    self.assert_pos(pos);
    self.text_lines.line_index(self.get_relative_index_from_pos(pos))
  }

  /// Gets the line start byte position of the provided 0-indexed line index.
  ///
  /// Note that this will panic if providing a line index outside the
  /// bounds of the number of lines.
  pub fn line_start(&self, line_index: usize) -> SourcePos {
    self.assert_line_index(line_index);
    self.get_pos_from_relative_index(self.text_lines.line_start(line_index))
  }

  /// Gets the line end byte position of the provided 0-indexed line index.
  ///
  /// Note that this will panic if providing a line index outside the
  /// bounds of the number of lines.
  pub fn line_end(&self, line_index: usize) -> SourcePos {
    self.assert_line_index(line_index);
    self.get_pos_from_relative_index(self.text_lines.line_end(line_index))
  }

  /// Gets the 0-indexed line and column index of the provided byte position.
  ///
  /// Note that this will panic when providing a byte position outside
  /// the range of the source text.
  pub fn line_and_column_index(&self, pos: SourcePos) -> LineAndColumnIndex {
    self.assert_pos(pos);
    self.text_lines.line_and_column_index(self.get_relative_index_from_pos(pos))
  }

  /// Gets the 1-indexed line and column index of the provided byte position
  /// taking into account the default indentation width.
  ///
  /// Note that this will panic when providing a byte position outside
  /// the range of the source text.
  pub fn line_and_column_display(&self, pos: SourcePos) -> LineAndColumnDisplay {
    self.assert_pos(pos);
    self.text_lines.line_and_column_display(self.get_relative_index_from_pos(pos))
  }

  /// Gets the 1-indexed line and column index of the provided byte position
  /// with a custom indentation width.
  ///
  /// Note that this will panic when providing a byte position outside
  /// the range of the source text.
  pub fn line_and_column_display_with_indent_width(&self, pos: SourcePos, indent_width: usize) -> LineAndColumnDisplay {
    self.assert_pos(pos);
    self
      .text_lines
      .line_and_column_display_with_indent_width(self.get_relative_index_from_pos(pos), indent_width)
  }

  /// Gets the source position of the provided line and column index.
  ///
  /// Note that this will panic if providing a line index outside the
  /// bounds of the number of lines, but will clip the the line end byte index
  /// when exceeding the line length.
  pub fn loc_to_source_pos(&self, line_and_column_index: LineAndColumnIndex) -> SourcePos {
    self.assert_line_index(line_and_column_index.line_index);
    self.get_pos_from_relative_index(self.text_lines.byte_index(line_and_column_index))
  }

  /// Gets a reference to the text slice of the line at the provided
  /// 0-based index.
  ///
  /// Note that this will panic if providing a line index outside the
  /// bounds of the number of lines.
  pub fn line_text(&self, line_index: usize) -> &str {
    let range = SourceRange {
      start: self.line_start(line_index),
      end: self.line_end(line_index),
    };
    self.range_text(&range)
  }

  /// Gets the source text located within the provided range.
  pub fn range_text(&self, range: &SourceRange) -> &str {
    let start = self.get_relative_index_from_pos(range.start);
    let end = self.get_relative_index_from_pos(range.end);
    &self.text_str()[start..end]
  }

  fn assert_pos(&self, pos: SourcePos) {
    let range = self.range();
    if pos < range.start {
      panic!("The provided position {} was less than the start position {}.", pos, range.start,);
    } else if pos > range.end {
      panic!("The provided position {} was greater than the end position {}.", pos, range.end,);
    }
  }

  fn assert_line_index(&self, line_index: usize) {
    if line_index >= self.lines_count() {
      panic!(
        "The specified line index {} was greater or equal to the number of lines of {}.",
        line_index,
        self.lines_count()
      );
    }
  }

  fn get_relative_index_from_pos(&self, pos: SourcePos) -> usize {
    pos - self.start_pos
  }

  fn get_pos_from_relative_index(&self, relative_index: usize) -> SourcePos {
    self.start_pos + relative_index
  }
}

impl std::fmt::Debug for SourceTextInfo {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("SourceTextInfo")
      .field("start_pos", &self.start_pos)
      .field("text", &self.text)
      .finish()
  }
}

pub trait SourceTextInfoProvider<'a> {
  fn text_info(&self) -> &'a SourceTextInfo;
}

impl<'a> SourceTextInfoProvider<'a> for &'a SourceTextInfo {
  fn text_info(&self) -> &'a SourceTextInfo {
    self
  }
}

pub trait SourceTextProvider<'a> {
  fn text(&self) -> &'a Arc<str>;
  fn start_pos(&self) -> StartSourcePos;
}

impl<'a, T> SourceTextProvider<'a> for T
where
  T: SourceTextInfoProvider<'a>,
{
  fn text(&self) -> &'a Arc<str> {
    &self.text_info().text
  }

  fn start_pos(&self) -> StartSourcePos {
    self.text_info().start_pos
  }
}

#[cfg(test)]
mod test {
  use super::*;

  #[test]
  fn line_and_column_index() {
    let text = "12\n3\r\nβ\n5";
    for i in 0..10 {
      run_with_text_info(SourceTextInfo::new_with_pos(SourcePos::new(i), text.to_string().into()), i);
    }

    fn run_with_text_info(text_info: SourceTextInfo, i: usize) {
      assert_pos_line_and_col(&text_info, i, 0, 0); // 1
      assert_pos_line_and_col(&text_info, 1 + i, 0, 1); // 2
      assert_pos_line_and_col(&text_info, 2 + i, 0, 2); // \n
      assert_pos_line_and_col(&text_info, 3 + i, 1, 0); // 3
      assert_pos_line_and_col(&text_info, 4 + i, 1, 1); // \r
      assert_pos_line_and_col(&text_info, 5 + i, 1, 2); // \n
      assert_pos_line_and_col(&text_info, 6 + i, 2, 0); // first β index
      assert_pos_line_and_col(&text_info, 7 + i, 2, 0); // second β index
      assert_pos_line_and_col(&text_info, 8 + i, 2, 1); // \n
      assert_pos_line_and_col(&text_info, 9 + i, 3, 0); // 5
      assert_pos_line_and_col(&text_info, 10 + i, 3, 1); // <EOF>
    }
  }

  fn assert_pos_line_and_col(text_info: &SourceTextInfo, pos: usize, line_index: usize, column_index: usize) {
    assert_eq!(
      text_info.line_and_column_index(SourcePos::new(pos)),
      LineAndColumnIndex { line_index, column_index }
    );
  }

  #[test]
  #[should_panic(expected = "The provided position 0 was less than the start position 1.")]
  fn line_and_column_index_panic_less_than() {
    let info = SourceTextInfo::new_with_pos(SourcePos::new(1), "test".to_string().into());
    info.line_and_column_index(SourcePos::new(0));
  }

  #[test]
  #[should_panic(expected = "The provided position 6 was greater than the end position 5.")]
  fn line_and_column_index_panic_greater_than() {
    let info = SourceTextInfo::new_with_pos(SourcePos::new(1), "test".to_string().into());
    info.line_and_column_index(SourcePos::new(6));
  }

  #[test]
  fn line_start() {
    let text = "12\n3\r\n4\n5";
    for i in 0..10 {
      run_with_text_info(SourceTextInfo::new_with_pos(SourcePos::new(i), text.to_string().into()), i);
    }

    fn run_with_text_info(text_info: SourceTextInfo, i: usize) {
      assert_line_start(&text_info, 0, i);
      assert_line_start(&text_info, 1, 3 + i);
      assert_line_start(&text_info, 2, 6 + i);
      assert_line_start(&text_info, 3, 8 + i);
    }
  }

  fn assert_line_start(text_info: &SourceTextInfo, line_index: usize, line_end: usize) {
    assert_eq!(text_info.line_start(line_index), SourcePos::new(line_end));
  }

  #[test]
  #[should_panic(expected = "The specified line index 1 was greater or equal to the number of lines of 1.")]
  fn line_start_equal_number_lines() {
    let info = SourceTextInfo::new_with_pos(SourcePos::new(1), "test".to_string().into());
    info.line_start(1);
  }

  #[test]
  fn line_end() {
    let text = "12\n3\r\n4\n5";
    for i in 0..10 {
      run_with_text_info(SourceTextInfo::new_with_pos(SourcePos::new(i), text.to_string().into()), i);
    }

    fn run_with_text_info(text_info: SourceTextInfo, i: usize) {
      assert_line_end(&text_info, 0, 2 + i);
      assert_line_end(&text_info, 1, 4 + i);
      assert_line_end(&text_info, 2, 7 + i);
      assert_line_end(&text_info, 3, 9 + i);
    }
  }

  fn assert_line_end(text_info: &SourceTextInfo, line_index: usize, line_end: usize) {
    assert_eq!(text_info.line_end(line_index), SourcePos::new(line_end));
  }

  #[test]
  #[should_panic(expected = "The specified line index 1 was greater or equal to the number of lines of 1.")]
  fn line_end_equal_number_lines() {
    let info = SourceTextInfo::new_with_pos(SourcePos::new(1), "test".to_string().into());
    info.line_end(1);
  }
}