feat(swc_common): Expose non-tty EmitterWriter (#1240)

swc_bundler:
 - Expose non-tty EmitterWriter
This commit is contained in:
강동윤 2020-11-29 01:37:09 +09:00 committed by GitHub
parent cdaefcc27e
commit 46b553ecc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 52 deletions

View File

@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_common"
repository = "https://github.com/swc-project/swc.git"
version = "0.10.5"
version = "0.10.6"
[features]
concurrent = ["parking_lot"]

View File

@ -7,13 +7,12 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[cfg(feature = "tty-emitter")]
pub use self::emitter::EmitterWriter;
use self::Level::*;
pub use self::{
diagnostic::{Diagnostic, DiagnosticId, DiagnosticStyledString, SubDiagnostic},
diagnostic_builder::DiagnosticBuilder,
emitter::{ColorConfig, Emitter},
emitter::{ColorConfig, Emitter, EmitterWriter},
};
#[cfg(feature = "tty-emitter")]
use crate::sync::Lrc;

View File

@ -8,40 +8,29 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[cfg(feature = "tty-emitter")]
use self::Destination::*;
#[cfg(feature = "tty-emitter")]
use super::snippet::{Annotation, Line};
use super::DiagnosticBuilder;
#[cfg(feature = "tty-emitter")]
use super::{
snippet::{AnnotationType, MultilineAnnotation, Style, StyledString},
snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, Style, StyledString},
styled_buffer::StyledBuffer,
CodeSuggestion, DiagnosticId, Level, SourceMapperDyn, SubDiagnostic,
CodeSuggestion, DiagnosticBuilder, DiagnosticId, Level, SourceMapperDyn, SubDiagnostic,
};
use crate::{
sync::Lrc,
syntax_pos::{MultiSpan, SourceFile, Span},
};
#[cfg(feature = "tty-emitter")]
use crate::sync::Lrc;
#[cfg(feature = "tty-emitter")]
use crate::syntax_pos::SourceFile;
#[cfg(feature = "tty-emitter")]
use crate::syntax_pos::{MultiSpan, Span};
#[cfg(feature = "tty-emitter")]
use atty;
#[cfg(feature = "tty-emitter")]
use std::borrow::Cow;
#[cfg(feature = "tty-emitter")]
use std::cmp::{min, Reverse};
#[cfg(feature = "tty-emitter")]
use std::collections::HashMap;
use std::io::{self, prelude::*};
#[cfg(feature = "tty-emitter")]
use std::ops::Range;
use std::{
borrow::Cow,
cmp::{min, Reverse},
collections::HashMap,
io::{self, prelude::*},
ops::Range,
};
#[cfg(feature = "tty-emitter")]
use termcolor::{Buffer, BufferWriter, Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
#[cfg(feature = "tty-emitter")]
use unicode_width;
#[cfg(feature = "tty-emitter")]
const ANONYMIZED_LINE_NUM: &str = "LL";
/// Emitter trait for emitting errors.
@ -55,7 +44,6 @@ pub trait Emitter: crate::sync::Send {
}
}
#[cfg(feature = "tty-emitter")]
impl Emitter for EmitterWriter {
fn emit(&mut self, db: &DiagnosticBuilder<'_>) {
let mut primary_span = db.span.clone();
@ -144,7 +132,6 @@ impl ColorConfig {
}
}
#[cfg(feature = "tty-emitter")]
pub struct EmitterWriter {
dst: Destination,
sm: Option<Lrc<SourceMapperDyn>>,
@ -153,7 +140,6 @@ pub struct EmitterWriter {
ui_testing: bool,
}
#[cfg(feature = "tty-emitter")]
struct FileWithAnnotatedLines {
file: Lrc<SourceFile>,
lines: Vec<Line>,
@ -177,7 +163,9 @@ impl EmitterWriter {
ui_testing: false,
}
}
}
impl EmitterWriter {
pub fn new(
dst: Box<dyn Write + Send>,
source_map: Option<Lrc<SourceMapperDyn>>,
@ -1395,17 +1383,14 @@ impl EmitterWriter {
}
}
#[cfg(feature = "tty-emitter")]
fn draw_col_separator(buffer: &mut StyledBuffer, line: usize, col: usize) {
buffer.puts(line, col, "| ", Style::LineNumber);
}
#[cfg(feature = "tty-emitter")]
fn draw_col_separator_no_space(buffer: &mut StyledBuffer, line: usize, col: usize) {
draw_col_separator_no_space_with_style(buffer, line, col, Style::LineNumber);
}
#[cfg(feature = "tty-emitter")]
fn draw_col_separator_no_space_with_style(
buffer: &mut StyledBuffer,
line: usize,
@ -1415,7 +1400,6 @@ fn draw_col_separator_no_space_with_style(
buffer.putc(line, col, '|', style);
}
#[cfg(feature = "tty-emitter")]
fn draw_range(
buffer: &mut StyledBuffer,
symbol: char,
@ -1429,12 +1413,10 @@ fn draw_range(
}
}
#[cfg(feature = "tty-emitter")]
fn draw_note_separator(buffer: &mut StyledBuffer, line: usize, col: usize) {
buffer.puts(line, col, "= ", Style::LineNumber);
}
#[cfg(feature = "tty-emitter")]
fn draw_multiline_line(
buffer: &mut StyledBuffer,
line: usize,
@ -1445,7 +1427,6 @@ fn draw_multiline_line(
buffer.putc(line, offset + depth - 1, '|', style);
}
#[cfg(feature = "tty-emitter")]
fn num_overlap(
a_start: usize,
a_end: usize,
@ -1459,7 +1440,7 @@ fn num_overlap(
}
contains(b_start..b_end + extra, a_start) || contains(a_start..a_end + extra, b_start)
}
#[cfg(feature = "tty-emitter")]
fn overlaps(a1: &Annotation, a2: &Annotation, padding: usize) -> bool {
num_overlap(
a1.start_col,
@ -1470,7 +1451,6 @@ fn overlaps(a1: &Annotation, a2: &Annotation, padding: usize) -> bool {
)
}
#[cfg(feature = "tty-emitter")]
fn emit_to_destination(
rendered_buffer: &[Vec<StyledString>],
lvl: Level,
@ -1498,6 +1478,7 @@ fn emit_to_destination(
let _buffer_lock = lock::acquire_global_lock("rustc_errors");
for (pos, line) in rendered_buffer.iter().enumerate() {
for part in line {
#[cfg(feature = "tty-emitter")]
dst.apply_style(lvl, part.style)?;
write!(dst, "{}", part.text)?;
dst.reset()?;
@ -1543,7 +1524,6 @@ impl Destination {
}
}
#[cfg(feature = "tty-emitter")]
fn writable(&mut self) -> WritableDst<'_> {
match *self {
#[cfg(feature = "tty-emitter")]
@ -1607,16 +1587,19 @@ impl<'a> WritableDst<'a> {
#[cfg(feature = "tty-emitter")]
fn set_color(&mut self, color: &ColorSpec) -> io::Result<()> {
match *self {
#[cfg(feature = "tty-emitter")]
WritableDst::Terminal(ref mut t) => t.set_color(color),
#[cfg(feature = "tty-emitter")]
WritableDst::Buffered(_, ref mut t) => t.set_color(color),
WritableDst::Raw(_) => Ok(()),
}
}
#[cfg(feature = "tty-emitter")]
fn reset(&mut self) -> io::Result<()> {
match *self {
#[cfg(feature = "tty-emitter")]
WritableDst::Terminal(ref mut t) => t.reset(),
#[cfg(feature = "tty-emitter")]
WritableDst::Buffered(_, ref mut t) => t.reset(),
WritableDst::Raw(_) => Ok(()),
}

View File

@ -8,8 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![cfg(feature = "tty-emitter")]
//! Bindings to acquire a global named lock.
//!
//! This is intended to be used to synchronize multiple compiler processes to

View File

@ -12,14 +12,12 @@
use super::Level;
#[cfg(feature = "tty-emitter")]
#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
pub struct Line {
pub line_index: usize,
pub annotations: Vec<Annotation>,
}
#[cfg(feature = "tty-emitter")]
#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
pub struct MultilineAnnotation {
pub depth: usize,
@ -31,7 +29,6 @@ pub struct MultilineAnnotation {
pub label: Option<String>,
}
#[cfg(feature = "tty-emitter")]
impl MultilineAnnotation {
pub fn increase_depth(&mut self) {
self.depth += 1;
@ -68,7 +65,6 @@ impl MultilineAnnotation {
}
}
#[cfg(feature = "tty-emitter")]
#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
pub enum AnnotationType {
/// Annotation under a single line of code
@ -98,7 +94,6 @@ pub enum AnnotationType {
MultilineLine(usize),
}
#[cfg(feature = "tty-emitter")]
#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
pub struct Annotation {
/// Start column, 0-based indexing -- counting *characters*, not
@ -121,7 +116,6 @@ pub struct Annotation {
pub annotation_type: AnnotationType,
}
#[cfg(feature = "tty-emitter")]
impl Annotation {
/// Whether this annotation is a vertical line placeholder.
pub fn is_line(&self) -> bool {

View File

@ -10,8 +10,6 @@
// Code for creating styled buffers
#![cfg(feature = "tty-emitter")]
use super::snippet::{Style, StyledString};
#[derive(Debug)]