Skip to content

Commit ca6b0e7

Browse files
authored
Allow to edit the command before running it (#16)
1 parent c9887fd commit ca6b0e7

5 files changed

Lines changed: 54 additions & 9 deletions

File tree

Default.sublime-commands

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,41 @@
2222
"command": "any_test_run",
2323
"args": { "scope": "line" }
2424
},
25+
{
26+
"caption": "AnyTest: Edit nearest",
27+
"command": "any_test_run",
28+
"args": { "scope": "line", "edit": true }
29+
},
2530
{
2631
"caption": "AnyTest: Test file",
2732
"command": "any_test_run",
2833
"args": { "scope": "file" }
2934
},
35+
{
36+
"caption": "AnyTest: Edit file",
37+
"command": "any_test_run",
38+
"args": { "scope": "file", "edit": true }
39+
},
3040
{
3141
"caption": "AnyTest: Test suite",
3242
"command": "any_test_run",
3343
"args": { "scope": "suite" }
3444
},
45+
{
46+
"caption": "AnyTest: Edit suite",
47+
"command": "any_test_run",
48+
"args": { "scope": "suite", "edit": true }
49+
},
3550
{
3651
"caption": "AnyTest: Run last",
3752
"command": "any_test_run",
3853
"args": { "scope": "last" }
3954
},
55+
{
56+
"caption": "AnyTest: Edit last",
57+
"command": "any_test_run",
58+
"args": { "scope": "last", "edit": true }
59+
},
4060
{
4161
"caption": "AnyTest: Show last output",
4262
"command": "any_test_show_last_output",

Default.sublime-keymap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
]
1717
},
1818
// { "keys": ["primary+j", "primary+n"], "command": "any_test_run", "args": { "scope": "line" } },
19+
// { "keys": ["primary+j", "primary+shift+n"], "command": "any_test_run", "args": { "scope": "line", "edit": true } },
1920
// { "keys": ["primary+j", "primary+f"], "command": "any_test_run", "args": { "scope": "file" } },
21+
// { "keys": ["primary+j", "primary+shift+f"], "command": "any_test_run", "args": { "scope": "file", "edit": true } },
2022
// { "keys": ["primary+j", "primary+s"], "command": "any_test_run", "args": { "scope": "suite" } },
23+
// { "keys": ["primary+j", "primary+shift+s"], "command": "any_test_run", "args": { "scope": "suite", "edit": true } },
2124
// { "keys": ["primary+j", "primary+l"], "command": "any_test_run", "args": { "scope": "last" } },
25+
// { "keys": ["primary+j", "primary+shift+l"], "command": "any_test_run", "args": { "scope": "last", "edit": true } },
2226
// { "keys": ["primary+j", "primary+k"], "command": "any_test_show_last_output", "args": { "focus": true } },
2327
// { "keys": ["primary+j", "primary+e"], "command": "any_test_edit_last" },
2428
]

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ There are 4 main commands that the package exposes:
3131
- `AnyTest: Test nearest`: runs the test nearest to the current line(cursor)
3232
- `AnyTest: Run last`: runs the last test
3333

34+
Any of the commands above supports the `edit` flag that allows editing the command before running it.
35+
3436
The package comes with polyfills for test frameworks that don't have built-in support for running tests for the current line.
3537
All the polyfills have been borrowed from the `vim-test` plugin and adapted for Sublime Text.
3638

@@ -135,7 +137,6 @@ Using `unittesting` only makes sense with PyUnit test framework, so it is usuall
135137
- Add more test frameworks (the end goal is to at least support all test frameworks that `vim-test` supports)
136138
- Allow to explicitly select a test framework for the given file/project
137139
- Run tests from the Side Bar (including testing folders)
138-
- Allow to edit a test command before running it
139140
- Potentially integrate the package with [Sublime Debugger](https://github.com/daveleroy/sublime_debugger)
140141

141142
## Contribution

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020

2121
class AnyTestRunCommand(sublime_plugin.TextCommand):
22-
def run(self, _, scope='file'):
23-
Plugin(self.view).run_test(scope)
22+
def run(self, _, scope='file', edit=False):
23+
Plugin(self.view).run_test(scope, edit)
2424

2525

2626
class AnyTestShowLastOutputCommand(sublime_plugin.ApplicationCommand):

plugin/__init__.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from . import runners, settings, test_frameworks
66
from .context import Context
7-
from .errors import handle_errors
7+
from .errors import Error, handle_errors
88
from .history import History
99
from .view_callbacks import ViewCallbacks
1010

@@ -31,6 +31,7 @@ def edit_last(cls):
3131

3232
def __init__(self, view):
3333
self.view = view
34+
self.runner = None
3435

3536
def build_runner(self, scope):
3637
if scope == SCOPE_LAST:
@@ -43,13 +44,32 @@ def build_runner(self, scope):
4344
return runner.build(test_framework(context), scope)
4445

4546
@handle_errors
46-
def run_test(self, scope):
47+
def run_test(self, scope, edit=False):
4748
settings.reload_project_settings()
4849

49-
runner = self.build_runner(scope)
50+
self.runner = self.build_runner(scope)
51+
52+
if edit:
53+
self.view.window().show_input_panel(
54+
'Command',
55+
self.runner.cmd,
56+
self.process_runner,
57+
lambda _: None,
58+
lambda: None,
59+
)
60+
else:
61+
self.process_runner()
62+
63+
@handle_errors
64+
def process_runner(self, command=''):
65+
if self.runner is None:
66+
raise Error('Runner is not set')
67+
68+
if bool(command):
69+
self.runner = self.runner._replace(cmd=command)
5070

5171
ViewCallbacks(self.view).run()
5272

53-
logger.debug("Running '%s' from '%s'", runner.cmd, runner.dir)
54-
runner.run()
55-
history.add(runner)
73+
logger.debug("Running '%s' from '%s'", self.runner.cmd, self.runner.dir)
74+
self.runner.run()
75+
history.add(self.runner)

0 commit comments

Comments
 (0)