Skip to content

Commit

Permalink
Add codespan::Files argument to ToDiagnostic
Browse files Browse the repository at this point in the history
This argument is necessary for `codespan_lsp::byte_span_to_range()` and
also for fetching the source text for a given token.
  • Loading branch information
ebkalderon committed Mar 27, 2020
1 parent 2f05fb2 commit b83ce51
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions nix-parser2/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::fmt::{self, Display, Formatter};
use std::slice::Iter;

use codespan::FileId;
use codespan::{FileId, Files};
use codespan_reporting::diagnostic::Diagnostic;
use lsp_types::Diagnostic as LspDiagnostic;
use smallvec::SmallVec;
Expand All @@ -14,7 +14,7 @@ use smallvec::SmallVec;
/// produced using this interface.
pub trait ToDiagnostic<D> {
/// Converts the error to a diagnostic `D` for the given source file specified by `file_id`.
fn to_diagnostic(&self, file_id: FileId) -> D;
fn to_diagnostic<S: AsRef<str>>(&self, file_id: FileId, files: &Files<S>) -> D;
}

/// A generic growable stack for accumulating errors.
Expand Down Expand Up @@ -75,8 +75,15 @@ where
///
/// [`Diagnostic`]: https://docs.rs/codespan-reporting/0.9.1/codespan_reporting/diagnostic/struct.Diagnostic.html
#[inline]
pub fn to_diagnostics(&self, file_id: FileId) -> impl Iterator<Item = Diagnostic<FileId>> + '_ {
self.iter().map(move |e| e.to_diagnostic(file_id))
pub fn to_diagnostics<'a, S>(
&'a self,
file_id: FileId,
files: &'a Files<S>,
) -> impl Iterator<Item = Diagnostic<FileId>> + 'a
where
S: AsRef<str>,
{
self.iter().map(move |e| e.to_diagnostic(file_id, files))
}
}

Expand All @@ -88,8 +95,15 @@ where
///
/// [`Diagnostic`]: https://docs.rs/lsp-types/0.73.0/lsp_types/struct.Diagnostic.html
#[inline]
pub fn to_lsp_diagnostics(&self, file_id: FileId) -> impl Iterator<Item = LspDiagnostic> + '_ {
self.iter().map(move |e| e.to_diagnostic(file_id))
pub fn to_lsp_diagnostics<'a, S>(
&'a self,
file_id: FileId,
files: &'a Files<S>,
) -> impl Iterator<Item = LspDiagnostic> + 'a
where
S: AsRef<str>,
{
self.iter().map(move |e| e.to_diagnostic(file_id, files))
}
}

Expand Down

0 comments on commit b83ce51

Please sign in to comment.