typstyle_core/
config.rs

1/// Configuration Options for Typstyle Printer.
2#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
4#[cfg_attr(feature = "serde", serde(default))]
5pub struct Config {
6    /// Number of spaces to use for each indentation level.
7    pub tab_spaces: usize,
8    /// Maximum width of each line.
9    pub max_width: usize,
10    /// Maximum number of consecutive blank lines allowed between code items.
11    pub blank_lines_upper_bound: usize,
12    /// When `true`, consecutive whitespace in markup is collapsed into a single space.
13    pub collapse_markup_spaces: bool,
14    /// When `true`, import items are sorted alphabetically.
15    pub reorder_import_items: bool,
16    /// When `true`, text in markup will be wrapped to fit within `max_width`.
17    /// Implies `collapse_markup_spaces`.
18    pub wrap_text: bool,
19}
20
21impl Default for Config {
22    fn default() -> Self {
23        Self {
24            tab_spaces: 2,
25            max_width: 80,
26            blank_lines_upper_bound: 2,
27            reorder_import_items: true,
28            collapse_markup_spaces: false,
29            wrap_text: false,
30        }
31    }
32}
33
34impl Config {
35    pub fn new() -> Self {
36        Default::default()
37    }
38
39    pub fn with_width(mut self, max_width: usize) -> Self {
40        self.max_width = max_width;
41        self
42    }
43
44    pub fn with_tab_spaces(mut self, tab_spaces: usize) -> Self {
45        self.tab_spaces = tab_spaces;
46        self
47    }
48
49    pub fn chain_width(&self) -> usize {
50        const CHAIN_WIDTH_RATIO: f32 = 0.6;
51        (self.max_width as f32 * CHAIN_WIDTH_RATIO) as usize
52    }
53
54    pub fn with_wrap_text(mut self, wrap_text: bool) -> Self {
55        self.wrap_text = wrap_text;
56        self
57    }
58}