makepad_live_compiler/
span.rs

1use{
2    std::fmt,
3    crate::{
4   //     makepad_live_tokenizer::Position,
5        live_token::LiveTokenId,
6        live_ptr::LiveFileId
7    }
8};
9
10#[derive(Clone, Copy, Default, Eq, Ord, PartialOrd, PartialEq)]
11pub struct TextPos {
12    pub line: u32,
13    pub column: u32
14}
15
16impl TextPos{
17    pub fn to_byte_offset(&self, s:&str)->Option<usize>{
18        let mut line = 0;
19        let mut col = 0;
20        for (byte_index, c) in s.char_indices(){
21            if line == self.line as usize && col == self.column as usize{
22                return Some(byte_index)
23            }
24            col += 1;
25            if c == '\n'{
26                line += 1;
27                col = 0;
28            }
29        }
30        None
31    }
32}
33/*
34impl From<TextPos> for Position {
35    fn from(text_pos: TextPos) -> Position {
36        Position{
37            line:text_pos.line as usize,
38            column:text_pos.column as usize
39        }
40    }*/
41
42#[derive(Clone, Copy, Default, Eq, Ord, PartialOrd, PartialEq)]
43pub struct TextSpan {
44    pub file_id: LiveFileId,
45    pub start: TextPos,
46    pub end: TextPos
47}
48
49#[derive(Clone, Copy, Default, Debug,  Eq, Ord, PartialOrd, PartialEq)]
50pub struct TokenSpan {
51    pub token_id: LiveTokenId,
52    pub len: usize
53}
54
55impl From<LiveTokenId> for TokenSpan {
56    fn from(val: LiveTokenId) -> Self {
57        TokenSpan { token_id: val, len: 1 }
58    }
59}
60
61impl fmt::Display for TextSpan {
62    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63        write!(f, "Span(start:{}, end:{}, file_id:{})", self.start, self.end, self.file_id.to_index())
64    }
65}
66
67impl fmt::Debug for TextSpan {
68    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69        write!(f, "Span(start:{}, end:{}, file_id:{})", self.start, self.end, self.file_id.to_index())
70    }
71}
72
73impl fmt::Display for TextPos {
74    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75        write!(f, "{}:{}", self.line, self.column)
76    }
77}
78
79impl fmt::Debug for TextPos {
80    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81        write!(f, "{}:{}", self.line, self.column)
82    }
83}
84