Skip to content

Commit 6259d00

Browse files
committed
Godot addon: Use standard output mode to avoid overwriting the file on disk
Should help with #160 but I have trouble reproducing the issue so this needs testing
1 parent 7fc2bc3 commit 6259d00

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

addons/GDQuest_GDScript_formatter/plugin.gd

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,13 @@ func has_editor_setting(setting_name: String) -> bool:
436436
## and returns the formatted code as a string. Optionally reorders the code.
437437
func format_code(script: GDScript, force_reorder := false) -> String:
438438
var script_path := script.resource_path
439+
if script_path.is_empty():
440+
push_error("GDScript Formatter Error: Can't format an unsaved script.")
441+
return ""
442+
439443
var output: Array = []
440-
var formatter_arguments: Array = [ProjectSettings.globalize_path(script_path)]
444+
var error_output: Array = []
445+
var formatter_arguments := PackedStringArray(["--stdout"])
441446

442447
if get_editor_setting(SETTING_USE_SPACES):
443448
formatter_arguments.push_back("--use-spaces")
@@ -455,18 +460,26 @@ func format_code(script: GDScript, force_reorder := false) -> String:
455460
if not force_reorder and get_editor_setting(SETTING_SAFE_MODE):
456461
formatter_arguments.push_back("--safe")
457462

458-
var exit_code := OS.execute(get_editor_setting(SETTING_FORMATTER_PATH), formatter_arguments, output)
463+
formatter_arguments.push_back(ProjectSettings.globalize_path(script_path))
464+
465+
var exit_code := OS.execute(
466+
get_editor_setting(SETTING_FORMATTER_PATH),
467+
formatter_arguments,
468+
output,
469+
)
459470
if exit_code == OK:
460-
var formatted_file := FileAccess.open(script_path, FileAccess.READ)
461-
if not is_instance_valid(formatted_file):
462-
push_error("GDScript Formatter Error: Can't read formatted file.")
471+
if output.is_empty():
472+
push_error("Format GDScript returned no output for: " + script_path)
463473
return ""
464-
var formatted_code := formatted_file.get_as_text()
465-
formatted_file.close()
466-
return formatted_code
474+
return output.front()
467475
else:
468476
push_error("Format GDScript failed: " + script_path)
469-
push_error("\tExit code: " + str(exit_code) + " Output: " + (output.front().strip_edges() if output.size() > 0 else "No output"))
477+
push_error(
478+
"\tExit code: " + str(exit_code) + " Stdout: " +
479+
(output.front().strip_edges() if output.size() > 0 else "No output"),
480+
)
481+
if error_output.size() > 0:
482+
push_error("\tStderr: " + error_output.front().strip_edges())
470483
push_error('\tIf your script does not have any syntax errors, this might be a formatter bug.')
471484
return ""
472485

0 commit comments

Comments
 (0)