1 // Copyright (c) Facebook, Inc. and its affiliates.
3 // This source code is licensed under the MIT license found in the
4 // LICENSE file in the "hack" directory of this source tree.
10 use relative_path::RelativePath;
12 /// Indent is an abstraction of indentation. Configurable indentation
13 /// and perf tweaking will be easier.
31 impl fmt::Display for Indent {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 pub struct Context<'a> {
42 pub(crate) path: Option<&'a RelativePath>,
46 impl<'a> Context<'a> {
47 pub fn new(path: Option<&'a RelativePath>) -> Self {
50 indent: Indent::new(),
54 /// Insert a newline with indentation
55 pub(crate) fn newline(&self, w: &mut dyn Write) -> Result<()> {
56 write!(w, "\n{}", self.indent)
59 /// Start a new indented block
60 pub(crate) fn block<F>(&self, w: &mut dyn Write, f: F) -> Result<()>
62 F: FnOnce(&Self, &mut dyn Write) -> Result<()>,
64 let mut ctx = self.clone();
69 pub(crate) fn unblock<F>(&self, w: &mut dyn Write, f: F) -> Result<()>
71 F: FnOnce(&Self, &mut dyn Write) -> Result<()>,
73 let mut ctx = self.clone();
78 /// Printing instruction list requires manually control indentation,
79 /// where indent_inc/indent_dec are called
80 pub(crate) fn indent_inc(&mut self) {
84 pub(crate) fn indent_dec(&mut self) {
89 pub(crate) struct FmtIndent<'a>(pub(crate) &'a Context<'a>);
91 impl fmt::Display for FmtIndent<'_> {
92 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93 self.0.indent.fmt(f)?;
98 write_bytes::display_bytes_using_display!(FmtIndent<'_>);