@@ -22,8 +22,10 @@ const SETTING_REORDER_CODE = "reorder_code"
2222const SETTING_SAFE_MODE = "safe_mode"
2323const SETTING_FORMATTER_PATH = "formatter_path"
2424const SETTING_LINT_ON_SAVE = "lint_on_save"
25+ const SETTING_MAX_LINE_LENGTH = "max_line_length"
2526const SETTING_LINT_LINE_LENGTH = "lint_line_length"
2627const SETTING_LINT_IGNORED_RULES = "lint_ignored_rules"
28+ const DEFAULT_MAX_LINE_LENGTH = 100
2729# Directories to ignore when Format on Save is enabled
2830const SETTING_IGNORED_DIRECTORIES = "format_on_save_ignored_directories"
2931
@@ -65,7 +67,7 @@ var DEFAULT_SETTINGS = {
6567 SETTING_FORMAT_MODE : FormatMode .NORMAL ,
6668 SETTING_FORMATTER_PATH : "" ,
6769 SETTING_LINT_ON_SAVE : false ,
68- SETTING_LINT_LINE_LENGTH : 100 ,
70+ SETTING_MAX_LINE_LENGTH : DEFAULT_MAX_LINE_LENGTH ,
6971 SETTING_LINT_IGNORED_RULES : "" ,
7072 SETTING_IGNORED_DIRECTORIES : PackedStringArray (["addons/" ]),
7173}
@@ -99,10 +101,74 @@ const FORMATTER_BINARY_NAME = "gdscript-formatter"
99101
100102
101103func _init () -> void :
102- migrate_format_mode_setting ()
104+ settings_migrate_old ()
105+ settings_register_default_and_new ()
106+
107+
108+ ## Migrates settings from older versions of the add-on.
109+ func settings_migrate_old () -> void :
110+ var editor_settings := EditorInterface .get_editor_settings ()
111+
112+ # Convert the old independent settings to the mutually exclusive format mode.
113+ # The legacy safe mode takes priority because it is the least destructive option.
114+ if not has_editor_setting (SETTING_FORMAT_MODE ):
115+ # Inferring a version number from the old settings; editor settings does not
116+ # give us a neat way to version our settings so we do it manually. It's just
117+ # to keep track of migrations.
118+ var version := - 1
119+ if has_editor_setting (SETTING_REORDER_CODE ) or has_editor_setting (SETTING_SAFE_MODE ):
120+ version = 1
121+
122+ # Upgrade to version 2; that's when we merged safe mode and reorder code
123+ # into a single format mode (because they're mutually exclusive).
124+ if version == 1 :
125+ var format_mode := FormatMode .NORMAL
126+ if (
127+ has_editor_setting (SETTING_SAFE_MODE )
128+ and get_editor_setting (SETTING_SAFE_MODE ) as bool
129+ ):
130+ format_mode = FormatMode .VERIFY_STRUCTURE
131+ elif (
132+ has_editor_setting (SETTING_REORDER_CODE )
133+ and get_editor_setting (SETTING_REORDER_CODE ) as bool
134+ ):
135+ format_mode = FormatMode .REORDER_CODE
136+
137+ set_editor_setting (SETTING_FORMAT_MODE , format_mode )
138+
139+ # Remove the old settings so users do not see two conflicting configurations.
140+ for setting_name : String in [SETTING_REORDER_CODE , SETTING_SAFE_MODE ]:
141+ if has_editor_setting (setting_name ):
142+ editor_settings .erase (EDITOR_SETTINGS_CATEGORY + setting_name )
143+
144+ # Rename the setting that was originally used only for linting. We now use
145+ # this max line length setting for both linting and formatting.
146+ if has_editor_setting (SETTING_LINT_LINE_LENGTH ):
147+ if not has_editor_setting (SETTING_MAX_LINE_LENGTH ):
148+ set_editor_setting (
149+ SETTING_MAX_LINE_LENGTH ,
150+ get_editor_setting (SETTING_LINT_LINE_LENGTH ),
151+ )
152+
153+ editor_settings .erase (EDITOR_SETTINGS_CATEGORY + SETTING_LINT_LINE_LENGTH )
154+
155+
156+ ## Registers and initializes the add-on's new and missing editor settings.
157+ func settings_register_default_and_new () -> void :
103158 if not has_editor_setting (SETTING_FORMAT_MODE ):
104159 set_editor_setting (SETTING_FORMAT_MODE , DEFAULT_SETTINGS [SETTING_FORMAT_MODE ])
105- register_format_mode_setting ()
160+
161+ var editor_settings := EditorInterface .get_editor_settings ()
162+ var setting_name := EDITOR_SETTINGS_CATEGORY + SETTING_FORMAT_MODE
163+ editor_settings .add_property_info (
164+ {
165+ "name" : setting_name ,
166+ "type" : TYPE_INT ,
167+ "hint" : PROPERTY_HINT_ENUM ,
168+ "hint_string" : "Normal,Reorder code,Verify structure" ,
169+ }
170+ )
171+ editor_settings .set_initial_value (setting_name , DEFAULT_SETTINGS [SETTING_FORMAT_MODE ], false )
106172
107173 for setting : String in DEFAULT_SETTINGS .keys ():
108174 if setting == SETTING_FORMAT_MODE :
@@ -125,56 +191,6 @@ func _init() -> void:
125191 set_editor_setting (SETTING_SHORTCUT , shortcut )
126192
127193
128- func register_format_mode_setting () -> void :
129- var editor_settings := EditorInterface .get_editor_settings ()
130- var setting_name := EDITOR_SETTINGS_CATEGORY + SETTING_FORMAT_MODE
131- editor_settings .add_property_info (
132- {
133- "name" : setting_name ,
134- "type" : TYPE_INT ,
135- "hint" : PROPERTY_HINT_ENUM ,
136- "hint_string" : "Normal,Reorder code,Verify structure" ,
137- }
138- )
139- editor_settings .set_initial_value (setting_name , DEFAULT_SETTINGS [SETTING_FORMAT_MODE ], false )
140-
141-
142- ## Converts the old independent settings to the mutually exclusive format mode.
143- ## The legacy safe mode takes priority because it is the least destructive option.
144- func migrate_format_mode_setting () -> void :
145- if has_editor_setting (SETTING_FORMAT_MODE ):
146- return
147-
148- # Inferring a version number from the old settings; editor settings does not
149- # give us a neat way to version our settings so we do it manually. It's just
150- # to keep track of migrations.
151- var version := - 1
152- if has_editor_setting (SETTING_REORDER_CODE ) or has_editor_setting (SETTING_SAFE_MODE ):
153- version = 1
154-
155- # Upgrade to version 2; that's when we merged safe mode and reorder code
156- # into a single format mode (because they're mutually exclusive).
157- if version == 1 :
158- var format_mode := FormatMode .NORMAL
159- if has_editor_setting (SETTING_SAFE_MODE ) and get_editor_setting (SETTING_SAFE_MODE ) as bool :
160- format_mode = FormatMode .VERIFY_STRUCTURE
161- elif (
162- has_editor_setting (SETTING_REORDER_CODE )
163- and get_editor_setting (SETTING_REORDER_CODE ) as bool
164- ):
165- format_mode = FormatMode .REORDER_CODE
166-
167- set_editor_setting (SETTING_FORMAT_MODE , format_mode )
168-
169- # Remove the old settings so users do not see two conflicting configurations.
170- var editor_settings := EditorInterface .get_editor_settings ()
171- for setting_name : String in [SETTING_REORDER_CODE , SETTING_SAFE_MODE ]:
172- if has_editor_setting (setting_name ):
173- editor_settings .erase (EDITOR_SETTINGS_CATEGORY + setting_name )
174-
175- version = 2
176-
177-
178194func _enable_plugin () -> void :
179195 # This is called by Godot when the user enables the plugin in their project
180196 # settings. Here we get version information about the formator program and
@@ -263,7 +279,10 @@ func _enter_tree() -> void:
263279
264280
265281func _update_quick_setup_settings_display () -> void :
266- for setting : QuickSetupWindow .Settings in [QuickSetupWindow .Settings .FORMAT_ON_SAVE , QuickSetupWindow .Settings .LINT_ON_SAVE ]:
282+ for setting : QuickSetupWindow .Settings in [
283+ QuickSetupWindow .Settings .FORMAT_ON_SAVE ,
284+ QuickSetupWindow .Settings .LINT_ON_SAVE ,
285+ ]:
267286 var state = get_editor_setting (_get_quick_setup_setting_name (setting ))
268287 quick_setup_window .set_setting_state (setting , state )
269288
@@ -767,6 +786,12 @@ func get_editor_setting(setting_name: String) -> Variant:
767786 return DEFAULT_SETTINGS [setting_name ]
768787
769788
789+ ## Returns whether the user configured a non-default line length in Godot.
790+ ## When it is still the default, the formatter can apply .editorconfig instead.
791+ func has_custom_max_line_length_setting () -> bool :
792+ return get_editor_setting (SETTING_MAX_LINE_LENGTH ) as int != DEFAULT_MAX_LINE_LENGTH
793+
794+
770795func set_editor_setting (setting_name : String , value : Variant ) -> void :
771796 var editor_settings := EditorInterface .get_editor_settings ()
772797 var full_setting_key := EDITOR_SETTINGS_CATEGORY + setting_name
@@ -901,7 +926,14 @@ func format_code(
901926 source_content = source_file .get_as_text ()
902927 source_file .close ()
903928
904- var path_temporary_file := OS .get_temp_dir ().path_join (
929+ # Keep the temporary file close to the source so the formatter can discover
930+ # the same .editorconfig files it would find when run from the command line.
931+ # Unsaved scripts have no source directory, so use the project root instead.
932+ var temporary_file_directory := ProjectSettings .globalize_path ("res://" )
933+ if not script_path .is_empty ():
934+ temporary_file_directory = ProjectSettings .globalize_path (script_path ).get_base_dir ()
935+
936+ var path_temporary_file := temporary_file_directory .path_join (
905937 "gdscript_formatter_%d .gd" % Time .get_ticks_msec ()
906938 )
907939 var temporary_file := FileAccess .open (path_temporary_file , FileAccess .WRITE )
@@ -912,6 +944,13 @@ func format_code(
912944 temporary_file .close ()
913945
914946 var formatter_arguments := PackedStringArray ()
947+ # A non-default Godot setting overrides .editorconfig. Leave the argument out
948+ # for the default so the formatter can use .editorconfig's max_line_length.
949+ if has_custom_max_line_length_setting ():
950+ var max_line_length := get_editor_setting (SETTING_MAX_LINE_LENGTH ) as int
951+ formatter_arguments .push_back ("--max-line-length" )
952+ formatter_arguments .push_back (str (max_line_length ))
953+
915954 if get_editor_setting (SETTING_USE_SPACES ):
916955 formatter_arguments .push_back ("--use-spaces" )
917956 formatter_arguments .push_back ("--indent-size=%d " % get_editor_setting (SETTING_INDENT_SIZE ))
@@ -976,9 +1015,12 @@ func lint_code(script: GDScript) -> Array:
9761015 var output : Array = []
9771016 var formatter_arguments : Array = ["lint" , ProjectSettings .globalize_path (script_path )]
9781017
979- var max_line_length := get_editor_setting (SETTING_LINT_LINE_LENGTH ) as int
980- formatter_arguments .append ("--max-line-length" )
981- formatter_arguments .append (str (max_line_length ))
1018+ # Match formatting: a non-default Godot setting overrides .editorconfig;
1019+ # otherwise let the formatter read max_line_length from .editorconfig.
1020+ if has_custom_max_line_length_setting ():
1021+ var max_line_length := get_editor_setting (SETTING_MAX_LINE_LENGTH ) as int
1022+ formatter_arguments .append ("--max-line-length" )
1023+ formatter_arguments .append (str (max_line_length ))
9821024
9831025 var ignored_rules := get_editor_setting (SETTING_LINT_IGNORED_RULES ) as String
9841026 if not ignored_rules .is_empty ():
0 commit comments