|
| 1 | +// Copyright 2016 The RLS Project Developers. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | +// option. This file may not be copied, modified, or distributed |
| 7 | +// except according to those terms. |
| 8 | + |
| 9 | +/// These are the structures emitted by the compiler as part of JSON errors. |
| 10 | +/// The original source can be found at |
| 11 | +/// https://github.com/rust-lang/rust/blob/master/src/libsyntax/json.rs |
| 12 | +
|
| 13 | +use std::path::PathBuf; |
| 14 | + |
| 15 | +use {Span, Row, Column, OneIndexed}; |
| 16 | + |
| 17 | +#[cfg_attr(feature = "serialize-serde", derive(Deserialize))] |
| 18 | +#[derive(Debug, Clone)] |
| 19 | +pub struct DiagnosticSpan { |
| 20 | + pub file_name: String, |
| 21 | + pub byte_start: u32, |
| 22 | + pub byte_end: u32, |
| 23 | + /// 1-based. |
| 24 | + pub line_start: usize, |
| 25 | + pub line_end: usize, |
| 26 | + /// 1-based, character offset. |
| 27 | + pub column_start: usize, |
| 28 | + pub column_end: usize, |
| 29 | + /// Is this a "primary" span -- meaning the point, or one of the points, |
| 30 | + /// where the error occurred? |
| 31 | + pub is_primary: bool, |
| 32 | + /// Source text from the start of line_start to the end of line_end. |
| 33 | + pub text: Vec<DiagnosticSpanLine>, |
| 34 | + /// Label that should be placed at this location (if any) |
| 35 | + pub label: Option<String>, |
| 36 | + /// If we are suggesting a replacement, this will contain text |
| 37 | + /// that should be sliced in atop this span. You may prefer to |
| 38 | + /// load the fully rendered version from the parent `Diagnostic`, |
| 39 | + /// however. |
| 40 | + pub suggested_replacement: Option<String>, |
| 41 | + /// Macro invocations that created the code at this span, if any. |
| 42 | + pub expansion: Option<Box<DiagnosticSpanMacroExpansion>>, |
| 43 | +} |
| 44 | + |
| 45 | +impl DiagnosticSpan { |
| 46 | + pub fn rls_span(&self) -> Span<OneIndexed> { |
| 47 | + Span::new( |
| 48 | + Row::new(self.line_start as u32), |
| 49 | + Row::new(self.line_end as u32), |
| 50 | + Column::new(self.column_start as u32), |
| 51 | + Column::new(self.column_end as u32), |
| 52 | + PathBuf::from(&self.file_name), |
| 53 | + ) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#[cfg_attr(feature = "serialize-serde", derive(Deserialize))] |
| 58 | +#[derive(Debug, Clone)] |
| 59 | +pub struct DiagnosticSpanLine { |
| 60 | + pub text: String, |
| 61 | + |
| 62 | + /// 1-based, character offset in self.text. |
| 63 | + pub highlight_start: usize, |
| 64 | + |
| 65 | + pub highlight_end: usize, |
| 66 | +} |
| 67 | + |
| 68 | +#[cfg_attr(feature = "serialize-serde", derive(Deserialize))] |
| 69 | +#[derive(Debug, Clone)] |
| 70 | +pub struct DiagnosticSpanMacroExpansion { |
| 71 | + /// span where macro was applied to generate this code; note that |
| 72 | + /// this may itself derive from a macro (if |
| 73 | + /// `span.expansion.is_some()`) |
| 74 | + pub span: DiagnosticSpan, |
| 75 | + |
| 76 | + /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]") |
| 77 | + pub macro_decl_name: String, |
| 78 | + |
| 79 | + /// span where macro was defined (if known) |
| 80 | + pub def_site_span: Option<DiagnosticSpan>, |
| 81 | +} |
0 commit comments