@@ -83,6 +83,7 @@ var _has_formatter_command := false
8383var _has_format_command := false
8484var _has_lint_command := false
8585var _already_warned_about_reorder_on_save := false
86+ var _already_warned_about_builtin_format_on_save := false
8687# Used to auto detect changes to the project's .editorconfig file.
8788var _editorconfig_last_modified_time := - 1
8889# Editorconfig allows setting rules per path glob. We track globs for the format
@@ -172,7 +173,7 @@ func _enter_tree() -> void:
172173 installer = FormatterInstaller .new (formatter_cache_dir )
173174 add_child (installer )
174175 installer .installation_completed .connect (
175- func _on_installation_completed (binary_path : String ) -> void :
176+ func _on_installation_completed (binary_path : String ) -> void :
176177 set_editor_setting (SETTING_FORMATTER_PATH , binary_path )
177178 _has_formatter_command = has_command (binary_path )
178179 if not _has_formatter_command :
@@ -187,7 +188,7 @@ func _enter_tree() -> void:
187188 menu .update_menu (true ),
188189 )
189190 installer .installation_failed .connect (
190- func _on_installation_failed (error_message : String ) -> void :
191+ func _on_installation_failed (error_message : String ) -> void :
191192 push_error ("Formatter installation failed: " , error_message ),
192193 )
193194
@@ -304,13 +305,35 @@ func update_shortcut() -> void:
304305func _on_resource_saved (saved_resource : Resource ) -> void :
305306 if saved_resource is not GDScript :
306307 return
307-
308308 var script := saved_resource as GDScript
309+ # We should normally never hit this condition, I'm adding it as a
310+ # safety check and to document issues with format on save and built-in
311+ # scripts. We support formatting built-in scripts, but not formatting them
312+ # on save:
313+ # - There's no way to retrieve built-in scripts and when their enclosing
314+ # scene is saved efficiently
315+ # - You would have to re-save the scene after formatting
316+ #
317+ # That would add complexity and slowdowns so we don't support it at the
318+ # moment. We may revisit this if multiple users depend on this feature (I
319+ # generally don't recommend built-in scripts because of their limitations
320+ # for debugging, error reporting, VCS... they have too many limitations for
321+ # their benefit)
322+ if script .is_built_in ():
323+ if do_format_on_save and not _already_warned_about_builtin_format_on_save :
324+ push_warning (
325+ "GDScript Formatter: Format on save is not supported for built-in scripts. "
326+ + "Format this script manually instead."
327+ )
328+ _already_warned_about_builtin_format_on_save = true
329+ return
330+
309331 var do_format_on_save := get_editor_setting (SETTING_FORMAT_ON_SAVE ) as bool
310332 var editorconfig_format_on_save = get_editorconfig_format_on_save (script .resource_path )
311333 if editorconfig_format_on_save != null :
312334 do_format_on_save = editorconfig_format_on_save as bool
313335 var lint_on_save := get_editor_setting (SETTING_LINT_ON_SAVE ) as bool
336+
314337 if (
315338 do_format_on_save and get_format_mode () == FormatMode .REORDER_CODE
316339 and not _already_warned_about_reorder_on_save
@@ -705,7 +728,11 @@ func editorconfig_section_matches(pattern: String, relative_script_path: String)
705728## the editor and requests formatting without having saved their changes (in
706729## that case, the code they're editing only exists in the script editor's open
707730## tab).
708- func format_code (script : GDScript , force_reorder := false , source_content : Variant = null ) -> String :
731+ func format_code (
732+ script : GDScript ,
733+ force_reorder := false ,
734+ source_content : Variant = null ,
735+ ) -> String :
709736 var script_path := script .resource_path
710737 if source_content == null and script_path .is_empty ():
711738 push_error ("GDScript Formatter Error: Can't format an unsaved script." )
0 commit comments