Skip to content

Commit

Permalink
feat(swc_common): Expose non-tty EmitterWriter (swc-project#1240)
Browse files Browse the repository at this point in the history
swc_bundler:
 - Expose non-tty EmitterWriter
  • Loading branch information
kdy1 authored Nov 28, 2020
1 parent cdaefcc commit 46b553e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 52 deletions.
2 changes: 1 addition & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
5 changes: 2 additions & 3 deletions common/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 21 additions & 38 deletions common/src/errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
Expand Down Expand Up @@ -144,7 +132,6 @@ impl ColorConfig {
}
}

#[cfg(feature = "tty-emitter")]
pub struct EmitterWriter {
dst: Destination,
sm: Option<Lrc<SourceMapperDyn>>,
Expand All @@ -153,7 +140,6 @@ pub struct EmitterWriter {
ui_testing: bool,
}

#[cfg(feature = "tty-emitter")]
struct FileWithAnnotatedLines {
file: Lrc<SourceFile>,
lines: Vec<Line>,
Expand All @@ -177,7 +163,9 @@ impl EmitterWriter {
ui_testing: false,
}
}
}

impl EmitterWriter {
pub fn new(
dst: Box<dyn Write + Send>,
source_map: Option<Lrc<SourceMapperDyn>>,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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()?;
Expand Down Expand Up @@ -1543,7 +1524,6 @@ impl Destination {
}
}

#[cfg(feature = "tty-emitter")]
fn writable(&mut self) -> WritableDst<'_> {
match *self {
#[cfg(feature = "tty-emitter")]
Expand Down Expand Up @@ -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(()),
}
Expand Down
2 changes: 0 additions & 2 deletions common/src/errors/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions common/src/errors/snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions common/src/errors/styled_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

// Code for creating styled buffers

#![cfg(feature = "tty-emitter")]

use super::snippet::{Style, StyledString};

#[derive(Debug)]
Expand Down

0 comments on commit 46b553e

Please sign in to comment.