Files
dioxus/packages/autofmt/src/buffer.rs
T
Alex Parrill 52fb080193 Use indentation settings in dx fmt and vscode extension
Adds the ability to specify an indent string to public autofmt methods - either
a sequence of spaces or a tab character.

Get the indentation style and size from rustfmt for dx fmt, or from the editor
settings for the vscode extension.

Closes #1595
2023-11-01 17:24:18 -04:00

64 lines
1.5 KiB
Rust

//! The output buffer that supports some helpful methods
//! These are separate from the input so we can lend references between the two
//!
//!
//!
use std::fmt::{Result, Write};
use dioxus_rsx::IfmtInput;
use crate::{indent::IndentOptions, write_ifmt};
/// The output buffer that tracks indent and string
#[derive(Debug, Default)]
pub struct Buffer {
pub buf: String,
pub indent_level: usize,
pub indent: IndentOptions,
}
impl Buffer {
// Create a new line and tab it to the current tab level
pub fn tabbed_line(&mut self) -> Result {
self.new_line()?;
self.tab()
}
// Create a new line and tab it to the current tab level
pub fn indented_tabbed_line(&mut self) -> Result {
self.new_line()?;
self.indented_tab()
}
pub fn tab(&mut self) -> Result {
self.write_tabs(self.indent_level)
}
pub fn indented_tab(&mut self) -> Result {
self.write_tabs(self.indent_level + 1)
}
pub fn write_tabs(&mut self, num: usize) -> std::fmt::Result {
for _ in 0..num {
write!(self.buf, "{}", self.indent.indent_str())?
}
Ok(())
}
pub fn new_line(&mut self) -> Result {
writeln!(self.buf)
}
pub fn write_text(&mut self, text: &IfmtInput) -> Result {
write_ifmt(text, &mut self.buf)
}
}
impl std::fmt::Write for Buffer {
fn write_str(&mut self, s: &str) -> std::fmt::Result {
self.buf.push_str(s);
Ok(())
}
}