30
30
31
31
class ConfigDict (dict ):
32
32
_bypass_restrictions = False
33
- _allowed_keys_for_get = ['custom_modules' , 'custom_modules_manifest' , 'formatters' , 'STOP' , ' environ' , 'quick_options' ]
33
+ _allowed_keys_for_get = ['custom_modules' , 'custom_modules_manifest' , 'formatters' , 'environ' , 'quick_options' ]
34
34
35
35
def get (self , key , * args , ** kwargs ): # access via CONFIG.get('key')
36
36
if key in ConfigDict ._allowed_keys_for_get :
@@ -94,6 +94,7 @@ def __init__(cls, name, bases, namespace, **kwargs):
94
94
class Module (metaclass = ModuleMeta ):
95
95
'''
96
96
API solely for interacting with files located in the 'modules' folder.
97
+ These methods allow you to create a custom formatter adapter for your plugin.
97
98
'''
98
99
99
100
def __init__ (self , view = None , uid = None , region = None , interpreters = None , executables = None , dotfiles = None , temp_dir = None , type = None , auto_format_config = None , ** kwargs ):
@@ -108,10 +109,37 @@ def __init__(self, view=None, uid=None, region=None, interpreters=None, executab
108
109
self .auto_format_config = auto_format_config
109
110
self .kwargs = kwargs # @unused
110
111
112
+ # Track temp files created
113
+ self ._tmp_files_created = {}
114
+
115
+ def __del__ (self ):
116
+ # Clean up automatically if autodel=True for any tmp files
117
+ for tmp_file , autodel in self ._tmp_files_created .items ():
118
+ if autodel :
119
+ TempFileHandler .remove_tmp_file (tmp_file )
120
+ else :
121
+ log .warning (
122
+ 'Temporary files were created but not removed: %s\n '
123
+ 'Ensure that all temporary files are manually removed by using "self.remove_tmp_file(tmp_file)"\n '
124
+ 'Or, create the temporary file with automatic deletion by using "self.create_tmp_file(suffix=None, autodel=True)"' , tmp_file
125
+ )
126
+
111
127
@abc .abstractmethod
112
128
def format (self ):
113
129
raise NotImplementedError ('Subclasses must implement the "format()" method.' )
114
130
131
+ def create_tmp_file (self , suffix = None , autodel = False ):
132
+ tmp_file = TempFileHandler .create_tmp_file (view = self .view , uid = self .uid , region = self .region , auto_format_config = self .auto_format_config , suffix = suffix )
133
+ self ._tmp_files_created [tmp_file ] = autodel
134
+ return tmp_file
135
+
136
+ def remove_tmp_file (self , tmp_file = None ):
137
+ if tmp_file in self ._tmp_files_created :
138
+ del self ._tmp_files_created [tmp_file ]
139
+ return TempFileHandler .remove_tmp_file (tmp_file = tmp_file )
140
+ else :
141
+ raise ValueError ('Attempting to remove a temporary file that was not created by this module instance.' )
142
+
115
143
def is_executable (self , file = None ):
116
144
return FileHandler .is_executable (file = file )
117
145
@@ -154,12 +182,6 @@ def is_view_formattable(self):
154
182
def query (self , data_dict , default = None , * keys ):
155
183
return OptionHandler .query (data_dict , default , * keys )
156
184
157
- def create_tmp_file (self , suffix = None ):
158
- return TempFileHandler .create_tmp_file (view = self .view , uid = self .uid , region = self .region , auto_format_config = self .auto_format_config , suffix = suffix )
159
-
160
- def remove_tmp_file (self , tmp_file = None ):
161
- return TempFileHandler .remove_tmp_file (tmp_file = tmp_file )
162
-
163
185
def get_executable (self , runtime_type = None ):
164
186
return ArgumentHandler .get_executable (view = self .view , uid = self .uid , executables = self .executables , runtime_type = runtime_type )
165
187
@@ -215,6 +237,24 @@ def popup_message(self, text, title=None, dialog=False):
215
237
216
238
# === Module Supporting Classes === #
217
239
240
+ class TempFileHandler :
241
+ @staticmethod
242
+ def create_tmp_file (view = None , uid = None , region = None , auto_format_config = None , suffix = None ):
243
+ if not suffix :
244
+ uid , syntax = SyntaxHandler .get_assigned_syntax (view = view , uid = uid , region = region , auto_format_config = auto_format_config )
245
+ suffix = '.' + syntax if syntax else None
246
+
247
+ with tempfile .NamedTemporaryFile (mode = 'w+' , delete = False , suffix = suffix , dir = None , encoding = 'utf-8' ) as file :
248
+ file .write (ViewHandler .get_text_from_region (view = view , region = region ))
249
+ file .close ()
250
+ return file .name
251
+
252
+ @staticmethod
253
+ def remove_tmp_file (tmp_file = None ):
254
+ if tmp_file and os .path .isfile (tmp_file ):
255
+ os .unlink (tmp_file )
256
+
257
+
218
258
class FileHandler :
219
259
@staticmethod
220
260
def _is_valid_file (file = None ):
@@ -530,26 +570,6 @@ def query(data_dict, default=None, *keys):
530
570
return data_dict
531
571
532
572
533
- class TempFileHandler :
534
- @staticmethod
535
- def create_tmp_file (view = None , uid = None , region = None , auto_format_config = None , suffix = None ):
536
- if not suffix :
537
- uid , syntax = SyntaxHandler .get_assigned_syntax (view = view , uid = uid , region = region , auto_format_config = auto_format_config )
538
- suffix = '.' + syntax if syntax else None
539
-
540
- with tempfile .NamedTemporaryFile (mode = 'w+' , delete = False , suffix = suffix , dir = None , encoding = 'utf-8' ) as file :
541
- file .write (ViewHandler .get_text_from_region (view = view , region = region ))
542
- file .close ()
543
- return file .name
544
-
545
- return None
546
-
547
- @staticmethod
548
- def remove_tmp_file (tmp_file = None ):
549
- if tmp_file and os .path .isfile (tmp_file ):
550
- os .unlink (tmp_file )
551
-
552
-
553
573
class ModeHandler :
554
574
@staticmethod
555
575
def is_generic_mode (uid = None ):
@@ -1131,9 +1151,10 @@ class DataHandler:
1131
1151
_categories = {
1132
1152
'__sublime_preferences__' : {'key' : None , 'value' : None },
1133
1153
'__project_config__' : {'key' : None , 'value' : None },
1134
- '__save_paste_action__' : {'key' : None , 'value' : None }, # current action
1154
+ '__save_paste_action__' : {'key' : None , 'value' : None }, # current action state
1135
1155
'__auto_format_chain_item__' : {'key' : None , 'value' : None }, # current chaining item
1136
- '__auto_format_noop__' : {'key' : None , 'value' : None } # current no operation id
1156
+ '__auto_format_noop__' : {'key' : None , 'value' : None }, # current no operation id
1157
+ '__dir_format_stop__' : {'key' : None , 'value' : None } # current dir format state
1137
1158
}
1138
1159
1139
1160
@classmethod
0 commit comments