| 1 | //! A data structure for tracking source positions in language implementations, inspired by the |
| 2 | //! [CodeMap type in rustc's libsyntax](https://github.com/rust-lang/rust/blob/master/src/libsyntax/codemap.rs). |
| 3 | //! |
| 4 | //! The `CodeMap` tracks all source files and maps positions within them to linear indexes as if all |
| 5 | //! source files were concatenated. This allows a source position to be represented by a small |
| 6 | //! 32-bit `Pos` indexing into the `CodeMap`, under the assumption that the total amount of parsed |
| 7 | //! source code will not exceed 4GiB. The `CodeMap` can look up the source file, line, and column |
| 8 | //! of a `Pos` or `Span`, as well as provide source code snippets for error reporting. |
| 9 | //! |
| 10 | //! # Example |
| 11 | //! ``` |
| 12 | //! use codemap::CodeMap; |
| 13 | //! let mut codemap = CodeMap::new(); |
| 14 | //! let file = codemap.add_file("test.rs".to_string(), "fn test(){\n println!(\"Hello\");\n}\n".to_string()); |
| 15 | //! let string_literal_span = file.span.subspan(24, 31); |
| 16 | //! |
| 17 | //! let location = codemap.look_up_span(string_literal_span); |
| 18 | //! assert_eq!(location.file.name(), "test.rs"); |
| 19 | //! assert_eq!(location.begin.line, 1); |
| 20 | //! assert_eq!(location.begin.column, 13); |
| 21 | //! assert_eq!(location.end.line, 1); |
| 22 | //! assert_eq!(location.end.column, 20); |
| 23 | //! ``` |
| 24 | |
| 25 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::{selfrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#}; |
| 26 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/; |
| 27 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::hashrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/::{Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#, Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher#}; |
| 28 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::{Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#, Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#, Subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#}; |
| 29 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#; |
| 30 | |
| 31 | /// A small, `Copy`, value representing a position in a `CodeMap`'s file. |
| 32 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 33 | pub struct Posrust-analyzer cargo codemap 0.1.3 Pos#(u32); |
| 34 | |
| 35 | impl Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#<u64> for Posrust-analyzer cargo codemap 0.1.3 Pos# { |
| 36 | type Outputrust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]Output# = Posrust-analyzer cargo codemap 0.1.3 Pos#; |
| 37 | fn addrust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add().(self, other: u64) -> Posrust-analyzer cargo codemap 0.1.3 Pos# { |
| 38 | Posrust-analyzer cargo codemap 0.1.3 Pos#(self.0rust-analyzer cargo codemap 0.1.3 Pos#0. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().other as u32) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl Subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#<Posrust-analyzer cargo codemap 0.1.3 Pos#> for Posrust-analyzer cargo codemap 0.1.3 Pos# { |
| 43 | type Outputrust-analyzer cargo codemap 0.1.3 impl#[Pos][`Sub<Self>`]Output# = u64; |
| 44 | fn subrust-analyzer cargo codemap 0.1.3 impl#[Pos][`Sub<Self>`]sub().(self, other: Posrust-analyzer cargo codemap 0.1.3 Pos#) -> u64 { |
| 45 | (self.0rust-analyzer cargo codemap 0.1.3 Pos#0. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub().other.0rust-analyzer cargo codemap 0.1.3 Pos#0.) as u64 |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// A range of text within a CodeMap. |
| 50 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 51 | pub struct Spanrust-analyzer cargo codemap 0.1.3 Span# { |
| 52 | /// The position in the codemap representing the first byte of the span. |
| 53 | lowrust-analyzer cargo codemap 0.1.3 Span#low.: Posrust-analyzer cargo codemap 0.1.3 Pos#, |
| 54 | |
| 55 | /// The position after the last byte of the span. |
| 56 | highrust-analyzer cargo codemap 0.1.3 Span#high.: Posrust-analyzer cargo codemap 0.1.3 Pos#, |
| 57 | } |
| 58 | |
| 59 | impl Spanrust-analyzer cargo codemap 0.1.3 Span# { |
| 60 | /// Makes a span from offsets relative to the start of this span. |
| 61 | /// |
| 62 | /// # Panics |
| 63 | /// * If `end < begin` |
| 64 | /// * If `end` is beyond the length of the span |
| 65 | pub fn subspanrust-analyzer cargo codemap 0.1.3 impl#[Span]subspan().(&self, begin: u64, end: u64) -> Spanrust-analyzer cargo codemap 0.1.3 Span# { |
| 66 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(end >= begin); |
| 67 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(self.lowrust-analyzer cargo codemap 0.1.3 Span#low. +rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add(). end <= self.highrust-analyzer cargo codemap 0.1.3 Span#high.); |
| 68 | Spanrust-analyzer cargo codemap 0.1.3 Span# { |
| 69 | lowrust-analyzer cargo codemap 0.1.3 Span#low.: self.lowrust-analyzer cargo codemap 0.1.3 Span#low. rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add().+rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add(). rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add().begin, |
| 70 | highrust-analyzer cargo codemap 0.1.3 Span#high.: self.lowrust-analyzer cargo codemap 0.1.3 Span#low. rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add().+rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add(). rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add().end, |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// Checks if a span is contained within this span. |
| 75 | pub fn containsrust-analyzer cargo codemap 0.1.3 impl#[Span]contains().(&self, other: Spanrust-analyzer cargo codemap 0.1.3 Span#) -> bool { |
| 76 | self.lowrust-analyzer cargo codemap 0.1.3 Span#low. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le().other.lowrust-analyzer cargo codemap 0.1.3 Span#low. && self.highrust-analyzer cargo codemap 0.1.3 Span#high. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#ge().other.highrust-analyzer cargo codemap 0.1.3 Span#high. |
| 77 | } |
| 78 | |
| 79 | /// The position in the codemap representing the first byte of the span. |
| 80 | pub fn lowrust-analyzer cargo codemap 0.1.3 impl#[Span]low().(&self) -> Posrust-analyzer cargo codemap 0.1.3 Pos# { |
| 81 | self.lowrust-analyzer cargo codemap 0.1.3 Span#low. |
| 82 | } |
| 83 | |
| 84 | /// The position after the last byte of the span. |
| 85 | pub fn highrust-analyzer cargo codemap 0.1.3 impl#[Span]high().(&self) -> Posrust-analyzer cargo codemap 0.1.3 Pos# { |
| 86 | self.highrust-analyzer cargo codemap 0.1.3 Span#high. |
| 87 | } |
| 88 | |
| 89 | /// The length in bytes of the text of the span |
| 90 | pub fn lenrust-analyzer cargo codemap 0.1.3 impl#[Span]len().(&self) -> u64 { |
| 91 | self.highrust-analyzer cargo codemap 0.1.3 Span#high. rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Sub<Self>`]sub().-rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Sub<Self>`]sub(). rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Sub<Self>`]sub().self.lowrust-analyzer cargo codemap 0.1.3 Span#low. |
| 92 | } |
| 93 | |
| 94 | /// Create a span that encloses both `self` and `other`. |
| 95 | pub fn mergerust-analyzer cargo codemap 0.1.3 impl#[Span]merge().(&self, other: Spanrust-analyzer cargo codemap 0.1.3 Span#) -> Spanrust-analyzer cargo codemap 0.1.3 Span# { |
| 96 | Spanrust-analyzer cargo codemap 0.1.3 Span# { |
| 97 | lowrust-analyzer cargo codemap 0.1.3 Span#low.: cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(self.lowrust-analyzer cargo codemap 0.1.3 Span#low., other.lowrust-analyzer cargo codemap 0.1.3 Span#low.), |
| 98 | highrust-analyzer cargo codemap 0.1.3 Span#high.: cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(self.highrust-analyzer cargo codemap 0.1.3 Span#high., other.highrust-analyzer cargo codemap 0.1.3 Span#high.), |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /// Associate a Span with a value of arbitrary type (e.g. an AST node). |
| 104 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)] |
| 105 | pub struct Spannedrust-analyzer cargo codemap 0.1.3 Spanned#<T> { |
| 106 | pub noderust-analyzer cargo codemap 0.1.3 Spanned#node.: T, |
| 107 | pub spanrust-analyzer cargo codemap 0.1.3 Spanned#span.: Spanrust-analyzer cargo codemap 0.1.3 Span#, |
| 108 | } |
| 109 | |
| 110 | impl<T> Spannedrust-analyzer cargo codemap 0.1.3 Spanned#<T> { |
| 111 | /// Maps a `Spanned<T>` to `Spanned<U>` by applying the function to the node, |
| 112 | /// leaving the span untouched. |
| 113 | pub fn map_noderust-analyzer cargo codemap 0.1.3 impl#[`Spanned<T>`]map_node().<U, F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(T) -> U>(self, op: F) -> Spannedrust-analyzer cargo codemap 0.1.3 Spanned#<U> { |
| 114 | Spannedrust-analyzer cargo codemap 0.1.3 Spanned# { |
| 115 | noderust-analyzer cargo codemap 0.1.3 Spanned#node.: op(self.noderust-analyzer cargo codemap 0.1.3 Spanned#node.), |
| 116 | spanrust-analyzer cargo codemap 0.1.3 Spanned#span.: self.spanrust-analyzer cargo codemap 0.1.3 Spanned#span. |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | impl<T> Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref# for Spannedrust-analyzer cargo codemap 0.1.3 Spanned#<T> { |
| 122 | type Targetrust-analyzer cargo codemap 0.1.3 impl#[`Spanned<T>`][Deref]Target# = T; |
| 123 | |
| 124 | fn derefrust-analyzer cargo codemap 0.1.3 impl#[`Spanned<T>`][Deref]deref().(&self) -> &T { |
| 125 | &self.noderust-analyzer cargo codemap 0.1.3 Spanned#node. |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /// A data structure recording source code files for position lookup. |
| 130 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 131 | pub struct CodeMaprust-analyzer cargo codemap 0.1.3 CodeMap# { |
| 132 | filesrust-analyzer cargo codemap 0.1.3 CodeMap#files.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<Filerust-analyzer cargo codemap 0.1.3 File#>>, |
| 133 | } |
| 134 | |
| 135 | impl CodeMaprust-analyzer cargo codemap 0.1.3 CodeMap# { |
| 136 | /// Creates an empty `CodeMap`. |
| 137 | pub fn newrust-analyzer cargo codemap 0.1.3 impl#[CodeMap]new().() -> CodeMaprust-analyzer cargo codemap 0.1.3 CodeMap# { |
| 138 | Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo codemap 0.1.3 impl#[CodeMap][Default]default().() |
| 139 | } |
| 140 | |
| 141 | /// Adds a file with the given name and contents. |
| 142 | /// |
| 143 | /// Use the returned `File` and its `.span` property to create `Spans` |
| 144 | /// representing substrings of the file. |
| 145 | pub fn add_filerust-analyzer cargo codemap 0.1.3 impl#[CodeMap]add_file().(&mut self, name: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, source: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<Filerust-analyzer cargo codemap 0.1.3 File#> { |
| 146 | let low = self.end_posrust-analyzer cargo codemap 0.1.3 impl#[CodeMap]end_pos().() rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add().+rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add(). rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add().1; |
| 147 | let high = low rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add().+rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add(). rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add().source.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().() as u64; |
| 148 | let mut lines = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![low]; |
| 149 | lines.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().( |
| 150 | source |
| 151 | .match_indicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]match_indices().('\n') |
| 152 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(p, _)| low rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add().+rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add(). rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add().(p rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1) as u64), |
| 153 | ); |
| 154 | |
| 155 | let file = Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T>`]new().(Filerust-analyzer cargo codemap 0.1.3 File# { |
| 156 | spanrust-analyzer cargo codemap 0.1.3 File#span.: Spanrust-analyzer cargo codemap 0.1.3 Span# { lowrust-analyzer cargo codemap 0.1.3 Span#low., highrust-analyzer cargo codemap 0.1.3 Span#high. }, |
| 157 | namerust-analyzer cargo codemap 0.1.3 File#name., |
| 158 | sourcerust-analyzer cargo codemap 0.1.3 File#source., |
| 159 | linesrust-analyzer cargo codemap 0.1.3 File#lines., |
| 160 | }); |
| 161 | |
| 162 | self.filesrust-analyzer cargo codemap 0.1.3 CodeMap#files..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(file.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().()); |
| 163 | file |
| 164 | } |
| 165 | |
| 166 | fn end_posrust-analyzer cargo codemap 0.1.3 impl#[CodeMap]end_pos().(&self) -> Posrust-analyzer cargo codemap 0.1.3 Pos# { |
| 167 | self.filesrust-analyzer cargo codemap 0.1.3 CodeMap#files..lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]last().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|x| x.spanrust-analyzer cargo codemap 0.1.3 File#span..highrust-analyzer cargo codemap 0.1.3 Span#high.).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(Posrust-analyzer cargo codemap 0.1.3 Pos#(0)) |
| 168 | } |
| 169 | |
| 170 | /// Looks up the `File` that contains the specified position. |
| 171 | pub fn find_filerust-analyzer cargo codemap 0.1.3 impl#[CodeMap]find_file().(&self, pos: Posrust-analyzer cargo codemap 0.1.3 Pos#) -> &Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<Filerust-analyzer cargo codemap 0.1.3 File#> { |
| 172 | self.filesrust-analyzer cargo codemap 0.1.3 CodeMap#files. |
| 173 | .binary_search_byrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]binary_search_by().(|file| { |
| 174 | if file.spanrust-analyzer cargo codemap 0.1.3 File#span..highrust-analyzer cargo codemap 0.1.3 Span#high. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().pos { |
| 175 | Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Lessrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Less# |
| 176 | } else if file.spanrust-analyzer cargo codemap 0.1.3 File#span..lowrust-analyzer cargo codemap 0.1.3 Span#low. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().pos { |
| 177 | Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Greaterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Greater# |
| 178 | } else { |
| 179 | Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Equalrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Equal# |
| 180 | } |
| 181 | }) |
| 182 | .okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().() |
| 183 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|i| &self.filesrust-analyzer cargo codemap 0.1.3 CodeMap#files.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().i]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().) |
| 184 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("Mapping unknown source location") |
| 185 | } |
| 186 | |
| 187 | /// Gets the file, line, and column represented by a `Pos`. |
| 188 | pub fn look_up_posrust-analyzer cargo codemap 0.1.3 impl#[CodeMap]look_up_pos().(&self, pos: Posrust-analyzer cargo codemap 0.1.3 Pos#) -> Locrust-analyzer cargo codemap 0.1.3 Loc# { |
| 189 | let file = self.find_filerust-analyzer cargo codemap 0.1.3 impl#[CodeMap]find_file().(pos); |
| 190 | let position = file.find_line_colrust-analyzer cargo codemap 0.1.3 impl#[File]find_line_col().(pos); |
| 191 | Locrust-analyzer cargo codemap 0.1.3 Loc# { |
| 192 | filerust-analyzer cargo codemap 0.1.3 Loc#file.: file.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(), |
| 193 | positionrust-analyzer cargo codemap 0.1.3 Loc#position., |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /// Gets the file and its line and column ranges represented by a `Span`. |
| 198 | pub fn look_up_spanrust-analyzer cargo codemap 0.1.3 impl#[CodeMap]look_up_span().(&self, span: Spanrust-analyzer cargo codemap 0.1.3 Span#) -> SpanLocrust-analyzer cargo codemap 0.1.3 SpanLoc# { |
| 199 | let file = self.find_filerust-analyzer cargo codemap 0.1.3 impl#[CodeMap]find_file().(span.lowrust-analyzer cargo codemap 0.1.3 Span#low.); |
| 200 | let begin = file.find_line_colrust-analyzer cargo codemap 0.1.3 impl#[File]find_line_col().(span.lowrust-analyzer cargo codemap 0.1.3 Span#low.); |
| 201 | let end = file.find_line_colrust-analyzer cargo codemap 0.1.3 impl#[File]find_line_col().(span.highrust-analyzer cargo codemap 0.1.3 Span#high.); |
| 202 | SpanLocrust-analyzer cargo codemap 0.1.3 SpanLoc# { |
| 203 | filerust-analyzer cargo codemap 0.1.3 SpanLoc#file.: file.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(), |
| 204 | beginrust-analyzer cargo codemap 0.1.3 SpanLoc#begin., |
| 205 | endrust-analyzer cargo codemap 0.1.3 SpanLoc#end., |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /// A `CodeMap`'s record of a source file. |
| 211 | pub struct Filerust-analyzer cargo codemap 0.1.3 File# { |
| 212 | /// The span representing the entire file. |
| 213 | pub spanrust-analyzer cargo codemap 0.1.3 File#span.: Spanrust-analyzer cargo codemap 0.1.3 Span#, |
| 214 | |
| 215 | /// The filename as it would be displayed in an error message. |
| 216 | namerust-analyzer cargo codemap 0.1.3 File#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 217 | |
| 218 | /// Contents of the file. |
| 219 | sourcerust-analyzer cargo codemap 0.1.3 File#source.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 220 | |
| 221 | /// Byte positions of line beginnings. |
| 222 | linesrust-analyzer cargo codemap 0.1.3 File#lines.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Posrust-analyzer cargo codemap 0.1.3 Pos#>, |
| 223 | } |
| 224 | |
| 225 | impl Filerust-analyzer cargo codemap 0.1.3 File# { |
| 226 | /// Gets the name of the file |
| 227 | pub fn namerust-analyzer cargo codemap 0.1.3 impl#[File]name().(&self) -> &str { |
| 228 | &self.namerust-analyzer cargo codemap 0.1.3 File#name. |
| 229 | } |
| 230 | |
| 231 | /// Gets the line number of a Pos. |
| 232 | /// |
| 233 | /// The lines are 0-indexed (first line is numbered 0) |
| 234 | /// |
| 235 | /// # Panics |
| 236 | /// |
| 237 | /// * If `pos` is not within this file's span |
| 238 | pub fn find_linerust-analyzer cargo codemap 0.1.3 impl#[File]find_line().(&self, pos: Posrust-analyzer cargo codemap 0.1.3 Pos#) -> usize { |
| 239 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(pos >= self.spanrust-analyzer cargo codemap 0.1.3 File#span..lowrust-analyzer cargo codemap 0.1.3 Span#low.); |
| 240 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(pos <= self.spanrust-analyzer cargo codemap 0.1.3 File#span..highrust-analyzer cargo codemap 0.1.3 Span#high.); |
| 241 | match self.linesrust-analyzer cargo codemap 0.1.3 File#lines..binary_searchrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]binary_search().(&pos) { |
| 242 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(i) => i, |
| 243 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(i) => i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1, |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /// Gets the line and column of a Pos. |
| 248 | /// |
| 249 | /// # Panics |
| 250 | /// |
| 251 | /// * If `pos` is not with this file's span |
| 252 | /// * If `pos` points to a byte in the middle of a UTF-8 character |
| 253 | pub fn find_line_colrust-analyzer cargo codemap 0.1.3 impl#[File]find_line_col().(&self, pos: Posrust-analyzer cargo codemap 0.1.3 Pos#) -> LineColrust-analyzer cargo codemap 0.1.3 LineCol# { |
| 254 | let line = self.find_linerust-analyzer cargo codemap 0.1.3 impl#[File]find_line().(pos); |
| 255 | let line_span = self.line_spanrust-analyzer cargo codemap 0.1.3 impl#[File]line_span().(line); |
| 256 | let byte_col = pos rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Sub<Self>`]sub().-rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Sub<Self>`]sub(). rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Sub<Self>`]sub().line_span.lowrust-analyzer cargo codemap 0.1.3 Span#low.; |
| 257 | let column = self.source_slicerust-analyzer cargo codemap 0.1.3 impl#[File]source_slice().(line_span)[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#byte_col as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). |
| 258 | .charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().() |
| 259 | .countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]count().(); |
| 260 | |
| 261 | LineColrust-analyzer cargo codemap 0.1.3 LineCol# { linerust-analyzer cargo codemap 0.1.3 LineCol#line., columnrust-analyzer cargo codemap 0.1.3 LineCol#column. } |
| 262 | } |
| 263 | |
| 264 | /// Gets the full source text of the file |
| 265 | pub fn sourcerust-analyzer cargo codemap 0.1.3 impl#[File]source().(&self) -> &str { |
| 266 | &self.sourcerust-analyzer cargo codemap 0.1.3 File#source. |
| 267 | } |
| 268 | |
| 269 | /// Gets the source text of a Span. |
| 270 | /// |
| 271 | /// # Panics |
| 272 | /// |
| 273 | /// * If `span` is not entirely within this file. |
| 274 | pub fn source_slicerust-analyzer cargo codemap 0.1.3 impl#[File]source_slice().(&self, span: Spanrust-analyzer cargo codemap 0.1.3 Span#) -> &str { |
| 275 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(self.spanrust-analyzer cargo codemap 0.1.3 File#span..containsrust-analyzer cargo codemap 0.1.3 impl#[Span]contains().(span)); |
| 276 | &self.sourcerust-analyzer cargo codemap 0.1.3 File#source.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().((span.lowrust-analyzer cargo codemap 0.1.3 Span#low. rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Sub<Self>`]sub().-rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Sub<Self>`]sub(). rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Sub<Self>`]sub().self.spanrust-analyzer cargo codemap 0.1.3 File#span..lowrust-analyzer cargo codemap 0.1.3 Span#low.) as usize)..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#((span.highrust-analyzer cargo codemap 0.1.3 Span#high. rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Sub<Self>`]sub().-rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Sub<Self>`]sub(). rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Sub<Self>`]sub().self.spanrust-analyzer cargo codemap 0.1.3 File#span..lowrust-analyzer cargo codemap 0.1.3 Span#low.) as usize)]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index(). |
| 277 | } |
| 278 | |
| 279 | /// Gets the span representing a line by line number. |
| 280 | /// |
| 281 | /// The line number is 0-indexed (first line is numbered 0). The returned span includes the |
| 282 | /// line terminator. |
| 283 | /// |
| 284 | /// # Panics |
| 285 | /// |
| 286 | /// * If the line number is out of range |
| 287 | pub fn line_spanrust-analyzer cargo codemap 0.1.3 impl#[File]line_span().(&self, line: usize) -> Spanrust-analyzer cargo codemap 0.1.3 Span# { |
| 288 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(line <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). self.linesrust-analyzer cargo codemap 0.1.3 File#lines..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 289 | Spanrust-analyzer cargo codemap 0.1.3 Span# { |
| 290 | lowrust-analyzer cargo codemap 0.1.3 Span#low.: self.linesrust-analyzer cargo codemap 0.1.3 File#lines.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().line]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()., |
| 291 | highrust-analyzer cargo codemap 0.1.3 Span#high.: *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self.linesrust-analyzer cargo codemap 0.1.3 File#lines..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(line rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(&self.spanrust-analyzer cargo codemap 0.1.3 File#span..highrust-analyzer cargo codemap 0.1.3 Span#high.), |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /// Gets the source text of a line. |
| 296 | /// |
| 297 | /// The string returned does not include the terminating \r or \n characters. |
| 298 | /// |
| 299 | /// # Panics |
| 300 | /// |
| 301 | /// * If the line number is out of range |
| 302 | pub fn source_linerust-analyzer cargo codemap 0.1.3 impl#[File]source_line().(&self, line: usize) -> &str { |
| 303 | self.source_slicerust-analyzer cargo codemap 0.1.3 impl#[File]source_slice().(self.line_spanrust-analyzer cargo codemap 0.1.3 impl#[File]line_span().(line)) |
| 304 | .trim_end_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_end_matches().(&['\n', '\r'][rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().) |
| 305 | } |
| 306 | |
| 307 | /// Gets the number of lines in the file |
| 308 | pub fn num_linesrust-analyzer cargo codemap 0.1.3 impl#[File]num_lines().(&self) -> usize { |
| 309 | self.linesrust-analyzer cargo codemap 0.1.3 File#lines..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for Filerust-analyzer cargo codemap 0.1.3 File# { |
| 314 | fn fmtrust-analyzer cargo codemap 0.1.3 impl#[File][Debug]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Error#> { |
| 315 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "File({:?})", self.namerust-analyzer cargo codemap 0.1.3 File#name.) |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | impl PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq# for Filerust-analyzer cargo codemap 0.1.3 File# { |
| 320 | /// Compares by identity |
| 321 | fn eqrust-analyzer cargo codemap 0.1.3 impl#[File][`PartialEq<Self>`]eq().(&self, other: &Filerust-analyzer cargo codemap 0.1.3 File#) -> bool { |
| 322 | self as *const _ rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`][`PartialEq<Self>`]eq().other as *const _ |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | impl Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# for Filerust-analyzer cargo codemap 0.1.3 File# {} |
| 327 | |
| 328 | impl Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# for Filerust-analyzer cargo codemap 0.1.3 File# { |
| 329 | fn hashrust-analyzer cargo codemap 0.1.3 impl#[File][Hash]hash().<H: Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher#>(&self, hasher: &mut H) { |
| 330 | self.spanrust-analyzer cargo codemap 0.1.3 File#span..hashrust-analyzer cargo codemap 0.1.3 impl#[Span][Hash]hash().(hasher); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /// A line and column. |
| 335 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 336 | pub struct LineColrust-analyzer cargo codemap 0.1.3 LineCol# { |
| 337 | /// The line number within the file (0-indexed). |
| 338 | pub linerust-analyzer cargo codemap 0.1.3 LineCol#line.: usize, |
| 339 | |
| 340 | /// The column within the line (0-indexed). |
| 341 | pub columnrust-analyzer cargo codemap 0.1.3 LineCol#column.: usize, |
| 342 | } |
| 343 | |
| 344 | /// A file, and a line and column within it. |
| 345 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 346 | pub struct Locrust-analyzer cargo codemap 0.1.3 Loc# { |
| 347 | pub filerust-analyzer cargo codemap 0.1.3 Loc#file.: Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<Filerust-analyzer cargo codemap 0.1.3 File#>, |
| 348 | pub positionrust-analyzer cargo codemap 0.1.3 Loc#position.: LineColrust-analyzer cargo codemap 0.1.3 LineCol#, |
| 349 | } |
| 350 | |
| 351 | impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Locrust-analyzer cargo codemap 0.1.3 Loc# { |
| 352 | /// Formats the location as `filename:line:column`, with a 1-indexed |
| 353 | /// line and column. |
| 354 | fn fmtrust-analyzer cargo codemap 0.1.3 impl#[Loc][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Error#> { |
| 355 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!( |
| 356 | f, |
| 357 | "{}:{}:{}", |
| 358 | self.filerust-analyzer cargo codemap 0.1.3 Loc#file..namerust-analyzer cargo codemap 0.1.3 File#name., |
| 359 | self.positionrust-analyzer cargo codemap 0.1.3 Loc#position..linerust-analyzer cargo codemap 0.1.3 LineCol#line. +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 1, |
| 360 | self.positionrust-analyzer cargo codemap 0.1.3 Loc#position..columnrust-analyzer cargo codemap 0.1.3 LineCol#column. +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 1 |
| 361 | ) |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /// A file, and a line and column range within it. |
| 366 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 367 | pub struct SpanLocrust-analyzer cargo codemap 0.1.3 SpanLoc# { |
| 368 | pub filerust-analyzer cargo codemap 0.1.3 SpanLoc#file.: Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<Filerust-analyzer cargo codemap 0.1.3 File#>, |
| 369 | pub beginrust-analyzer cargo codemap 0.1.3 SpanLoc#begin.: LineColrust-analyzer cargo codemap 0.1.3 LineCol#, |
| 370 | pub endrust-analyzer cargo codemap 0.1.3 SpanLoc#end.: LineColrust-analyzer cargo codemap 0.1.3 LineCol#, |
| 371 | } |
| 372 | |
| 373 | impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for SpanLocrust-analyzer cargo codemap 0.1.3 SpanLoc# { |
| 374 | /// Formats the span as `filename:start_line:start_column: end_line:end_column`, |
| 375 | /// or if the span is zero-length, `filename:line:column`, with a 1-indexed line and column. |
| 376 | fn fmtrust-analyzer cargo codemap 0.1.3 impl#[SpanLoc][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Error#> { |
| 377 | if self.beginrust-analyzer cargo codemap 0.1.3 SpanLoc#begin. rust-analyzer cargo codemap 0.1.3 impl#[LineCol][`PartialEq<Self>`]eq().==rust-analyzer cargo codemap 0.1.3 impl#[LineCol][`PartialEq<Self>`]eq(). rust-analyzer cargo codemap 0.1.3 impl#[LineCol][`PartialEq<Self>`]eq().self.endrust-analyzer cargo codemap 0.1.3 SpanLoc#end. { |
| 378 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!( |
| 379 | f, |
| 380 | "{}:{}:{}", |
| 381 | self.filerust-analyzer cargo codemap 0.1.3 SpanLoc#file..namerust-analyzer cargo codemap 0.1.3 File#name., |
| 382 | self.beginrust-analyzer cargo codemap 0.1.3 SpanLoc#begin..linerust-analyzer cargo codemap 0.1.3 LineCol#line. +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 1, |
| 383 | self.beginrust-analyzer cargo codemap 0.1.3 SpanLoc#begin..columnrust-analyzer cargo codemap 0.1.3 LineCol#column. +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 1 |
| 384 | ) |
| 385 | } else { |
| 386 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!( |
| 387 | f, |
| 388 | "{}:{}:{}: {}:{}", |
| 389 | self.filerust-analyzer cargo codemap 0.1.3 SpanLoc#file..namerust-analyzer cargo codemap 0.1.3 File#name., |
| 390 | self.beginrust-analyzer cargo codemap 0.1.3 SpanLoc#begin..linerust-analyzer cargo codemap 0.1.3 LineCol#line. +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 1, |
| 391 | self.beginrust-analyzer cargo codemap 0.1.3 SpanLoc#begin..columnrust-analyzer cargo codemap 0.1.3 LineCol#column. +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 1, |
| 392 | self.endrust-analyzer cargo codemap 0.1.3 SpanLoc#end..linerust-analyzer cargo codemap 0.1.3 LineCol#line. +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 1, |
| 393 | self.endrust-analyzer cargo codemap 0.1.3 SpanLoc#end..columnrust-analyzer cargo codemap 0.1.3 LineCol#column. +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 1 |
| 394 | ) |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 400 | fn test_codemaprust-analyzer cargo codemap 0.1.3 test_codemap().() { |
| 401 | let mut codemap = CodeMaprust-analyzer cargo codemap 0.1.3 CodeMap#::newrust-analyzer cargo codemap 0.1.3 impl#[CodeMap]new().(); |
| 402 | let f1 = codemap.add_filerust-analyzer cargo codemap 0.1.3 impl#[CodeMap]add_file().("test1.rs".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), "abcd\nefghij\nqwerty".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 403 | let f2 = codemap.add_filerust-analyzer cargo codemap 0.1.3 impl#[CodeMap]add_file().("test2.rs".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), "foo\nbar".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 404 | |
| 405 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(codemap.find_filerust-analyzer cargo codemap 0.1.3 impl#[CodeMap]find_file().(f1.spanrust-analyzer cargo codemap 0.1.3 File#span..lowrust-analyzer cargo codemap 0.1.3 impl#[Span]low().()).namerust-analyzer cargo codemap 0.1.3 impl#[File]name().(), "test1.rs"); |
| 406 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(codemap.find_filerust-analyzer cargo codemap 0.1.3 impl#[CodeMap]find_file().(f1.spanrust-analyzer cargo codemap 0.1.3 File#span..highrust-analyzer cargo codemap 0.1.3 impl#[Span]high().()).namerust-analyzer cargo codemap 0.1.3 impl#[File]name().(), "test1.rs"); |
| 407 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(codemap.find_filerust-analyzer cargo codemap 0.1.3 impl#[CodeMap]find_file().(f2.spanrust-analyzer cargo codemap 0.1.3 File#span..lowrust-analyzer cargo codemap 0.1.3 impl#[Span]low().()).namerust-analyzer cargo codemap 0.1.3 impl#[File]name().(), "test2.rs"); |
| 408 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(codemap.find_filerust-analyzer cargo codemap 0.1.3 impl#[CodeMap]find_file().(f2.spanrust-analyzer cargo codemap 0.1.3 File#span..highrust-analyzer cargo codemap 0.1.3 impl#[Span]high().()).namerust-analyzer cargo codemap 0.1.3 impl#[File]name().(), "test2.rs"); |
| 409 | |
| 410 | let x = f1.spanrust-analyzer cargo codemap 0.1.3 File#span..subspanrust-analyzer cargo codemap 0.1.3 impl#[Span]subspan().(5, 10); |
| 411 | let f = codemap.find_filerust-analyzer cargo codemap 0.1.3 impl#[CodeMap]find_file().(x.lowrust-analyzer cargo codemap 0.1.3 Span#low.); |
| 412 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(f.namerust-analyzer cargo codemap 0.1.3 File#name., "test1.rs"); |
| 413 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 414 | f.find_line_colrust-analyzer cargo codemap 0.1.3 impl#[File]find_line_col().(f.spanrust-analyzer cargo codemap 0.1.3 File#span..lowrust-analyzer cargo codemap 0.1.3 impl#[Span]low().()), |
| 415 | LineColrust-analyzer cargo codemap 0.1.3 LineCol# { linerust-analyzer cargo codemap 0.1.3 LineCol#line.: 0, columnrust-analyzer cargo codemap 0.1.3 LineCol#column.: 0 } |
| 416 | ); |
| 417 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 418 | f.find_line_colrust-analyzer cargo codemap 0.1.3 impl#[File]find_line_col().(f.spanrust-analyzer cargo codemap 0.1.3 File#span..lowrust-analyzer cargo codemap 0.1.3 impl#[Span]low().() +rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add(). 4), |
| 419 | LineColrust-analyzer cargo codemap 0.1.3 LineCol# { linerust-analyzer cargo codemap 0.1.3 LineCol#line.: 0, columnrust-analyzer cargo codemap 0.1.3 LineCol#column.: 4 } |
| 420 | ); |
| 421 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 422 | f.find_line_colrust-analyzer cargo codemap 0.1.3 impl#[File]find_line_col().(f.spanrust-analyzer cargo codemap 0.1.3 File#span..lowrust-analyzer cargo codemap 0.1.3 impl#[Span]low().() +rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add(). 5), |
| 423 | LineColrust-analyzer cargo codemap 0.1.3 LineCol# { linerust-analyzer cargo codemap 0.1.3 LineCol#line.: 1, columnrust-analyzer cargo codemap 0.1.3 LineCol#column.: 0 } |
| 424 | ); |
| 425 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 426 | f.find_line_colrust-analyzer cargo codemap 0.1.3 impl#[File]find_line_col().(f.spanrust-analyzer cargo codemap 0.1.3 File#span..lowrust-analyzer cargo codemap 0.1.3 impl#[Span]low().() +rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add(). 16), |
| 427 | LineColrust-analyzer cargo codemap 0.1.3 LineCol# { linerust-analyzer cargo codemap 0.1.3 LineCol#line.: 2, columnrust-analyzer cargo codemap 0.1.3 LineCol#column.: 4 } |
| 428 | ); |
| 429 | |
| 430 | let x = f2.spanrust-analyzer cargo codemap 0.1.3 File#span..subspanrust-analyzer cargo codemap 0.1.3 impl#[Span]subspan().(4, 7); |
| 431 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(codemap.find_filerust-analyzer cargo codemap 0.1.3 impl#[CodeMap]find_file().(x.lowrust-analyzer cargo codemap 0.1.3 impl#[Span]low().()).namerust-analyzer cargo codemap 0.1.3 impl#[File]name().(), "test2.rs"); |
| 432 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(codemap.find_filerust-analyzer cargo codemap 0.1.3 impl#[CodeMap]find_file().(x.highrust-analyzer cargo codemap 0.1.3 impl#[Span]high().()).namerust-analyzer cargo codemap 0.1.3 impl#[File]name().(), "test2.rs"); |
| 433 | } |
| 434 | |
| 435 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 436 | fn test_issue2rust-analyzer cargo codemap 0.1.3 test_issue2().() { |
| 437 | let mut codemap = CodeMaprust-analyzer cargo codemap 0.1.3 CodeMap#::newrust-analyzer cargo codemap 0.1.3 impl#[CodeMap]new().(); |
| 438 | let content = "a \nxyz\r\n"; |
| 439 | let file = codemap.add_filerust-analyzer cargo codemap 0.1.3 impl#[CodeMap]add_file().("<test>".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), content.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()); |
| 440 | |
| 441 | let span = file.spanrust-analyzer cargo codemap 0.1.3 File#span..subspanrust-analyzer cargo codemap 0.1.3 impl#[Span]subspan().(2, 3); |
| 442 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 443 | codemap.look_up_spanrust-analyzer cargo codemap 0.1.3 impl#[CodeMap]look_up_span().(span), |
| 444 | SpanLocrust-analyzer cargo codemap 0.1.3 SpanLoc# { |
| 445 | filerust-analyzer cargo codemap 0.1.3 SpanLoc#file.: file.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(), |
| 446 | beginrust-analyzer cargo codemap 0.1.3 SpanLoc#begin.: LineColrust-analyzer cargo codemap 0.1.3 LineCol# { linerust-analyzer cargo codemap 0.1.3 LineCol#line.: 0, columnrust-analyzer cargo codemap 0.1.3 LineCol#column.: 2 }, |
| 447 | endrust-analyzer cargo codemap 0.1.3 SpanLoc#end.: LineColrust-analyzer cargo codemap 0.1.3 LineCol# { linerust-analyzer cargo codemap 0.1.3 LineCol#line.: 1, columnrust-analyzer cargo codemap 0.1.3 LineCol#column.: 0 } |
| 448 | } |
| 449 | ); |
| 450 | |
| 451 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(file.source_linerust-analyzer cargo codemap 0.1.3 impl#[File]source_line().(0), "a "); |
| 452 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(file.source_linerust-analyzer cargo codemap 0.1.3 impl#[File]source_line().(1), "xyz"); |
| 453 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(file.source_linerust-analyzer cargo codemap 0.1.3 impl#[File]source_line().(2), ""); |
| 454 | } |
| 455 | |
| 456 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 457 | fn test_multibyterust-analyzer cargo codemap 0.1.3 test_multibyte().() { |
| 458 | let mut codemap = CodeMaprust-analyzer cargo codemap 0.1.3 CodeMap#::newrust-analyzer cargo codemap 0.1.3 impl#[CodeMap]new().(); |
| 459 | let content = "65°00′N 18°00′W 汉语\n🔬"; |
| 460 | let file = codemap.add_filerust-analyzer cargo codemap 0.1.3 impl#[CodeMap]add_file().("<test>".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), content.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()); |
| 461 | |
| 462 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 463 | codemap.look_up_posrust-analyzer cargo codemap 0.1.3 impl#[CodeMap]look_up_pos().(file.spanrust-analyzer cargo codemap 0.1.3 File#span..lowrust-analyzer cargo codemap 0.1.3 impl#[Span]low().() +rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add(). 21), |
| 464 | Locrust-analyzer cargo codemap 0.1.3 Loc# { |
| 465 | filerust-analyzer cargo codemap 0.1.3 Loc#file.: file.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(), |
| 466 | positionrust-analyzer cargo codemap 0.1.3 Loc#position.: LineColrust-analyzer cargo codemap 0.1.3 LineCol# { |
| 467 | linerust-analyzer cargo codemap 0.1.3 LineCol#line.: 0, |
| 468 | columnrust-analyzer cargo codemap 0.1.3 LineCol#column.: 15 |
| 469 | } |
| 470 | } |
| 471 | ); |
| 472 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 473 | codemap.look_up_posrust-analyzer cargo codemap 0.1.3 impl#[CodeMap]look_up_pos().(file.spanrust-analyzer cargo codemap 0.1.3 File#span..lowrust-analyzer cargo codemap 0.1.3 impl#[Span]low().() +rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add(). 28), |
| 474 | Locrust-analyzer cargo codemap 0.1.3 Loc# { |
| 475 | filerust-analyzer cargo codemap 0.1.3 Loc#file.: file.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(), |
| 476 | positionrust-analyzer cargo codemap 0.1.3 Loc#position.: LineColrust-analyzer cargo codemap 0.1.3 LineCol# { |
| 477 | linerust-analyzer cargo codemap 0.1.3 LineCol#line.: 0, |
| 478 | columnrust-analyzer cargo codemap 0.1.3 LineCol#column.: 18 |
| 479 | } |
| 480 | } |
| 481 | ); |
| 482 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 483 | codemap.look_up_posrust-analyzer cargo codemap 0.1.3 impl#[CodeMap]look_up_pos().(file.spanrust-analyzer cargo codemap 0.1.3 File#span..lowrust-analyzer cargo codemap 0.1.3 impl#[Span]low().() +rust-analyzer cargo codemap 0.1.3 impl#[Pos][`Add<u64>`]add(). 33), |
| 484 | Locrust-analyzer cargo codemap 0.1.3 Loc# { |
| 485 | filerust-analyzer cargo codemap 0.1.3 Loc#file.: file.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(), |
| 486 | positionrust-analyzer cargo codemap 0.1.3 Loc#position.: LineColrust-analyzer cargo codemap 0.1.3 LineCol# { linerust-analyzer cargo codemap 0.1.3 LineCol#line.: 1, columnrust-analyzer cargo codemap 0.1.3 LineCol#column.: 1 } |
| 487 | } |
| 488 | ); |
| 489 | } |