From 6bd2d4985132e680e27bd189870c399ca0f72ceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 26 Oct 2023 03:29:45 +0200 Subject: [PATCH] feat: print visibility specifier before a symbol (#359) --- examples/ddoc/main.rs | 6 ++++-- src/node.rs | 2 +- src/printer.rs | 30 +++++++++++++++++++++++------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/examples/ddoc/main.rs b/examples/ddoc/main.rs index 8b59857a..a1222364 100644 --- a/examples/ddoc/main.rs +++ b/examples/ddoc/main.rs @@ -50,10 +50,12 @@ fn main() { let matches = App::new("ddoc") .arg(Arg::with_name("source_file").required(true)) .arg(Arg::with_name("filter")) + .arg(Arg::with_name("private").long("private")) .get_matches(); let source_file = matches.value_of("source_file").unwrap(); let maybe_filter = matches.value_of("filter"); + let private = matches.is_present("private"); let source_file = ModuleSpecifier::from_directory_path(current_dir().unwrap()) .unwrap() @@ -74,7 +76,7 @@ fn main() { }, ) .await; - let parser = DocParser::new(graph, false, analyzer.as_capturing_parser()); + let parser = DocParser::new(graph, private, analyzer.as_capturing_parser()); let parse_result = parser.parse_with_reexports(&source_file); let mut doc_nodes = match parse_result { @@ -89,7 +91,7 @@ fn main() { if let Some(filter) = maybe_filter { doc_nodes = find_nodes_by_name_recursively(doc_nodes, filter.to_string()); } - let result = DocPrinter::new(&doc_nodes, true, false); + let result = DocPrinter::new(&doc_nodes, true, private); println!("{}", result); }; diff --git a/src/node.rs b/src/node.rs index 70d137be..7e8ec6a0 100644 --- a/src/node.rs +++ b/src/node.rs @@ -63,7 +63,7 @@ pub struct ImportDef { pub imported: Option, } -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] #[serde(rename_all = "camelCase")] pub enum DeclarationKind { Private, diff --git a/src/printer.rs b/src/printer.rs index 5f02bd1a..0bebf64b 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -18,6 +18,7 @@ use crate::display::Indent; use crate::display::SliceDisplayer; use crate::js_doc::JsDoc; use crate::js_doc::JsDocTag; +use crate::node::DeclarationKind; use crate::node::DocNode; use crate::node::DocNodeKind; @@ -491,8 +492,9 @@ impl<'a> DocPrinter<'a> { } write!( w, - "{}{}{} {}", + "{}{}{}{} {}", Indent(indent), + fmt_visibility(node.declaration_kind), display_abstract(class_def.is_abstract), colors::magenta("class"), colors::bold(&node.name), @@ -536,8 +538,9 @@ impl<'a> DocPrinter<'a> { ) -> FmtResult { writeln!( w, - "{}{} {}", + "{}{}{} {}", Indent(indent), + fmt_visibility(node.declaration_kind), colors::magenta("enum"), colors::bold(&node.name) ) @@ -554,8 +557,9 @@ impl<'a> DocPrinter<'a> { if !has_overloads || !function_def.has_body { write!( w, - "{}{}{}{} {}", + "{}{}{}{}{} {}", Indent(indent), + fmt_visibility(node.declaration_kind), display_async(function_def.is_async), colors::magenta("function"), display_generator(function_def.is_generator), @@ -590,8 +594,9 @@ impl<'a> DocPrinter<'a> { let interface_def = node.interface_def.as_ref().unwrap(); write!( w, - "{}{} {}", + "{}{}{} {}", Indent(indent), + fmt_visibility(node.declaration_kind), colors::magenta("interface"), colors::bold(&node.name) )?; @@ -636,8 +641,9 @@ impl<'a> DocPrinter<'a> { let type_alias_def = node.type_alias_def.as_ref().unwrap(); write!( w, - "{}{} {}", + "{}{}{} {}", Indent(indent), + fmt_visibility(node.declaration_kind), colors::magenta("type"), colors::bold(&node.name), )?; @@ -661,8 +667,9 @@ impl<'a> DocPrinter<'a> { ) -> FmtResult { writeln!( w, - "{}{} {}", + "{}{}{} {}", Indent(indent), + fmt_visibility(node.declaration_kind), colors::magenta("namespace"), colors::bold(&node.name) ) @@ -677,8 +684,9 @@ impl<'a> DocPrinter<'a> { let variable_def = node.variable_def.as_ref().unwrap(); write!( w, - "{}{} {}", + "{}{}{} {}", Indent(indent), + fmt_visibility(node.declaration_kind), colors::magenta(match variable_def.kind { deno_ast::swc::ast::VarDeclKind::Const => "const", deno_ast::swc::ast::VarDeclKind::Let => "let", @@ -698,3 +706,11 @@ impl<'a> Display for DocPrinter<'a> { self.format(f) } } + +fn fmt_visibility(decl_kind: DeclarationKind) -> impl std::fmt::Display { + colors::italic_gray(if decl_kind == DeclarationKind::Private { + "private " + } else { + "" + }) +}