summaryrefslogtreecommitdiff
path: root/src/source.rs
blob: abd3c2460fe71c1028b327f4ca6ec186c3144cc5 (plain) (blame)
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
//! Source files.

use std::collections::{hash_map::Entry, HashMap};

use crate::loading::FileId;
use crate::parse::{is_newline, Scanner};
use crate::syntax::{Pos, Span};

/// A store for loaded source files.
#[derive(Default)]
pub struct SourceMap {
    sources: HashMap<FileId, SourceFile>,
}

impl SourceMap {
    /// Create a new, empty source map
    pub fn new() -> Self {
        Self::default()
    }

    /// Get a source file by id.
    pub fn get(&self, file: FileId) -> Option<&SourceFile> {
        self.sources.get(&file)
    }

    /// Insert a sources.
    pub fn insert(&mut self, source: SourceFile) -> &SourceFile {
        match self.sources.entry(source.file) {
            Entry::Occupied(mut entry) => {
                entry.insert(source);
                entry.into_mut()
            }
            Entry::Vacant(entry) => entry.insert(source),
        }
    }

    /// Remove all sources.
    pub fn clear(&mut self) {
        self.sources.clear();
    }
}

/// A single source file.
pub struct SourceFile {
    file: FileId,
    src: String,
    line_starts: Vec<Pos>,
}

impl SourceFile {
    /// Create a new source file from  string.
    pub fn new(file: FileId, src: String) -> Self {
        let mut line_starts = vec![Pos::ZERO];
        let mut s = Scanner::new(&src);

        while let Some(c) = s.eat() {
            if is_newline(c) {
                if c == '\r' {
                    s.eat_if('\n');
                }
                line_starts.push(s.index().into());
            }
        }

        Self { file, src, line_starts }
    }

    /// The file id.
    pub fn file(&self) -> FileId {
        self.file
    }

    /// The whole source as a string slice.
    pub fn src(&self) -> &str {
        &self.src
    }

    /// Get the length of the file in bytes.
    pub fn len_bytes(&self) -> usize {
        self.src.len()
    }

    /// Get the length of the file in lines.
    pub fn len_lines(&self) -> usize {
        self.line_starts.len()
    }

    /// Slice out the part of the source code enclosed by the span.
    pub fn get(&self, span: Span) -> Option<&str> {
        self.src.get(span.to_range())
    }

    /// Return the index of the line that contains the given byte position.
    pub fn pos_to_line(&self, byte_pos: Pos) -> Option<usize> {
        (byte_pos.to_usize() <= self.src.len()).then(|| {
            match self.line_starts.binary_search(&byte_pos) {
                Ok(i) => i,
                Err(i) => i - 1,
            }
        })
    }

    /// Return the column of the byte index.
    ///
    /// Tabs are counted as occupying two columns.
    pub fn pos_to_column(&self, byte_pos: Pos) -> Option<usize> {
        let line = self.pos_to_line(byte_pos)?;
        let start = self.line_to_pos(line)?;
        let head = self.get(Span::new(start, byte_pos))?;
        Some(head.chars().map(width).sum())
    }

    /// Return the byte position at which the given line starts.
    pub fn line_to_pos(&self, line_idx: usize) -> Option<Pos> {
        self.line_starts.get(line_idx).copied()
    }

    /// Return the span which encloses the given line.
    pub fn line_to_span(&self, line_idx: usize) -> Option<Span> {
        let start = self.line_to_pos(line_idx)?;
        let end = self.line_to_pos(line_idx + 1).unwrap_or(self.src.len().into());
        Some(Span::new(start, end))
    }

    /// Return the byte position of the given (line, column) pair.
    ///
    /// Tabs are counted as occupying two columns.
    pub fn line_column_to_pos(&self, line_idx: usize, column_idx: usize) -> Option<Pos> {
        let span = self.line_to_span(line_idx)?;
        let line = self.get(span)?;

        if column_idx == 0 {
            return Some(span.start);
        }

        let mut column = 0;
        for (i, c) in line.char_indices() {
            column += width(c);
            if column >= column_idx {
                return Some(span.start + Pos::from(i + c.len_utf8()));
            }
        }

        None
    }
}

/// The display width of the character.
fn width(c: char) -> usize {
    if c == '\t' { 2 } else { 1 }
}

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

    const ID: FileId = FileId::from_raw(0);
    const TEST: &str = "äbcde\nf💛g\r\nhi\rjkl";

    #[test]
    fn test_source_file_new() {
        let source = SourceFile::new(ID, TEST.into());
        assert_eq!(source.line_starts, vec![Pos(0), Pos(7), Pos(15), Pos(18)]);
    }

    #[test]
    fn test_source_file_pos_to_line() {
        let source = SourceFile::new(ID, TEST.into());
        assert_eq!(source.pos_to_line(Pos(0)), Some(0));
        assert_eq!(source.pos_to_line(Pos(2)), Some(0));
        assert_eq!(source.pos_to_line(Pos(6)), Some(0));
        assert_eq!(source.pos_to_line(Pos(7)), Some(1));
        assert_eq!(source.pos_to_line(Pos(8)), Some(1));
        assert_eq!(source.pos_to_line(Pos(12)), Some(1));
        assert_eq!(source.pos_to_line(Pos(21)), Some(3));
        assert_eq!(source.pos_to_line(Pos(22)), None);
    }

    #[test]
    fn test_source_file_roundtrip() {
        #[track_caller]
        fn roundtrip(source: &SourceFile, byte_pos: Pos) {
            let line = source.pos_to_line(byte_pos).unwrap();
            let column = source.pos_to_column(byte_pos).unwrap();
            let result = source.line_column_to_pos(line, column).unwrap();
            assert_eq!(result, byte_pos);
        }

        let source = SourceFile::new(ID, TEST.into());
        roundtrip(&source, Pos(0));
        roundtrip(&source, Pos(7));
        roundtrip(&source, Pos(12));
        roundtrip(&source, Pos(21));
    }
}