Remove filespan type

This commit is contained in:
Brendan Zabarauskas
2019-06-04 18:39:10 +10:00
parent fe90da1686
commit b585e8dfce
6 changed files with 23 additions and 33 deletions
+4 -4
View File
@@ -169,16 +169,16 @@ pub fn make_lsp_diagnostic(
mut correlate_file_url: impl FnMut(FileId, &str) -> Result<Url, ()>,
) -> Result<lsp::Diagnostic, Error> {
// We need a position for the primary error so take the span from the first primary label
let primary_file_id = diagnostic.primary_label.file_span.id;
let primary_span = diagnostic.primary_label.file_span.span;
let primary_file_id = diagnostic.primary_label.file_id;
let primary_span = diagnostic.primary_label.span;
let primary_label_range = byte_span_to_range(files, primary_file_id, primary_span)?;
let related_information = diagnostic
.secondary_labels
.into_iter()
.map(|label| {
let file_id = label.file_span.id;
let range = byte_span_to_range(files, file_id, label.file_span.span)?;
let file_id = label.file_id;
let range = byte_span_to_range(files, file_id, label.span)?;
let uri = correlate_file_url(file_id, files.name(file_id))
.map_err(|()| Error::UnableToCorrelateFilename(files.name(file_id).to_owned()))?;
+6 -4
View File
@@ -1,6 +1,6 @@
use structopt::StructOpt;
use codespan::{FileSpan, Files, Span};
use codespan::{Files, Span};
use codespan_reporting::termcolor::StandardStream;
use codespan_reporting::{emit, ColorArg, Diagnostic, Label};
@@ -34,19 +34,21 @@ fn main() {
let error = Diagnostic::new_error(
"Unexpected type in `+` application",
Label::new(
FileSpan::new(file_id, Span::new(36, 38)),
file_id,
Span::new(36, 38),
"Expected integer but got string",
),
)
.with_code("E0001")
.with_secondary_labels(vec![Label::new(
FileSpan::new(file_id, Span::new(36, 38)),
file_id,
Span::new(36, 38),
"Expected integer but got string",
)]);
let warning = Diagnostic::new_warning(
"`+` function has no effect unless its result is used",
Label::new(FileSpan::new(file_id, Span::new(22, 49)), "Value discarded"),
Label::new(file_id, Span::new(22, 49), "Value discarded"),
);
let diagnostics = [error, warning];
+7 -4
View File
@@ -1,6 +1,6 @@
//! Diagnostic reporting support for the codespan crate
use codespan::FileSpan;
use codespan::{FileId, Span};
#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
@@ -57,17 +57,20 @@ impl PartialOrd for Severity {
#[cfg_attr(feature = "memory_usage", derive(heapsize_derive::HeapSizeOf))]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
pub struct Label {
/// The file that we are labelling.
pub file_id: FileId,
/// The span we are going to include in the final snippet.
pub file_span: FileSpan,
pub span: Span,
/// A message to provide some additional information for the underlined code.
pub message: String,
}
impl Label {
/// Create a new label.
pub fn new(file_span: FileSpan, message: impl Into<String>) -> Label {
pub fn new(file_id: FileId, span: Span, message: impl Into<String>) -> Label {
Label {
file_span,
file_id,
span,
message: message.into(),
}
}
+5 -5
View File
@@ -188,7 +188,7 @@ impl<'a> MarkedSource<'a> {
}
fn span(&self) -> Span {
self.label.file_span.span
self.label.span
}
fn start(&self) -> ByteIndex {
@@ -200,19 +200,19 @@ impl<'a> MarkedSource<'a> {
}
fn file_name(&self) -> &'a str {
self.files.name(self.label.file_span.id)
self.files.name(self.label.file_id)
}
fn location(&self, byte_index: ByteIndex) -> Result<Location, impl std::error::Error> {
self.files.location(self.label.file_span.id, byte_index)
self.files.location(self.label.file_id, byte_index)
}
fn source_slice(&self, span: Span) -> Result<&'a str, impl std::error::Error> {
self.files.source_slice(self.label.file_span.id, span)
self.files.source_slice(self.label.file_id, span)
}
fn line_span(&self, line_index: LineIndex) -> Result<Span, impl std::error::Error> {
self.files.line_span(self.label.file_span.id, line_index)
self.files.line_span(self.label.file_id, line_index)
}
fn label_color(&self, config: &Config) -> Color {
-15
View File
@@ -38,21 +38,6 @@ impl error::Error for SpanOutOfBoundsError {}
#[cfg_attr(feature = "memory_usage", derive(heapsize_derive::HeapSizeOf))]
pub struct FileId(u32);
/// A span that is situated in a source file.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "memory_usage", derive(heapsize_derive::HeapSizeOf))]
pub struct FileSpan {
pub id: FileId,
pub span: Span,
}
impl FileSpan {
pub fn new(id: FileId, span: Span) -> FileSpan {
FileSpan { id, span }
}
}
/// A database of source files.
#[derive(Debug, Clone)]
pub struct Files {
+1 -1
View File
@@ -16,7 +16,7 @@ mod index;
mod location;
mod span;
pub use crate::file::{FileId, FileSpan, Files};
pub use crate::file::{FileId, Files};
pub use crate::file::{LineIndexOutOfBoundsError, LocationError, SpanOutOfBoundsError};
pub use crate::index::{ByteIndex, ByteOffset};
pub use crate::index::{ColumnIndex, ColumnNumber, ColumnOffset};