Skip to main content

oak_pretty_print/config/
mod.rs

1use alloc::borrow::Cow;
2
3/// 缩进样式
4#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
5#[serde(rename_all = "snake_case")]
6pub enum IndentStyle {
7    /// 使用空格
8    Spaces(u8),
9    /// 使用制表符
10    Tabs,
11}
12
13impl Default for IndentStyle {
14    fn default() -> Self {
15        IndentStyle::Spaces(4)
16    }
17}
18
19/// 行结束符
20#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
21#[serde(rename_all = "snake_case")]
22pub enum LineEnding {
23    /// Unix 风格 (\n)
24    Unix,
25    /// Windows 风格 (\r\n)
26    Windows,
27    /// 自动检测
28    Auto,
29}
30
31impl Default for LineEnding {
32    fn default() -> Self {
33        LineEnding::Auto
34    }
35}
36
37/// 格式化配置
38#[derive(Debug, Clone, serde::Serialize)]
39#[serde(rename_all = "camelCase")]
40pub struct FormatConfig {
41    /// 缩进样式
42    pub indent_style: IndentStyle,
43    /// 缩进文本(缓存的单级缩进字符串)
44    pub indent_text: Cow<'static, str>,
45    /// 行结束符
46    pub line_ending: LineEnding,
47    /// 最大行长度
48    pub max_width: usize,
49    /// 是否在文件末尾插入换行符
50    pub insert_final_newline: bool,
51    /// 是否修剪行尾空白
52    pub trim_trailing_whitespace: bool,
53    /// 是否保留空行
54    pub preserve_blank_lines: bool,
55    /// 最大连续空行数
56    pub max_blank_lines: usize,
57    /// 是否格式化注释
58    pub format_comments: bool,
59    /// 是否格式化字符串
60    pub format_strings: bool,
61    /// 缩进大小(用于列计算)
62    pub indent_size: usize,
63}
64
65impl Default for FormatConfig {
66    fn default() -> Self {
67        let indent_style = IndentStyle::default();
68        let (indent_text, indent_size) = match indent_style {
69            IndentStyle::Spaces(count) => (" ".repeat(count as usize).into(), count as usize),
70            IndentStyle::Tabs => ("\t".into(), 4), // Default tab size for column calculation
71        };
72
73        Self {
74            indent_style,
75            indent_text,
76            line_ending: LineEnding::default(),
77            max_width: 100,
78            insert_final_newline: true,
79            trim_trailing_whitespace: true,
80            preserve_blank_lines: true,
81            max_blank_lines: 2,
82            format_comments: true,
83            format_strings: false,
84            indent_size,
85        }
86    }
87}
88
89impl FormatConfig {
90    /// 创建新的配置
91    pub fn new() -> Self {
92        Self::default()
93    }
94
95    /// 设置缩进样式
96    pub fn with_indent_style(mut self, style: IndentStyle) -> Self {
97        self.indent_style = style;
98        let (indent_text, indent_size) = match style {
99            IndentStyle::Spaces(count) => (" ".repeat(count as usize).into(), count as usize),
100            IndentStyle::Tabs => ("\t".into(), 4),
101        };
102        self.indent_text = indent_text;
103        self.indent_size = indent_size;
104        self
105    }
106
107    /// 设置行结束符
108    pub fn with_line_ending(mut self, ending: LineEnding) -> Self {
109        self.line_ending = ending;
110        self
111    }
112
113    /// 设置最大行长度
114    pub fn with_max_width(mut self, length: usize) -> Self {
115        self.max_width = length;
116        self
117    }
118
119    /// 获取行结束符字符串
120    pub fn line_ending_string(&self) -> &'static str {
121        match self.line_ending {
122            LineEnding::Unix => "\n",
123            LineEnding::Windows => "\r\n",
124            LineEnding::Auto => {
125                // 在实际使用中,应该根据输入文件检测
126                #[cfg(windows)]
127                return "\r\n";
128                #[cfg(not(windows))]
129                return "\n";
130            }
131        }
132    }
133}