forked from pivotalexperimental/jasmine-tmbundle
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding cmd-period to switch between code and spec
- Loading branch information
Showing
5 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
{ "keys": ["ctrl+period"], "command": "jasmine_switch_between_code_and_spec" } // switch between code and test | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
{ "keys": ["super+period"], "command": "jasmine_switch_between_code_and_spec" } // switch between code and test | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
{ "keys": ["ctrl+period"], "command": "jasmine_switch_between_code_and_spec" } // switch between code and test | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
{ "caption": "Jasmine: Switch between code and spec", "command": "jasmine_switch_between_code_and_spec" } | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import os | ||
import re | ||
import thread | ||
import subprocess | ||
import functools | ||
import time | ||
import sublime | ||
import sublime_plugin | ||
|
||
class JasmineSwitchBetweenCodeAndSpec(sublime_plugin.TextCommand): | ||
def window(self): | ||
return self.view.window() | ||
|
||
class BaseFile(object): | ||
def __init__(self, file_name): self.folder_name, self.file_name = os.path.split(file_name) | ||
def possible_alternate_files(self): return [] | ||
|
||
class JavascriptFile(BaseFile): | ||
def possible_alternate_files(self): return [self.file_name.replace(".js", "_spec.js")] | ||
|
||
class JasmineFile(BaseFile): | ||
def possible_alternate_files(self): return [self.file_name.replace("_spec.js", ".js")] | ||
|
||
def current_file(self): | ||
file_name = self.view.file_name() | ||
if re.search('\w+\_spec.js', file_name): | ||
return JasmineSwitchBetweenCodeAndSpec.JasmineFile(file_name) | ||
elif re.search('\w+\.js', file_name): | ||
return JasmineSwitchBetweenCodeAndSpec.JavascriptFile(file_name) | ||
else: | ||
return JasmineSwitchBetweenCodeAndSpec.BaseFile(file_name) | ||
|
||
def is_enabled(self): | ||
return self.current_file().possible_alternate_files() | ||
|
||
def run(self, args): | ||
possible_alternates = self.current_file().possible_alternate_files() | ||
alternates = self.project_files(lambda(file): file in possible_alternates) | ||
if alternates: | ||
self.window().open_file(alternates.pop()) | ||
else: | ||
sublime.error_message("could not find " + str(possible_alternates)) | ||
|
||
def walk(self, directory, ignored_directories = []): | ||
ignored_directories = ['.git', 'vendor'] # Move this into config | ||
for dir, dirnames, files in os.walk(directory): | ||
dirnames[:] = [dirname for dirname in dirnames if dirname not in ignored_directories] | ||
yield dir, dirnames, files | ||
|
||
def project_files(self, file_matcher): | ||
directories = self.window().folders() | ||
return [os.path.join(dirname, file) for directory in directories for dirname, _, files in self.walk(directory) for file in filter(file_matcher, files)] |