Skip to content

Commit 81de06d

Browse files
committed
Fix Editorconfig settings ignored when running on a directory
fix #263
1 parent 9951045 commit 81de06d

4 files changed

Lines changed: 59 additions & 22 deletions

File tree

src/main.rs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ struct FormatterOutput {
2828
is_formatted: bool,
2929
}
3030

31+
#[derive(Clone, Copy)]
32+
struct FormatterConfigOverrides {
33+
max_line_length: Option<usize>,
34+
blank_lines_around_definitions: Option<u16>,
35+
continuation_indent_level: Option<u16>,
36+
}
37+
3138
fn main() -> Result<(), Box<dyn std::error::Error>> {
3239
let args = parse_args();
3340

@@ -82,33 +89,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
8289
unreachable!();
8390
};
8491

85-
// Precedence: CLI > editorconfig > defaults.
86-
// Build from defaults, apply editorconfig once, then CLI overrides.
8792
let mut config = FormatterConfiguration::default();
8893

8994
config.printer.indent_size = indent_size;
9095
config.printer.use_spaces = use_spaces;
9196
config.safe = use_safe_mode;
9297
config.reorder_code = do_reorder_code;
9398

94-
// Apply editorconfig using current dir or first file as anchor.
95-
let anchor = if let Some(first) = args.input_file_paths.first() {
96-
first.clone()
97-
} else {
98-
env::current_dir().map_err(|error| format!("Failed to get current directory: {}", error))?
99+
let config_overrides = FormatterConfigOverrides {
100+
max_line_length,
101+
blank_lines_around_definitions,
102+
continuation_indent_level,
99103
};
100-
gdscript_formatter::editorconfig::apply_editorconfig_to_formatter_config(&mut config, &anchor);
101-
102-
// CLI overrides beat editorconfig.
103-
if let Some(n) = max_line_length {
104-
config.printer.max_line_length = n;
105-
}
106-
if let Some(n) = blank_lines_around_definitions {
107-
config.blank_lines_around_definitions = n;
108-
}
109-
if let Some(n) = continuation_indent_level {
110-
config.printer.continuation_indent_level = n;
111-
}
112104

113105
if args.input_file_paths.is_empty() && !io::stdin().is_terminal() {
114106
let mut input_content = String::new();
@@ -152,7 +144,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
152144
let _ = io::stdout().flush();
153145

154146
let mut sorted_outputs: Vec<Result<FormatterOutput, String>> =
155-
format_files_parallel(&input_gdscript_files, &config);
147+
format_files_parallel(&input_gdscript_files, &config, config_overrides);
156148

157149
sorted_outputs.sort_by(compare_output_index);
158150

@@ -247,13 +239,31 @@ fn format_one_file(
247239
index: usize,
248240
file_path: &PathBuf,
249241
config: &FormatterConfiguration,
242+
config_overrides: FormatterConfigOverrides,
250243
render_elements: &mut Vec<RenderElement>,
251244
output: &mut String,
252245
) -> Result<FormatterOutput, String> {
253246
let input_content = fs::read_to_string(file_path)
254247
.map_err(|error| format!("Failed to read file {}: {}", file_path.display(), error))?;
255248

256-
format_gdscript_with_buffers(&input_content, config, render_elements, output)
249+
// We need to clone that config because files in nested directories can
250+
// match different EditorConfig files and rules.
251+
let mut file_config = config.clone();
252+
gdscript_formatter::editorconfig::apply_editorconfig_to_formatter_config(
253+
&mut file_config,
254+
file_path,
255+
);
256+
if let Some(max_line_length) = config_overrides.max_line_length {
257+
file_config.printer.max_line_length = max_line_length;
258+
}
259+
if let Some(blank_lines_around_definitions) = config_overrides.blank_lines_around_definitions {
260+
file_config.blank_lines_around_definitions = blank_lines_around_definitions;
261+
}
262+
if let Some(continuation_indent_level) = config_overrides.continuation_indent_level {
263+
file_config.printer.continuation_indent_level = continuation_indent_level;
264+
}
265+
266+
format_gdscript_with_buffers(&input_content, &file_config, render_elements, output)
257267
.map_err(|error| format!("Failed to format file {}: {}", file_path.display(), error))?;
258268

259269
let is_formatted = input_content == *output;
@@ -269,6 +279,7 @@ fn format_one_file(
269279
fn format_files_parallel(
270280
files: &[PathBuf],
271281
config: &FormatterConfiguration,
282+
config_overrides: FormatterConfigOverrides,
272283
) -> Vec<Result<FormatterOutput, String>> {
273284
if files.is_empty() {
274285
return Vec::new();
@@ -284,7 +295,9 @@ fn format_files_parallel(
284295
thread::scope(|scope| {
285296
let mut handles = Vec::with_capacity(thread_count);
286297
for (chunk_index, chunk) in files.chunks(chunk_size).enumerate() {
287-
let handle = scope.spawn(move || format_chunk(chunk, chunk_index, chunk_size, config));
298+
let handle = scope.spawn(move || {
299+
format_chunk(chunk, chunk_index, chunk_size, config, config_overrides)
300+
});
288301
handles.push(handle);
289302
}
290303

@@ -301,6 +314,7 @@ fn format_chunk(
301314
chunk_index: usize,
302315
chunk_size: usize,
303316
config: &FormatterConfiguration,
317+
config_overrides: FormatterConfigOverrides,
304318
) -> Vec<Result<FormatterOutput, String>> {
305319
let mut results = Vec::with_capacity(chunk.len());
306320
let mut render_elements: Vec<RenderElement> = Vec::new();
@@ -311,6 +325,7 @@ fn format_chunk(
311325
global_index,
312326
file_path,
313327
config,
328+
config_overrides,
314329
&mut render_elements,
315330
&mut output,
316331
));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
root = true
2+
3+
[*.gd]
4+
gdscript_formatter_indent_blank_lines = true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Tests that the editorconfig file works as intended when running the formatter
2+
# over a directory.
3+
extends Node
4+
5+
6+
func _ready():
7+
print("Hello, world!")
8+
9+
print("Hello again!")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Tests that the editorconfig file works as intended when running the formatter
2+
# over a directory.
3+
extends Node
4+
5+
6+
func _ready():
7+
print("Hello from a subdirectory!")
8+
9+
print("Hello again!")

0 commit comments

Comments
 (0)