|
4 | 4 | import shlex
|
5 | 5 | import re
|
6 | 6 | from os import path, fstat
|
| 7 | +from pathlib import Path |
7 | 8 | from time import time as now
|
8 | 9 | from datetime import datetime
|
9 | 10 | from .utils import *
|
@@ -223,6 +224,47 @@ def run(self, **_kwargs):
|
223 | 224 | save_json_file(mix_settings_path, mix_params)
|
224 | 225 | print_status_msg('%s mix test --stale flag!' % ['Added', 'Removed'][has_stale_flag])
|
225 | 226 |
|
| 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 | + |
226 | 268 | class MixTestShowPanelCommand(sublime_plugin.WindowCommand):
|
227 | 269 | def description(self):
|
228 | 270 | return 'Shows the output panel if existent and hidden.'
|
|
0 commit comments