Skip to content

Commit fb51bdf

Browse files
azizkprincemaple
authored andcommitted
Commands: refactored / improved some code.
- Renamed command caption to `Mix Test: Set --seed` - Added palette command `Mix Test: All` - Renamed unused `kwargs` -> `_kwargs` - Renamed save_json_settings() -> save_json_file() - Renamed load_json_settings() -> load_json_file() - Improved exception handling in json functions
1 parent 0388f18 commit fb51bdf

File tree

5 files changed

+34
-30
lines changed

5 files changed

+34
-30
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@ Use the default shortcut `Alt+Shift+F` or the palette command `Mix Format: File`
8484

8585
- `ElixirSyntax: Settings`
8686
- `Mix Test: Settings`
87+
- `Mix Test: All`
8788
- `Mix Test: File`
8889
- `Mix Test: Selection(s)`
8990
- `Mix Test: Failed`
9091
- `Mix Test: Repeat`
91-
- `Mix Test: Set Seed`
92+
- `Mix Test: Set --seed`
9293
- `Mix Test: Toggle --stale Flag`
9394
- `Mix Format: File`
9495
- `Mix Format: Project / Folder`

commands/Default.sublime-commands

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
"default": "{\n $0\n}\n"
55
} },
66
{ "caption": "Mix Test: Settings", "command": "mix_test_settings" },
7+
{ "caption": "Mix Test: All", "command": "mix_test" },
78
{ "caption": "Mix Test: File", "command": "mix_test_file" },
89
{ "caption": "Mix Test: Selection(s)", "command": "mix_test_selection" },
910
{ "caption": "Mix Test: Failed", "command": "mix_test_failed" },
1011
{ "caption": "Mix Test: Repeat", "command": "mix_test_repeat" },
11-
{ "caption": "Mix Test: Set Seed", "command": "mix_test_set_seed" },
12+
{ "caption": "Mix Test: Set --seed", "command": "mix_test_set_seed" },
1213
{ "caption": "Mix Test: Toggle --stale Flag", "command": "mix_test_toggle_stale_flag" },
1314
{ "caption": "Mix Format: File", "command": "mix_format_file" },
1415
{ "caption": "Mix Format: Project / Folder", "command": "mix_format_project" },

commands/mix_format.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class MixFormatProjectCommand(sublime_plugin.WindowCommand):
1212
def description(self):
1313
return 'Runs `mix format` on the project path or the opened folder.'
1414

15-
def run(self, **kwargs):
15+
def run(self, **_kwargs):
1616
call_mix_format(self.window)
1717

1818
class MixFormatFileCommand(sublime_plugin.TextCommand):
@@ -32,7 +32,7 @@ class MixFormatToggleAutoFormatCommand(sublime_plugin.TextCommand):
3232
def description(self):
3333
return 'Enables or disables auto-formatting on save.'
3434

35-
def run(self, _edit, **kwargs):
35+
def run(self, _edit, **_kwargs):
3636
package_settings, mix_format_settings = load_mix_format_settings()
3737
on_save = mix_format_settings['on_save'] = not mix_format_settings.get('on_save', False)
3838
package_settings.set('mix_format', mix_format_settings)

commands/mix_test.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class MixTestSettingsCommand(sublime_plugin.WindowCommand):
2222
def description(self):
2323
return 'Opens the `mix test` settings file for the current project.'
2424

25-
def run(self, **kwargs):
25+
def run(self, **_kwargs):
2626
abs_file_path = self.window.active_view().file_name()
2727
window_vars = self.window.extract_variables()
2828
mix_settings_path = reverse_find_json_path(self.window, FILE_NAMES.SETTINGS_JSON)
2929

3030
if mix_settings_path:
3131
if not path.exists(mix_settings_path):
32-
save_json_settings(mix_settings_path, add_help_info({'args': []}))
32+
save_json_file(mix_settings_path, add_help_info({'args': []}))
3333
sublime_NewFileFlags_NONE = 4
3434
self.window.open_file(mix_settings_path, flags=sublime_NewFileFlags_NONE)
3535
else:
@@ -42,14 +42,14 @@ class MixTestCommand(sublime_plugin.WindowCommand):
4242
def description(self):
4343
return 'Runs the full test-suite with `mix test`.'
4444

45-
def run(self, **kwargs):
45+
def run(self, **_kwargs):
4646
call_mix_test_with_settings(self.window)
4747

4848
class MixTestFileCommand(sublime_plugin.WindowCommand):
4949
def description(self):
5050
return 'Runs `mix test` on the current test file.'
5151

52-
def run(self, **kwargs):
52+
def run(self, **_kwargs):
5353
abs_file_path = self.window.active_view().file_name()
5454
assert_is_test_file(abs_file_path)
5555
call_mix_test_with_settings(self.window, abs_file_path=abs_file_path)
@@ -156,18 +156,18 @@ class MixTestFailedCommand(sublime_plugin.WindowCommand):
156156
def description(self):
157157
return 'Repeats only tests that failed the last time.'
158158

159-
def run(self, **kwargs):
159+
def run(self, **_kwargs):
160160
call_mix_test_with_settings(self.window, failed=True)
161161

162162
class MixTestRepeatCommand(sublime_plugin.WindowCommand):
163163
def description(self):
164164
return 'Repeats `mix test` with the last used parameters.'
165165

166-
def run(self, **kwargs):
166+
def run(self, **_kwargs):
167167
json_path = reverse_find_json_path(self.window, path.join('_build', FILE_NAMES.REPEAT_JSON))
168168

169169
if json_path:
170-
call_mix_test_with_settings(self.window, **load_json_settings(json_path))
170+
call_mix_test_with_settings(self.window, **load_json_file(json_path))
171171
else:
172172
print_status_msg('Error: No tests to repeat.')
173173

@@ -180,7 +180,7 @@ def run(self, _edit, seed=None):
180180
if not mix_settings_path:
181181
return
182182

183-
mix_params = load_json_settings(mix_settings_path)
183+
mix_params = load_json_file(mix_settings_path)
184184
seed = self.view.substr(self.view.sel()[0]) if seed is None else seed
185185
seed = seed.strip() if type(seed) == str else seed
186186
msg = None
@@ -193,7 +193,7 @@ def run(self, _edit, seed=None):
193193
msg = 'Erased mix test seed.'
194194
'seed' in mix_params and mix_params.pop('seed')
195195

196-
save_json_settings(mix_settings_path, add_help_info(mix_params))
196+
save_json_file(mix_settings_path, add_help_info(mix_params))
197197

198198
print_status_msg(msg or 'Error: cannot set mix test seed to: %s' % repr(seed))
199199

@@ -209,16 +209,16 @@ class MixTestToggleStaleFlagCommand(sublime_plugin.WindowCommand):
209209
def description(self):
210210
return 'Toggles the --stale flag.'
211211

212-
def run(self, **kwargs):
212+
def run(self, **_kwargs):
213213
mix_settings_path = reverse_find_json_path(self.window, FILE_NAMES.SETTINGS_JSON)
214214
if not mix_settings_path:
215215
return
216-
mix_params = load_json_settings(mix_settings_path)
216+
mix_params = load_json_file(mix_settings_path)
217217
args = mix_params.get('args', [])
218218
has_stale_flag = '--stale' in args
219219
args = [a for a in args if a != '--stale'] if has_stale_flag else args + ['--stale']
220220
mix_params['args'] = args
221-
save_json_settings(mix_settings_path, mix_params)
221+
save_json_file(mix_settings_path, mix_params)
222222
print_status_msg('%s mix test --stale flag!' % ['Added', 'Removed'][has_stale_flag])
223223

224224

@@ -242,6 +242,8 @@ def get_test_block_regions(view, header_region, lookup_table):
242242
point, view_size = name_region.b, view.size()
243243
begin_scopes_counter = 0
244244

245+
# TODO: use view.expand_to_scope() when available?
246+
245247
while point < view_size:
246248
token_region = view.extract_scope(point)
247249
token_str = view.substr(token_region)
@@ -390,9 +392,9 @@ def call_mix_test_with_settings(window, **params):
390392
params.setdefault('file_path', path.relpath(params['abs_file_path'], root_dir))
391393
del params['abs_file_path']
392394

393-
save_json_settings(path.join(build_dir, FILE_NAMES.REPEAT_JSON), params)
395+
save_json_file(path.join(build_dir, FILE_NAMES.REPEAT_JSON), params)
394396

395-
mix_params = load_json_settings(mix_settings_path)
397+
mix_params = load_json_file(mix_settings_path)
396398
mix_params = remove_help_info(mix_params)
397399
mix_params.update(params)
398400
mix_params.setdefault('cwd', root_dir)

commands/utils.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,25 @@ def reverse_find_root_folder(bottom_path):
3737

3838
return None
3939

40-
def save_json_settings(file_path, dict_data):
40+
def save_json_file(file_path, dict_data):
4141
try:
42-
with open(file_path, 'w') as file:
42+
with open(str(file_path), 'w') as file:
4343
try:
4444
return json.dump(dict_data, file, indent=2)
45-
except:
46-
print_status_msg('Error: could not save JSON to: %s' % file_path)
47-
except:
48-
print_status_msg('Error: could not open file: %s' % file_path)
45+
except BaseException as e:
46+
print_status_msg('Error: could not save JSON to: %r\nException: %s' % (file_path, e))
47+
except BaseException as e:
48+
print_status_msg('Error: could not open file: %r\nException: %s' % (file_path, e))
4949

50-
def load_json_settings(file_path):
50+
def load_json_file(file_path):
5151
try:
52-
with open(file_path, 'r') as file:
52+
with open(str(file_path), 'r') as file:
5353
try:
5454
return json.load(file)
55-
except:
56-
print_status_msg('Error: could not load JSON from: %s' % file_path)
57-
except:
55+
except BaseException as e:
56+
print_status_msg('Error: could not load JSON from: %r\nException: %s' % (file_path, e))
57+
except BaseException as e:
5858
exists = Path(file_path).exists()
59-
exists and print_status_msg('Error: could not open file: %s' % file_path)
59+
exists and print_status_msg('Error: could not open file: %r\nException: %s' % (file_path, e))
6060

6161
return {}

0 commit comments

Comments
 (0)