From 3374c73fba3a96df22d0c04e6c17078ca8cce45b Mon Sep 17 00:00:00 2001 From: Valentin Anger Date: Sun, 12 Jul 2020 14:16:33 +0200 Subject: feat(doc): Improve terminal printer (#6594) - Add more support for generics - Add the --private flag - displays documentation for not exported and private nodes - Display more attributes like abstract, static and readonly - Display type aliases - Refactor module to use the Display trait - Use a bit more color --- cli/doc/display.rs | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 cli/doc/display.rs (limited to 'cli/doc/display.rs') diff --git a/cli/doc/display.rs b/cli/doc/display.rs new file mode 100644 index 000000000..9da04363f --- /dev/null +++ b/cli/doc/display.rs @@ -0,0 +1,90 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +use crate::colors; +use crate::swc_ecma_ast; +use std::fmt::{Display, Formatter, Result}; + +pub(crate) struct Indent(pub i64); + +impl Display for Indent { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + for _ in 0..self.0 { + write!(f, " ")?; + } + Ok(()) + } +} + +pub(crate) struct SliceDisplayer<'a, T: Display>(&'a [T], &'a str, bool); + +impl<'a, T: Display> SliceDisplayer<'a, T> { + pub fn new( + slice: &'a [T], + separator: &'a str, + trailing: bool, + ) -> SliceDisplayer<'a, T> { + SliceDisplayer(slice, separator, trailing) + } +} + +impl Display for SliceDisplayer<'_, T> { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + if self.0.is_empty() { + return Ok(()); + } + + write!(f, "{}", self.0[0])?; + for v in &self.0[1..] { + write!(f, "{}{}", self.1, v)?; + } + + if self.2 { + write!(f, "{}", self.1)?; + } + + Ok(()) + } +} + +pub(crate) fn display_abstract(is_abstract: bool) -> impl Display { + colors::magenta(if is_abstract { "abstract " } else { "" }) +} + +pub(crate) fn display_accessibility( + accessibility: Option, +) -> impl Display { + colors::magenta( + match accessibility.unwrap_or(swc_ecma_ast::Accessibility::Public) { + swc_ecma_ast::Accessibility::Public => "", + swc_ecma_ast::Accessibility::Protected => "protected ", + swc_ecma_ast::Accessibility::Private => "private ", + }, + ) +} + +pub(crate) fn display_async(is_async: bool) -> impl Display { + colors::magenta(if is_async { "async " } else { "" }) +} + +pub(crate) fn display_generator(is_generator: bool) -> impl Display { + colors::magenta(if is_generator { "*" } else { "" }) +} + +pub(crate) fn display_method(method: swc_ecma_ast::MethodKind) -> impl Display { + colors::magenta(match method { + swc_ecma_ast::MethodKind::Getter => "get ", + swc_ecma_ast::MethodKind::Setter => "set ", + _ => "", + }) +} + +pub(crate) fn display_optional(is_optional: bool) -> impl Display { + colors::magenta(if is_optional { "?" } else { "" }) +} + +pub(crate) fn display_readonly(is_readonly: bool) -> impl Display { + colors::magenta(if is_readonly { "readonly " } else { "" }) +} + +pub(crate) fn display_static(is_static: bool) -> impl Display { + colors::magenta(if is_static { "static " } else { "" }) +} -- cgit v1.2.3