| 1 | //! Source file support for diagnostic reporting. |
| 2 | //! |
| 3 | //! The main trait defined in this module is the [`Files`] trait, which provides |
| 4 | //! provides the minimum amount of functionality required for printing [`Diagnostics`] |
| 5 | //! with the [`term::emit`] function. |
| 6 | //! |
| 7 | //! Simple implementations of this trait are implemented: |
| 8 | //! |
| 9 | //! - [`SimpleFile`]: For single-file use-cases |
| 10 | //! - [`SimpleFiles`]: For multi-file use-cases |
| 11 | //! |
| 12 | //! These data structures provide a pretty minimal API, however, |
| 13 | //! so end-users are encouraged to create their own implementations for their |
| 14 | //! own specific use-cases, such as an implementation that accesses the file |
| 15 | //! system directly (and caches the line start locations), or an implementation |
| 16 | //! using an incremental compilation library like [`salsa`]. |
| 17 | //! |
| 18 | //! [`term::emit`]: crate::term::emit |
| 19 | //! [`Diagnostics`]: crate::diagnostic::Diagnostic |
| 20 | //! [`Files`]: Files |
| 21 | //! [`SimpleFile`]: SimpleFile |
| 22 | //! [`SimpleFiles`]: SimpleFiles |
| 23 | //! |
| 24 | //! [`salsa`]: https://crates.io/crates/salsa |
| 25 | |
| 26 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#; |
| 27 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#; |
| 28 | |
| 29 | #[cfg(feature = "std")] |
| 30 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/; |
| 31 | |
| 32 | #[cfg(not(feature = "std"))] |
| 33 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/; |
| 34 | |
| 35 | /// An enum representing an error that happened while looking up a file or a piece of content in that file. |
| 36 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 37 | #[non_exhaustive] |
| 38 | pub enum Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error# { |
| 39 | /// A required file is not in the file database. |
| 40 | FileMissingrust-analyzer cargo codespan-reporting 0.12.0 files/Error#FileMissing#, |
| 41 | /// The file is present, but does not contain the specified byte index. |
| 42 | IndexTooLargerust-analyzer cargo codespan-reporting 0.12.0 files/Error#IndexTooLarge# { givenrust-analyzer cargo codespan-reporting 0.12.0 files/Error#IndexTooLarge#given.: usize, maxrust-analyzer cargo codespan-reporting 0.12.0 files/Error#IndexTooLarge#max.: usize }, |
| 43 | /// The file is present, but does not contain the specified line index. |
| 44 | LineTooLargerust-analyzer cargo codespan-reporting 0.12.0 files/Error#LineTooLarge# { givenrust-analyzer cargo codespan-reporting 0.12.0 files/Error#LineTooLarge#given.: usize, maxrust-analyzer cargo codespan-reporting 0.12.0 files/Error#LineTooLarge#max.: usize }, |
| 45 | /// The file is present and contains the specified line index, but the line does not contain the specified column index. |
| 46 | ColumnTooLargerust-analyzer cargo codespan-reporting 0.12.0 files/Error#ColumnTooLarge# { givenrust-analyzer cargo codespan-reporting 0.12.0 files/Error#ColumnTooLarge#given.: usize, maxrust-analyzer cargo codespan-reporting 0.12.0 files/Error#ColumnTooLarge#max.: usize }, |
| 47 | /// The given index is contained in the file, but is not a boundary of a UTF-8 code point. |
| 48 | InvalidCharBoundaryrust-analyzer cargo codespan-reporting 0.12.0 files/Error#InvalidCharBoundary# { givenrust-analyzer cargo codespan-reporting 0.12.0 files/Error#InvalidCharBoundary#given.: usize }, |
| 49 | /// There was a error while doing IO. |
| 50 | #[cfg(feature = "std")] |
| 51 | Iorust-analyzer cargo codespan-reporting 0.12.0 files/Error#Io#(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#), |
| 52 | /// There was a error during formatting. |
| 53 | FormatErrorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#FormatError#, |
| 54 | } |
| 55 | |
| 56 | #[cfg(feature = "std")] |
| 57 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> for Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error# { |
| 58 | fn fromrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[Error][`From<std::io::Error>`]from().(err: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#) -> Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error# { |
| 59 | Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#::Iorust-analyzer cargo codespan-reporting 0.12.0 files/Error#Io#(err) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Error#> for Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error# { |
| 64 | fn fromrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[Error][`From<core::fmt::Error>`]from().(_err: corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Error#) -> Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error# { |
| 65 | Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#::FormatErrorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#FormatError# |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error# { |
| 70 | fn fmtrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[Error][Display]fmt().(&self, f: &mut corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 71 | match self { |
| 72 | Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#::FileMissingrust-analyzer cargo codespan-reporting 0.12.0 files/Error#FileMissing# => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "file missing"), |
| 73 | Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#::IndexTooLargerust-analyzer cargo codespan-reporting 0.12.0 files/Error#IndexTooLarge# { givenrust-analyzer cargo codespan-reporting 0.12.0 files/Error#IndexTooLarge#given., maxrust-analyzer cargo codespan-reporting 0.12.0 files/Error#IndexTooLarge#max. } => { |
| 74 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "invalid index {}, maximum index is {}", given, max) |
| 75 | } |
| 76 | Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#::LineTooLargerust-analyzer cargo codespan-reporting 0.12.0 files/Error#LineTooLarge# { givenrust-analyzer cargo codespan-reporting 0.12.0 files/Error#LineTooLarge#given., maxrust-analyzer cargo codespan-reporting 0.12.0 files/Error#LineTooLarge#max. } => { |
| 77 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "invalid line {}, maximum line is {}", given, max) |
| 78 | } |
| 79 | Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#::ColumnTooLargerust-analyzer cargo codespan-reporting 0.12.0 files/Error#ColumnTooLarge# { givenrust-analyzer cargo codespan-reporting 0.12.0 files/Error#ColumnTooLarge#given., maxrust-analyzer cargo codespan-reporting 0.12.0 files/Error#ColumnTooLarge#max. } => { |
| 80 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "invalid column {}, maximum column {}", given, max) |
| 81 | } |
| 82 | Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#::InvalidCharBoundaryrust-analyzer cargo codespan-reporting 0.12.0 files/Error#InvalidCharBoundary# { .. } => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "index is not a code point boundary"), |
| 83 | #[cfg(feature = "std")] |
| 84 | Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#::Iorust-analyzer cargo codespan-reporting 0.12.0 files/Error#Io#(err) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{}", err), |
| 85 | Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#::FormatErrorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#FormatError# => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "formatting error"), |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | impl errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error# { |
| 91 | fn sourcerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[Error][Error]source().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&(dyn errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# + 'static)> { |
| 92 | match &self { |
| 93 | #[cfg(feature = "std")] |
| 94 | Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#::Iorust-analyzer cargo codespan-reporting 0.12.0 files/Error#Io#(err) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(err), |
| 95 | _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /// A minimal interface for accessing source files when rendering diagnostics. |
| 101 | /// |
| 102 | /// A lifetime parameter `'a` is provided to allow any of the returned values to returned by reference. |
| 103 | /// This is to workaround the lack of higher kinded lifetime parameters. |
| 104 | /// This can be ignored if this is not needed, however. |
| 105 | pub trait Filesrust-analyzer cargo codespan-reporting 0.12.0 files/Files#<'a> { |
| 106 | /// A unique identifier for files in the file provider. This will be used |
| 107 | /// for rendering `diagnostic::Label`s in the corresponding source files. |
| 108 | type FileIdrust-analyzer cargo codespan-reporting 0.12.0 files/Files#FileId#: 'a + Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy# + PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#; |
| 109 | /// The user-facing name of a file, to be displayed in diagnostics. |
| 110 | type Namerust-analyzer cargo codespan-reporting 0.12.0 files/Files#Name#: 'a + corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#; |
| 111 | /// The source code of a file. |
| 112 | type Sourcerust-analyzer cargo codespan-reporting 0.12.0 files/Files#Source#: 'a + AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<str>; |
| 113 | |
| 114 | /// The user-facing name of a file. |
| 115 | fn namerust-analyzer cargo codespan-reporting 0.12.0 files/Files#name().(&'a self, id: Self::FileIdrust-analyzer cargo codespan-reporting 0.12.0 files/Files#FileId#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Self::Namerust-analyzer cargo codespan-reporting 0.12.0 files/Files#Name#, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#>; |
| 116 | |
| 117 | /// The source code of a file. |
| 118 | fn sourcerust-analyzer cargo codespan-reporting 0.12.0 files/Files#source().(&'a self, id: Self::FileIdrust-analyzer cargo codespan-reporting 0.12.0 files/Files#FileId#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Self::Sourcerust-analyzer cargo codespan-reporting 0.12.0 files/Files#Source#, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#>; |
| 119 | |
| 120 | /// The index of the line at the given byte index. |
| 121 | /// If the byte index is past the end of the file, returns the maximum line index in the file. |
| 122 | /// This means that this function only fails if the file is not present. |
| 123 | /// |
| 124 | /// # Note for trait implementors |
| 125 | /// |
| 126 | /// This can be implemented efficiently by performing a binary search over |
| 127 | /// a list of line starts that was computed by calling the [`line_starts`] |
| 128 | /// function that is exported from the [`files`] module. It might be useful |
| 129 | /// to pre-compute and cache these line starts. |
| 130 | /// |
| 131 | /// [`line_starts`]: crate::files::line_starts |
| 132 | /// [`files`]: crate::files |
| 133 | fn line_indexrust-analyzer cargo codespan-reporting 0.12.0 files/Files#line_index().(&'a self, id: Self::FileIdrust-analyzer cargo codespan-reporting 0.12.0 files/Files#FileId#, byte_index: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#>; |
| 134 | |
| 135 | /// The user-facing line number at the given line index. |
| 136 | /// It is not necessarily checked that the specified line index |
| 137 | /// is actually in the file. |
| 138 | /// |
| 139 | /// # Note for trait implementors |
| 140 | /// |
| 141 | /// This is usually 1-indexed from the beginning of the file, but |
| 142 | /// can be useful for implementing something like the |
| 143 | /// [C preprocessor's `#line` macro][line-macro]. |
| 144 | /// |
| 145 | /// [line-macro]: https://en.cppreference.com/w/c/preprocessor/line |
| 146 | #[allow(unused_variables)] |
| 147 | fn line_numberrust-analyzer cargo codespan-reporting 0.12.0 files/Files#line_number().(&'a self, id: Self::FileIdrust-analyzer cargo codespan-reporting 0.12.0 files/Files#FileId#, line_index: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#> { |
| 148 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(line_index 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) |
| 149 | } |
| 150 | |
| 151 | /// The user-facing column number at the given line index and byte index. |
| 152 | /// |
| 153 | /// # Note for trait implementors |
| 154 | /// |
| 155 | /// This is usually 1-indexed from the the start of the line. |
| 156 | /// A default implementation is provided, based on the [`column_index`] |
| 157 | /// function that is exported from the [`files`] module. |
| 158 | /// |
| 159 | /// [`files`]: crate::files |
| 160 | /// [`column_index`]: crate::files::column_index |
| 161 | fn column_numberrust-analyzer cargo codespan-reporting 0.12.0 files/Files#column_number().( |
| 162 | &'a self, |
| 163 | id: Self::FileIdrust-analyzer cargo codespan-reporting 0.12.0 files/Files#FileId#, |
| 164 | line_index: usize, |
| 165 | byte_index: usize, |
| 166 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#> { |
| 167 | let source = self.sourcerust-analyzer cargo codespan-reporting 0.12.0 files/Files#source().(id)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 168 | let line_range = self.line_rangerust-analyzer cargo codespan-reporting 0.12.0 files/Files#line_range().(id, line_index)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 169 | let column_index = column_indexrust-analyzer cargo codespan-reporting 0.12.0 files/column_index().(source.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(), line_range, byte_index); |
| 170 | |
| 171 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(column_index 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) |
| 172 | } |
| 173 | |
| 174 | /// Convenience method for returning line and column number at the given |
| 175 | /// byte index in the file. |
| 176 | fn locationrust-analyzer cargo codespan-reporting 0.12.0 files/Files#location().(&'a self, id: Self::FileIdrust-analyzer cargo codespan-reporting 0.12.0 files/Files#FileId#, byte_index: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Locationrust-analyzer cargo codespan-reporting 0.12.0 files/Location#, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#> { |
| 177 | let line_index = self.line_indexrust-analyzer cargo codespan-reporting 0.12.0 files/Files#line_index().(id, byte_index)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 178 | |
| 179 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Locationrust-analyzer cargo codespan-reporting 0.12.0 files/Location# { |
| 180 | line_numberrust-analyzer cargo codespan-reporting 0.12.0 files/Location#line_number.: self.line_numberrust-analyzer cargo codespan-reporting 0.12.0 files/Files#line_number().(id, line_index)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 181 | column_numberrust-analyzer cargo codespan-reporting 0.12.0 files/Location#column_number.: self.column_numberrust-analyzer cargo codespan-reporting 0.12.0 files/Files#column_number().(id, line_index, byte_index)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 182 | }) |
| 183 | } |
| 184 | |
| 185 | /// The byte range of line in the source of the file. |
| 186 | fn line_rangerust-analyzer cargo codespan-reporting 0.12.0 files/Files#line_range().(&'a self, id: Self::FileIdrust-analyzer cargo codespan-reporting 0.12.0 files/Files#FileId#, line_index: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#<usize>, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#>; |
| 187 | } |
| 188 | |
| 189 | /// A user-facing location in a source file. |
| 190 | /// |
| 191 | /// Returned by [`Files::location`]. |
| 192 | /// |
| 193 | /// [`Files::location`]: Files::location |
| 194 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(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:, 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:)] |
| 195 | pub struct Locationrust-analyzer cargo codespan-reporting 0.12.0 files/Location# { |
| 196 | /// The user-facing line number. |
| 197 | pub line_numberrust-analyzer cargo codespan-reporting 0.12.0 files/Location#line_number.: usize, |
| 198 | /// The user-facing column number. |
| 199 | pub column_numberrust-analyzer cargo codespan-reporting 0.12.0 files/Location#column_number.: usize, |
| 200 | } |
| 201 | |
| 202 | /// The column index at the given byte index in the source file. |
| 203 | /// This is the number of characters to the given byte index. |
| 204 | /// |
| 205 | /// If the byte index is smaller than the start of the line, then `0` is returned. |
| 206 | /// If the byte index is past the end of the line, the column index of the last |
| 207 | /// character `+ 1` is returned. |
| 208 | /// |
| 209 | /// # Example |
| 210 | /// |
| 211 | /// ```rust |
| 212 | /// use codespan_reporting::files; |
| 213 | /// |
| 214 | /// let source = "\n\n🗻∈🌏\n\n"; |
| 215 | /// |
| 216 | /// assert_eq!(files::column_index(source, 0..1, 0), 0); |
| 217 | /// assert_eq!(files::column_index(source, 2..13, 0), 0); |
| 218 | /// assert_eq!(files::column_index(source, 2..13, 2 + 0), 0); |
| 219 | /// assert_eq!(files::column_index(source, 2..13, 2 + 1), 0); |
| 220 | /// assert_eq!(files::column_index(source, 2..13, 2 + 4), 1); |
| 221 | /// assert_eq!(files::column_index(source, 2..13, 2 + 8), 2); |
| 222 | /// assert_eq!(files::column_index(source, 2..13, 2 + 10), 2); |
| 223 | /// assert_eq!(files::column_index(source, 2..13, 2 + 11), 3); |
| 224 | /// assert_eq!(files::column_index(source, 2..13, 2 + 12), 3); |
| 225 | /// ``` |
| 226 | pub fn column_indexrust-analyzer cargo codespan-reporting 0.12.0 files/column_index().(source: &str, line_range: Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#<usize>, byte_index: usize) -> usize { |
| 227 | let end_index = corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::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().(byte_index, corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::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().(line_range.endrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end., source.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().())); |
| 228 | |
| 229 | (line_range.startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#start...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end_index) |
| 230 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|byte_index| source.is_char_boundaryrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_char_boundary().(byte_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[`&usize`][`Add<usize>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[`&usize`][`Add<usize>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[`&usize`][`Add<usize>`]add().1)) |
| 231 | .countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter/impl#[`Filter<I, P>`][Iterator]count().() |
| 232 | } |
| 233 | |
| 234 | /// Return the starting byte index of each line in the source string. |
| 235 | /// |
| 236 | /// This can make it easier to implement [`Files::line_index`] by allowing |
| 237 | /// implementors of [`Files`] to pre-compute the line starts, then search for |
| 238 | /// the corresponding line range, as shown in the example below. |
| 239 | /// |
| 240 | /// [`Files`]: Files |
| 241 | /// [`Files::line_index`]: Files::line_index |
| 242 | /// |
| 243 | /// # Example |
| 244 | /// |
| 245 | /// ```rust |
| 246 | /// use codespan_reporting::files; |
| 247 | /// |
| 248 | /// let source = "foo\nbar\r\n\nbaz"; |
| 249 | /// let line_starts: Vec<_> = files::line_starts(source).collect(); |
| 250 | /// |
| 251 | /// assert_eq!( |
| 252 | /// line_starts, |
| 253 | /// [ |
| 254 | /// 0, // "foo\n" |
| 255 | /// 4, // "bar\r\n" |
| 256 | /// 9, // "" |
| 257 | /// 10, // "baz" |
| 258 | /// ], |
| 259 | /// ); |
| 260 | /// |
| 261 | /// fn line_index(line_starts: &[usize], byte_index: usize) -> Option<usize> { |
| 262 | /// match line_starts.binary_search(&byte_index) { |
| 263 | /// Ok(line) => Some(line), |
| 264 | /// Err(next_line) => Some(next_line - 1), |
| 265 | /// } |
| 266 | /// } |
| 267 | /// |
| 268 | /// assert_eq!(line_index(&line_starts, 5), Some(1)); |
| 269 | /// ``` |
| 270 | // NOTE: this is copied in `codespan::file::line_starts` and should be kept in sync. |
| 271 | pub fn line_startsrust-analyzer cargo codespan-reporting 0.12.0 files/line_starts().(source: &str) -> impl '_ + Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = usize> { |
| 272 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::oncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/once/once().(0).chainrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#chain().(source.match_indicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]match_indices().('\n').maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(i, _)| i 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)) |
| 273 | } |
| 274 | |
| 275 | /// A file database that contains a single source file. |
| 276 | /// |
| 277 | /// Because there is only single file in this database we use `()` as a [`FileId`]. |
| 278 | /// |
| 279 | /// This is useful for simple language tests, but it might be worth creating a |
| 280 | /// custom implementation when a language scales beyond a certain size. |
| 281 | /// |
| 282 | /// [`FileId`]: Files::FileId |
| 283 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 284 | pub struct SimpleFilerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#<Name, Source> { |
| 285 | /// The name of the file. |
| 286 | namerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#name.: Name, |
| 287 | /// The source code of the file. |
| 288 | sourcerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#source.: Source, |
| 289 | /// The starting byte indices in the source code. |
| 290 | line_startsrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#line_starts.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<usize>, |
| 291 | } |
| 292 | |
| 293 | impl<Name, Source> SimpleFilerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#<Name, Source> |
| 294 | where |
| 295 | Name: corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 296 | Source: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<str>, |
| 297 | { |
| 298 | /// Create a new source file. |
| 299 | pub fn newrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`]new().(name: Name, source: Source) -> SimpleFilerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#<Name, Source> { |
| 300 | SimpleFilerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile# { |
| 301 | namerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#name., |
| 302 | line_startsrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#line_starts.: line_startsrust-analyzer cargo codespan-reporting 0.12.0 files/line_starts().(source.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 303 | sourcerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#source., |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /// Return the name of the file. |
| 308 | pub fn namerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`]name().(&self) -> &Name { |
| 309 | &self.namerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#name. |
| 310 | } |
| 311 | |
| 312 | /// Return the source of the file. |
| 313 | pub fn sourcerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`]source().(&self) -> &Source { |
| 314 | &self.sourcerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#source. |
| 315 | } |
| 316 | |
| 317 | /// Return the starting byte index of the line with the specified line index. |
| 318 | /// Convenience method that already generates errors if necessary. |
| 319 | fn line_startrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`]line_start().(&self, line_index: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#> { |
| 320 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::cmprust-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#; |
| 321 | |
| 322 | match line_index.cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][Ord]cmp().(&self.line_startsrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#line_starts..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()) { |
| 323 | 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# => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self |
| 324 | .line_startsrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#line_starts. |
| 325 | .getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(line_index) |
| 326 | .clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<&T>`]cloned().() |
| 327 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("failed despite previous check")), |
| 328 | 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# => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.sourcerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#source..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().()), |
| 329 | 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# => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#::LineTooLargerust-analyzer cargo codespan-reporting 0.12.0 files/Error#LineTooLarge# { |
| 330 | givenrust-analyzer cargo codespan-reporting 0.12.0 files/Error#LineTooLarge#given.: line_index, |
| 331 | maxrust-analyzer cargo codespan-reporting 0.12.0 files/Error#LineTooLarge#max.: self.line_startsrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#line_starts..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() 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, |
| 332 | }), |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | impl<'a, Name, Source> Filesrust-analyzer cargo codespan-reporting 0.12.0 files/Files#<'a> for SimpleFilerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#<Name, Source> |
| 338 | where |
| 339 | Name: 'a + corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 340 | Source: 'a + AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<str>, |
| 341 | { |
| 342 | type FileIdrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`][`Files<'a>`]FileId# = (); |
| 343 | type Namerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`][`Files<'a>`]Name# = Name; |
| 344 | type Sourcerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`][`Files<'a>`]Source# = &'a str; |
| 345 | |
| 346 | fn namerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`][`Files<'a>`]name().(&self, (): ()) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Name, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#> { |
| 347 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.namerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#name..clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().()) |
| 348 | } |
| 349 | |
| 350 | fn sourcerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`][`Files<'a>`]source().(&self, (): ()) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&str, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#> { |
| 351 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.sourcerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#source..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()) |
| 352 | } |
| 353 | |
| 354 | fn line_indexrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`][`Files<'a>`]line_index().(&self, (): (), byte_index: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#> { |
| 355 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self |
| 356 | .line_startsrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#line_starts. |
| 357 | .binary_searchrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]binary_search().(&byte_index) |
| 358 | .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(|next_line| next_line 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)) |
| 359 | } |
| 360 | |
| 361 | fn line_rangerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`][`Files<'a>`]line_range().(&self, (): (), line_index: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#<usize>, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#> { |
| 362 | let line_start = self.line_startrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`]line_start().(line_index)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 363 | let next_line_start = self.line_startrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`]line_start().(line_index 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)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 364 | |
| 365 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(line_start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#next_line_start) |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /// A file database that can store multiple source files. |
| 370 | /// |
| 371 | /// This is useful for simple language tests, but it might be worth creating a |
| 372 | /// custom implementation when a language scales beyond a certain size. |
| 373 | /// It is a glorified `Vec<SimpleFile>` that implements the `Files` trait. |
| 374 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 375 | pub struct SimpleFilesrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFiles#<Name, Source> { |
| 376 | filesrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFiles#files.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<SimpleFilerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#<Name, Source>>, |
| 377 | } |
| 378 | |
| 379 | impl<Name, Source> SimpleFilesrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFiles#<Name, Source> |
| 380 | where |
| 381 | Name: corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 382 | Source: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<str>, |
| 383 | { |
| 384 | /// Create a new files database. |
| 385 | pub fn newrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`]new().() -> SimpleFilesrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFiles#<Name, Source> { |
| 386 | SimpleFilesrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFiles# { filesrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFiles#files.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().() } |
| 387 | } |
| 388 | |
| 389 | /// Add a file to the database, returning the handle that can be used to |
| 390 | /// refer to it again. |
| 391 | pub fn addrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`]add().(&mut self, name: Name, source: Source) -> usize { |
| 392 | let file_id = self.filesrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFiles#files..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 393 | self.filesrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFiles#files..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(SimpleFilerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#::newrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`]new().(name, source)); |
| 394 | file_id |
| 395 | } |
| 396 | |
| 397 | /// Get the file corresponding to the given id. |
| 398 | pub fn getrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`]get().(&self, file_id: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&SimpleFilerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#<Name, Source>, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#> { |
| 399 | self.filesrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFiles#files..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(file_id).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#::FileMissingrust-analyzer cargo codespan-reporting 0.12.0 files/Error#FileMissing#) |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | impl<'a, Name, Source> Filesrust-analyzer cargo codespan-reporting 0.12.0 files/Files#<'a> for SimpleFilesrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFiles#<Name, Source> |
| 404 | where |
| 405 | Name: 'a + corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 406 | Source: 'a + AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<str>, |
| 407 | { |
| 408 | type FileIdrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`][`Files<'a>`]FileId# = usize; |
| 409 | type Namerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`][`Files<'a>`]Name# = Name; |
| 410 | type Sourcerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`][`Files<'a>`]Source# = &'a str; |
| 411 | |
| 412 | fn namerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`][`Files<'a>`]name().(&self, file_id: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Name, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#> { |
| 413 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.getrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`]get().(file_id)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..namerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`]name().().clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().()) |
| 414 | } |
| 415 | |
| 416 | fn sourcerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`][`Files<'a>`]source().(&self, file_id: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&str, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#> { |
| 417 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.getrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`]get().(file_id)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..sourcerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`]source().().as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()) |
| 418 | } |
| 419 | |
| 420 | fn line_indexrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`][`Files<'a>`]line_index().(&self, file_id: usize, byte_index: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#> { |
| 421 | self.getrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`]get().(file_id)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..line_indexrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`][`Files<'a>`]line_index().((), byte_index) |
| 422 | } |
| 423 | |
| 424 | fn line_rangerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`][`Files<'a>`]line_range().(&self, file_id: usize, line_index: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#<usize>, Errorrust-analyzer cargo codespan-reporting 0.12.0 files/Error#> { |
| 425 | self.getrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFiles<Name, Source>`]get().(file_id)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..line_rangerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`][`Files<'a>`]line_range().((), line_index) |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | #[cfg(test)] |
| 430 | mod testrust-analyzer cargo codespan-reporting 0.12.0 files/test/ { |
| 431 | use superrust-analyzer cargo codespan-reporting 0.12.0 files/::*; |
| 432 | |
| 433 | const TEST_SOURCErust-analyzer cargo codespan-reporting 0.12.0 files/test/TEST_SOURCE.: &str = "foo\nbar\r\n\nbaz"; |
| 434 | |
| 435 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 436 | fn line_startsrust-analyzer cargo codespan-reporting 0.12.0 files/test/line_starts().() { |
| 437 | let file = SimpleFilerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#::newrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`]new().("test", TEST_SOURCErust-analyzer cargo codespan-reporting 0.12.0 files/test/TEST_SOURCE.); |
| 438 | |
| 439 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 440 | file.line_startsrust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#line_starts., |
| 441 | [ |
| 442 | 0, // "foo\n" |
| 443 | 4, // "bar\r\n" |
| 444 | 9, // "" |
| 445 | 10, // "baz" |
| 446 | ], |
| 447 | ); |
| 448 | } |
| 449 | |
| 450 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 451 | fn line_span_sourcesrust-analyzer cargo codespan-reporting 0.12.0 files/test/line_span_sources().() { |
| 452 | let file = SimpleFilerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#::newrust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`]new().("test", TEST_SOURCErust-analyzer cargo codespan-reporting 0.12.0 files/test/TEST_SOURCE.); |
| 453 | |
| 454 | let line_sources = (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#4) |
| 455 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|line| { |
| 456 | let line_range = file.line_rangerust-analyzer cargo codespan-reporting 0.12.0 files/impl#[`SimpleFile<Name, Source>`][`Files<'a>`]line_range().((), line).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 457 | &file.sourcerust-analyzer cargo codespan-reporting 0.12.0 files/SimpleFile#source.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().line_range]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). |
| 458 | }) |
| 459 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(); |
| 460 | |
| 461 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(line_sources, ["foo\n", "bar\r\n", "\n", "baz"]); |
| 462 | } |
| 463 | } |