Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add emit_single, which emits a Diagnostic given a single FileMap #56

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 36 additions & 2 deletions codespan-reporting/src/emitter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use codespan::{CodeMap, LineIndex, LineNumber};
use codespan::{ByteIndex, CodeMap, FileMap, LineIndex, LineNumber};
use std::{fmt, io};
use std::ops::Deref;
use termcolor::{Color, ColorSpec, WriteColor};

use {Diagnostic, LabelStyle};
Expand All @@ -15,10 +16,43 @@ impl<T: fmt::Display> fmt::Display for Pad<T> {
}
}

pub fn emit<W, S>(mut writer: W, codemap: &CodeMap<S>, diagnostic: &Diagnostic) -> io::Result<()>
pub fn emit<W, S>(writer: W, codemap: &CodeMap<S>, diagnostic: &Diagnostic) -> io::Result<()>
where
W: WriteColor,
S: AsRef<str>,
{
emit_internal(writer, codemap, diagnostic)
}

pub fn emit_single<W, S>(writer: W, filemap: &FileMap<S>, diagnostic: &Diagnostic) -> io::Result<()>
where
W: WriteColor,
S: AsRef<str>,
{
emit_internal(writer, filemap, diagnostic)
}

trait FindFile<S> {
fn find_file(&self, index: ByteIndex) -> Option<&FileMap<S>>;
}

impl<S: AsRef<str>> FindFile<S> for CodeMap<S> {
fn find_file(&self, index: ByteIndex) -> Option<&FileMap<S>> {
self.find_file(index).map(Deref::deref)
}
}

impl<S: AsRef<str>> FindFile<S> for FileMap<S> {
fn find_file(&self, _index: ByteIndex) -> Option<&FileMap<S>> {
Some(self)
}
}

fn emit_internal<W, S, M>(mut writer: W, codemap: &M, diagnostic: &Diagnostic) -> io::Result<()>
where
W: WriteColor,
S: AsRef<str>,
M: FindFile<S>,
{
let supports_color = writer.supports_color();
let line_location_color = ColorSpec::new()
Expand Down
2 changes: 1 addition & 1 deletion codespan-reporting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod diagnostic;
mod emitter;

pub use self::diagnostic::{Diagnostic, Label, LabelStyle};
pub use self::emitter::emit;
pub use self::emitter::{emit, emit_single};

/// A severity level for diagnostic messages
///
Expand Down