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
use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use dprint_core::configuration::*;
pub struct ConfigurationBuilder {
config: HashMap<String, String>,
global_config: Option<GlobalConfiguration>,
}
impl ConfigurationBuilder {
pub fn new() -> ConfigurationBuilder {
ConfigurationBuilder {
config: HashMap::new(),
global_config: None,
}
}
pub fn build(&self) -> Configuration {
if let Some(global_config) = &self.global_config {
resolve_config(&self.config, global_config).config
} else {
let global_config = resolve_global_config(&HashMap::new()).config;
resolve_config(&self.config, &global_config).config
}
}
pub fn global_config(&mut self, global_config: GlobalConfiguration) -> &mut Self {
self.global_config = Some(global_config);
self
}
pub fn line_width(&mut self, value: u32) -> &mut Self {
self.insert("lineWidth", value)
}
pub fn use_tabs(&mut self, value: bool) -> &mut Self {
self.insert("useTabs", value)
}
pub fn indent_width(&mut self, value: u8) -> &mut Self {
self.insert("indentWidth", value)
}
pub fn new_line_kind(&mut self, value: NewLineKind) -> &mut Self {
self.insert("newLineKind", value)
}
#[cfg(test)]
pub(super) fn get_inner_config(&self) -> HashMap<String, String> {
self.config.clone()
}
fn insert<T>(&mut self, name: &str, value: T) -> &mut Self where T : std::string::ToString {
self.config.insert(String::from(name), value.to_string());
self
}
}
pub fn resolve_config(config: &HashMap<String, String>, global_config: &GlobalConfiguration) -> ResolveConfigurationResult<Configuration> {
let mut diagnostics = Vec::new();
let mut config = config.clone();
let resolved_config = Configuration {
line_width: get_value(&mut config, "lineWidth", global_config.line_width.unwrap_or(80), &mut diagnostics),
use_tabs: get_value(&mut config, "useTabs", global_config.use_tabs.unwrap_or(DEFAULT_GLOBAL_CONFIGURATION.use_tabs), &mut diagnostics),
indent_width: get_value(&mut config, "indentWidth", global_config.indent_width.unwrap_or(DEFAULT_GLOBAL_CONFIGURATION.indent_width), &mut diagnostics),
new_line_kind: get_value(&mut config, "newLineKind", global_config.new_line_kind.unwrap_or(DEFAULT_GLOBAL_CONFIGURATION.new_line_kind), &mut diagnostics),
};
for (key, _) in config.iter() {
diagnostics.push(ConfigurationDiagnostic {
property_name: String::from(key),
message: format!("Unknown property in configuration: {}", key),
});
}
ResolveConfigurationResult {
config: resolved_config,
diagnostics,
}
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Configuration {
pub indent_width: u8,
pub line_width: u32,
pub use_tabs: bool,
pub new_line_kind: NewLineKind,
}