Skip to content

Commit 9a5f3b6

Browse files
azizkprincemaple
authored andcommitted
Commands: added MixTestSwitchToCodeOrTestCommand.
1 parent f035ac0 commit 9a5f3b6

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Use the default shortcut `Alt+Shift+F` or the palette command `Mix Format: File`
9191
- `Mix Test: Repeat`
9292
- `Mix Test: Set --seed`
9393
- `Mix Test: Toggle --stale Flag`
94+
- `Mix Test: Switch to Code or Test`
9495
- `Mix Test: Show Panel`
9596
- `Mix Format: File`
9697
- `Mix Format: Project / Folder`

commands/Default.sublime-commands

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
{ "caption": "Mix Test: Repeat", "command": "mix_test_repeat" },
1212
{ "caption": "Mix Test: Set --seed", "command": "mix_test_set_seed" },
1313
{ "caption": "Mix Test: Toggle --stale Flag", "command": "mix_test_toggle_stale_flag" },
14+
{ "caption": "Mix Test: Switch to Code or Test", "command": "mix_test_switch_to_code_or_test" },
1415
{ "caption": "Mix Test: Show Panel", "command": "mix_test_show_panel" },
1516
{ "caption": "Mix Format: File", "command": "mix_format_file" },
1617
{ "caption": "Mix Format: Project / Folder", "command": "mix_format_project" },

commands/mix_test.py

+42
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import shlex
55
import re
66
from os import path, fstat
7+
from pathlib import Path
78
from time import time as now
89
from datetime import datetime
910
from .utils import *
@@ -223,6 +224,47 @@ def run(self, **_kwargs):
223224
save_json_file(mix_settings_path, mix_params)
224225
print_status_msg('%s mix test --stale flag!' % ['Added', 'Removed'][has_stale_flag])
225226

227+
class MixTestSwitchToCodeOrTestCommand(sublime_plugin.TextCommand):
228+
def description(self):
229+
return 'Finds the corresponding source file of the test and vice versa if possible.'
230+
231+
def run(self, _edit):
232+
window = self.view.window()
233+
file_path = Path(self.view.file_name())
234+
parts = file_path.name.rsplit('_test.exs', 1)
235+
is_test = parts[1:] == ['']
236+
search_names = \
237+
[parts[0] + ext for ext in ('.ex', '.exs')] if is_test else [file_path.stem + '_test.exs']
238+
239+
counterpart_paths = [
240+
(folder, p)
241+
for folder in window.folders()
242+
for p in Path(folder).rglob("*.ex*")
243+
if p.name in search_names
244+
]
245+
246+
if len(counterpart_paths) > 1:
247+
on_select = lambda i: i >= 0 and window.open_file(str(counterpart_paths[i][1]))
248+
249+
file_path_items = [
250+
sublime.QuickPanelItem(
251+
trigger=str(path.relative_to(folder)),
252+
details='Folder: %s' % folder,
253+
kind=sublime.KIND_NAVIGATION
254+
)
255+
for folder, path in counterpart_paths
256+
]
257+
258+
window.show_quick_panel(file_path_items, on_select)
259+
elif counterpart_paths:
260+
window.open_file(str(counterpart_paths[0][1]))
261+
else:
262+
test_or_code = ['test', 'code'][is_test]
263+
print_status_msg('Error: could not find the counterpart %s file.' % test_or_code)
264+
265+
def is_enabled(self):
266+
return is_elixir_syntax(self.view)
267+
226268
class MixTestShowPanelCommand(sublime_plugin.WindowCommand):
227269
def description(self):
228270
return 'Shows the output panel if existent and hidden.'

0 commit comments

Comments
 (0)