Skip to content

Commit d92b0bb

Browse files
committed
Add --debug/-d to print the formatter configuration applied to a
given file Close #362
1 parent a5a8461 commit d92b0bb

3 files changed

Lines changed: 91 additions & 21 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ This file documents the changes made to the formatter with each release.
44

55
## Unreleased
66

7+
### Added
8+
9+
- Add `--debug`/`-d` to print the formatter configuration applied to a given file (#362)
10+
711
### Fixed
812

913
- Fix `@export_category` annotations sometimes wrapping with variables on a single line (#358)

‎src/cli.rs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const HELP_FORMATTER: &str = "\
2323
-x, --exclude <PATH> Exclude one file or directory (you can repeat this option multiple times)
2424
--verify-structure Verify formatted output has the same structure as the input
2525
--stdout Write to stdout instead of overwriting files
26-
-v, --verbose Print one status line for each processed file
26+
-v, --verbose Print one status line for each processed file
27+
-d, --debug Print effective settings for each processed file
2728
--use-spaces Use spaces instead of tabs for indentation
2829
--indent-size <NUM> Spaces per indent level (default: 4)
2930
--reorder-code Reorder code to match the style guide
@@ -78,6 +79,8 @@ pub enum Command {
7879
do_print_to_stdout: bool,
7980
/// If true, prints one status line for each processed file.
8081
use_verbose_output: bool,
82+
/// If true, prints the effective formatter settings for each processed file.
83+
use_debug_output: bool,
8184
/// If true, only checks if the files are formatted, without modifying
8285
/// them. Returns error code ERROR_CODE_NOT_FORMATTED if any of the input
8386
/// files are not formatted.
@@ -134,6 +137,7 @@ pub fn parse_args() -> CliArguments {
134137
let mut excluded_paths: Vec<PathBuf> = Vec::new();
135138
let mut format_do_print_to_stdout = false;
136139
let mut format_use_verbose_output = false;
140+
let mut format_use_debug_output = false;
137141
let mut format_do_check_formatted_only = false;
138142
let mut format_use_spaces: Option<bool> = None;
139143
let mut format_indent_size: Option<usize> = None;
@@ -205,6 +209,10 @@ pub fn parse_args() -> CliArguments {
205209
require_no_value(assigned_value, "--verbose");
206210
format_use_verbose_output = true;
207211
}
212+
"debug" => {
213+
require_no_value(assigned_value, "--debug");
214+
format_use_debug_output = true;
215+
}
208216
"check" => {
209217
require_no_value(assigned_value, "--check");
210218
format_do_check_formatted_only = true;
@@ -390,6 +398,12 @@ pub fn parse_args() -> CliArguments {
390398
}
391399
format_use_verbose_output = true;
392400
}
401+
'd' => {
402+
if matches!(active_command, ActiveCommand::Lint) {
403+
print_error_invalid_argument("unexpected argument '-d'");
404+
}
405+
format_use_debug_output = true;
406+
}
393407
_ => print_error_invalid_argument(&format!(
394408
"unexpected argument '-{}'",
395409
flag_char
@@ -409,6 +423,7 @@ pub fn parse_args() -> CliArguments {
409423
command: Command::Format {
410424
do_print_to_stdout: format_do_print_to_stdout,
411425
use_verbose_output: format_use_verbose_output,
426+
use_debug_output: format_use_debug_output,
412427
do_check_formatted_only: format_do_check_formatted_only,
413428
use_spaces: format_use_spaces,
414429
indent_size: format_indent_size,

‎src/main.rs‎

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
118118
let Command::Format {
119119
do_print_to_stdout,
120120
use_verbose_output,
121+
use_debug_output,
121122
do_check_formatted_only,
122123
use_spaces,
123124
indent_size,
@@ -157,17 +158,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
157158
.read_to_string(&mut input_content)
158159
.map_err(|error| format!("Failed to read from stdin: {}", error))?;
159160

160-
let mut stdin_config = config.clone();
161161
let current_directory = env::current_dir().expect("Failed to get current directory");
162162
// When running from stdin users would still like to apply editorconfig
163163
// settings. For the most part, you'd use a section like `[*.gd]`
164164
// matching GDScript files. we fake running the formatter on a `.gd`
165165
// file in the current directory to get user settings to apply.
166-
config_apply_editorconfig_then_cli_overrides(
167-
&mut stdin_config,
168-
&current_directory.join("stdin.gd"),
169-
config_overrides,
170-
);
166+
let stdin_path = current_directory.join("stdin.gd");
167+
let stdin_config =
168+
get_final_config_applied_to_script(&config, &stdin_path, config_overrides);
169+
if use_debug_output {
170+
eprintln!(
171+
"{}",
172+
get_formatted_configuration_as_string(&stdin_path, &stdin_config)
173+
);
174+
}
171175
let formatted_content = match format_gdscript(&input_content, &stdin_config) {
172176
Ok(formatted_content) => formatted_content,
173177
Err(FormatErrors::ParseErrors) => {
@@ -214,6 +218,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
214218
let _ = io::stdout().flush();
215219
}
216220

221+
if use_debug_output {
222+
for file_path in &input_gdscript_files {
223+
let file_config =
224+
get_final_config_applied_to_script(&config, file_path, config_overrides);
225+
eprintln!(
226+
"{}",
227+
get_formatted_configuration_as_string(file_path, &file_config)
228+
);
229+
}
230+
}
231+
217232
let mut sorted_outputs: Vec<Result<FormatterFileProcessingResult, String>> =
218233
format_files_parallel(&input_gdscript_files, &config, config_overrides);
219234

@@ -393,8 +408,7 @@ fn format_one_file(
393408

394409
// We need to clone that config because files in nested directories can
395410
// match different EditorConfig files and rules.
396-
let mut file_config = config.clone();
397-
config_apply_editorconfig_then_cli_overrides(&mut file_config, file_path, config_overrides);
411+
let file_config = get_final_config_applied_to_script(config, file_path, config_overrides);
398412
match format_gdscript_with_buffers(&input_content, &file_config, render_elements, output) {
399413
Ok(()) => {}
400414
Err(FormatErrors::ParseErrors) => {
@@ -422,36 +436,73 @@ fn format_one_file(
422436
}))
423437
}
424438

425-
/// Applies project editorconfig settings first and CLI settings second.
439+
/// Builds the final configuration applied to one file by applying the
440+
/// editorconfig settings first and CLI settings second.
426441
///
427442
/// The override fields are `Option`s because `None` means that the user did
428443
/// not pass that flag. Without this distinction, a CLI default such as an
429444
/// indentation size of four would look like an explicit request and would
430445
/// incorrectly override `.editorconfig`.
431-
fn config_apply_editorconfig_then_cli_overrides(
432-
config: &mut FormatterConfiguration,
433-
config_path: &Path,
446+
fn get_final_config_applied_to_script(
447+
config: &FormatterConfiguration,
448+
file_path: &Path,
434449
config_overrides: FormatterConfigOverrides,
435-
) {
436-
gdscript_formatter::editorconfig::apply_editorconfig_to_formatter_config(config, config_path);
450+
) -> FormatterConfiguration {
451+
let mut file_config = config.clone();
452+
gdscript_formatter::editorconfig::apply_editorconfig_to_formatter_config(
453+
&mut file_config,
454+
file_path,
455+
);
437456
if let Some(use_spaces) = config_overrides.use_spaces {
438-
config.printer.use_spaces = use_spaces;
457+
file_config.printer.use_spaces = use_spaces;
439458
}
440459
if let Some(indent_size) = config_overrides.indent_size {
441-
config.printer.indent_size = indent_size;
460+
file_config.printer.indent_size = indent_size;
442461
}
443462
if let Some(max_line_length) = config_overrides.max_line_length {
444-
config.printer.max_line_length = max_line_length;
463+
file_config.printer.max_line_length = max_line_length;
445464
}
446465
if let Some(blank_lines_around_definitions) = config_overrides.blank_lines_around_definitions {
447-
config.blank_lines_around_definitions = blank_lines_around_definitions;
466+
file_config.blank_lines_around_definitions = blank_lines_around_definitions;
448467
}
449468
if let Some(continuation_indent_level) = config_overrides.continuation_indent_level {
450-
config.printer.continuation_indent_level = continuation_indent_level;
469+
file_config.printer.continuation_indent_level = continuation_indent_level;
451470
}
452471
if let Some(quote_style) = config_overrides.quote_style {
453-
config.quote_style = quote_style;
472+
file_config.quote_style = quote_style;
454473
}
474+
file_config
475+
}
476+
477+
fn get_formatted_configuration_as_string(
478+
file_path: &Path,
479+
config: &FormatterConfiguration,
480+
) -> String {
481+
let indentation_style = if config.printer.use_spaces {
482+
format!("spaces (size {})", config.printer.indent_size)
483+
} else {
484+
"tabs".to_string()
485+
};
486+
let quote_style = match config.quote_style {
487+
QuoteStyle::Preserve => "preserve",
488+
QuoteStyle::Single => "single",
489+
QuoteStyle::Double => "double",
490+
};
491+
492+
format!(
493+
"Debug: {}\n max_line_length: {}\n indentation: {}\n blank_lines_around_definitions: {}\n continuation_indent_level: {}\n quote_style: {}\n insert_final_newline: {}\n trim_trailing_whitespace: {}\n indent_blank_lines: {}\n reorder_code: {}\n verify_structure: {}",
494+
file_path.display(),
495+
config.printer.max_line_length,
496+
indentation_style,
497+
config.blank_lines_around_definitions,
498+
config.printer.continuation_indent_level,
499+
quote_style,
500+
config.printer.insert_final_newline,
501+
config.printer.trim_trailing_whitespace,
502+
config.printer.indent_blank_lines,
503+
config.reorder_code,
504+
config.safe,
505+
)
455506
}
456507

457508
/// Formats files concurrently but preserves their original order when

0 commit comments

Comments
 (0)