Repository navigation
Expand file tree
/
Copy pathformatter.rs
More file actions
215 lines (180 loc) · 6.66 KB
/
Copy pathformatter.rs
File metadata and controls
215 lines (180 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! Built-in formatters for common file types.
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
/// Formatter configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FormatterConfig {
/// Formatter identifier.
pub id: String,
/// Display name.
pub name: String,
/// Command to run.
pub command: Vec<String>,
/// File extensions this formatter handles.
pub extensions: Vec<String>,
/// Environment variables.
#[serde(default)]
pub environment: HashMap<String, String>,
/// Whether formatter is disabled.
#[serde(default)]
pub disabled: bool,
}
impl FormatterConfig {
pub fn new(id: impl Into<String>, name: impl Into<String>) -> Self {
Self {
id: id.into(),
name: name.into(),
command: Vec::new(),
extensions: Vec::new(),
environment: HashMap::new(),
disabled: false,
}
}
pub fn command(mut self, cmd: Vec<&str>) -> Self {
self.command = cmd.into_iter().map(String::from).collect();
self
}
pub fn extensions(mut self, exts: Vec<&str>) -> Self {
self.extensions = exts.into_iter().map(String::from).collect();
self
}
pub fn matches_extension(&self, ext: &str) -> bool {
self.extensions.iter().any(|e| e == ext)
}
pub fn matches_file(&self, path: &str) -> bool {
if let Some(ext) = std::path::Path::new(path).extension() {
self.matches_extension(&ext.to_string_lossy())
} else {
false
}
}
}
/// A formatter instance.
pub struct Formatter {
pub config: FormatterConfig,
}
impl Formatter {
pub fn new(config: FormatterConfig) -> Self {
Self { config }
}
/// Build command for formatting a file.
pub fn build_command(&self, file_path: &str) -> Vec<String> {
self.config
.command
.iter()
.map(|arg| arg.replace("{file}", file_path))
.collect()
}
}
lazy_static::lazy_static! {
pub static ref BUILTIN_FORMATTERS: Vec<FormatterConfig> = vec![
// Prettier - JS/TS/HTML/CSS/JSON/YAML/MD
FormatterConfig::new("prettier", "Prettier")
.command(vec!["prettier", "--write", "{file}"])
.extensions(vec![
"js", "jsx", "ts", "tsx", "mjs", "cjs",
"json", "jsonc", "json5",
"html", "htm", "vue", "svelte",
"css", "scss", "less",
"md", "mdx",
"yaml", "yml",
"graphql", "gql"
]),
// Black - Python
FormatterConfig::new("black", "Black")
.command(vec!["black", "{file}"])
.extensions(vec!["py", "pyi"]),
// Ruff - Python (faster alternative)
FormatterConfig::new("ruff", "Ruff")
.command(vec!["ruff", "format", "{file}"])
.extensions(vec!["py", "pyi"]),
// Rustfmt - Rust
FormatterConfig::new("rustfmt", "Rustfmt")
.command(vec!["rustfmt", "{file}"])
.extensions(vec!["rs"]),
// gofmt - Go
FormatterConfig::new("gofmt", "Gofmt")
.command(vec!["gofmt", "-w", "{file}"])
.extensions(vec!["go"]),
// goimports - Go (with import sorting)
FormatterConfig::new("goimports", "Goimports")
.command(vec!["goimports", "-w", "{file}"])
.extensions(vec!["go"]),
// clang-format - C/C++
FormatterConfig::new("clang-format", "Clang-Format")
.command(vec!["clang-format", "-i", "{file}"])
.extensions(vec!["c", "cpp", "cc", "cxx", "h", "hpp", "hxx"]),
// shfmt - Shell
FormatterConfig::new("shfmt", "Shfmt")
.command(vec!["shfmt", "-w", "{file}"])
.extensions(vec!["sh", "bash"]),
// stylua - Lua
FormatterConfig::new("stylua", "StyLua")
.command(vec!["stylua", "{file}"])
.extensions(vec!["lua"]),
// mix format - Elixir
FormatterConfig::new("mix-format", "Mix Format")
.command(vec!["mix", "format", "{file}"])
.extensions(vec!["ex", "exs"]),
// zigfmt - Zig
FormatterConfig::new("zigfmt", "Zig Fmt")
.command(vec!["zig", "fmt", "{file}"])
.extensions(vec!["zig"]),
// terraform fmt - Terraform
FormatterConfig::new("terraform-fmt", "Terraform Fmt")
.command(vec!["terraform", "fmt", "{file}"])
.extensions(vec!["tf", "tfvars"]),
// SQL formatter
FormatterConfig::new("sql-formatter", "SQL Formatter")
.command(vec!["sql-formatter", "--fix", "{file}"])
.extensions(vec!["sql"]),
// xmllint - XML
FormatterConfig::new("xmllint", "XMLLint")
.command(vec!["xmllint", "--format", "--output", "{file}", "{file}"])
.extensions(vec!["xml", "xsl", "xslt"]),
// rubocop - Ruby
FormatterConfig::new("rubocop", "RuboCop")
.command(vec!["rubocop", "-a", "{file}"])
.extensions(vec!["rb", "rake"]),
// ktlint - Kotlin
FormatterConfig::new("ktlint", "ktlint")
.command(vec!["ktlint", "-F", "{file}"])
.extensions(vec!["kt", "kts"]),
// swift-format - Swift
FormatterConfig::new("swift-format", "Swift Format")
.command(vec!["swift-format", "--in-place", "{file}"])
.extensions(vec!["swift"]),
// dart format - Dart
FormatterConfig::new("dart-format", "Dart Format")
.command(vec!["dart", "format", "{file}"])
.extensions(vec!["dart"]),
];
}
/// Find a formatter for a file.
pub fn find_formatter_for_file(path: &str) -> Option<&'static FormatterConfig> {
BUILTIN_FORMATTERS.iter().find(|f| f.matches_file(path))
}
/// Find a formatter by ID.
pub fn find_formatter_by_id(id: &str) -> Option<&'static FormatterConfig> {
BUILTIN_FORMATTERS.iter().find(|f| f.id == id)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_find_formatter() {
let formatter = find_formatter_for_file("main.rs");
assert!(formatter.is_some());
assert_eq!(formatter.unwrap().id, "rustfmt");
let formatter = find_formatter_for_file("app.tsx");
assert!(formatter.is_some());
assert_eq!(formatter.unwrap().id, "prettier");
}
#[test]
fn test_formatter_command() {
let config = FormatterConfig::new("test", "Test").command(vec!["fmt", "--write", "{file}"]);
let formatter = Formatter::new(config);
let cmd = formatter.build_command("/path/to/file.rs");
assert_eq!(cmd[2], "/path/to/file.rs");
}
}