-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c526c6e
Showing
10 changed files
with
142 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,2 @@ | ||
*.sublime-workspace | ||
*.pyc |
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+t"], "command": "open_related" } | ||
] |
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+t"], "command": "open_related" } | ||
] |
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+t"], "command": "open_related" } | ||
] |
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,28 @@ | ||
{ | ||
"build_systems": | ||
[ | ||
{ | ||
"cmd": | ||
["python", "${project_path}/converter_test.py"], | ||
"name": "Unit tests", | ||
"working_dir": "${project_path}" | ||
} | ||
], | ||
"folders": | ||
[ | ||
{ | ||
"path": "." | ||
} | ||
], | ||
"settings": | ||
{ | ||
"open_related_patterns": | ||
[ | ||
[ | ||
"*_test.py", | ||
"*.py" | ||
] | ||
], | ||
"tab_size": 4 | ||
} | ||
} |
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,9 @@ | ||
{ | ||
"open_related_patterns": | ||
[ | ||
["*/src/*.js", "*/test/*Spec.js"], | ||
["*/src/*.js", "*/test/*Test.js"], | ||
["*/lib/*.js", "*/test/*Spec.js"], | ||
["*/lib/*.js", "*/test/*Test.js"] | ||
] | ||
} |
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,34 @@ | ||
import re | ||
|
||
|
||
class Converter(): | ||
def __init__(self, from_exp, to_exp): | ||
self.from_re = [re.compile(from_exp.replace("*", "(.*)"))] | ||
self.to_exp = [] | ||
|
||
if self._is_formatter(to_exp): | ||
self.from_re.append(re.compile(to_exp.replace("*", "(.*)"))) | ||
self.to_exp.append(self._pattern_to_formatter(to_exp)) | ||
self.to_exp.append(self._pattern_to_formatter(from_exp)) | ||
else: | ||
self.to_exp.append(to_exp) | ||
|
||
def convert(self, path): | ||
idx = 0 | ||
while idx < len(self.from_re): | ||
match = self.from_re[idx].match(path) | ||
if match: return self.to_exp[idx].format(*match.groups()) | ||
idx += 1 | ||
|
||
return None | ||
|
||
def _pattern_to_formatter(self, pattern): | ||
counter = [-1] | ||
def increment(m): | ||
counter[0] += 1 | ||
return "{" + str(counter[0]) + "}" | ||
|
||
return re.sub(r"\*", increment, pattern) | ||
|
||
def _is_formatter(self, exp): | ||
return exp.find("*") != -1 |
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,30 @@ | ||
import unittest | ||
from converter import Converter | ||
|
||
|
||
class ConverterTest(unittest.TestCase): | ||
|
||
def test_none_if_not_match(self): | ||
# should return None if given path does not match from pattern | ||
c = Converter("^abc", "{1}") | ||
self.assertEqual(c.convert("aaa"), None) | ||
|
||
def test_convert_basic(self): | ||
# should convert basic expression | ||
c = Converter("*/test/*Spec.js", "{0}/src/{1}.js") | ||
self.assertEqual(c.convert("some/path/test/oneSpec.js"), "some/path/src/one.js") | ||
|
||
def test_pattern_to_formatter(self): | ||
# should convert pattern (i.e. */test/*.js) to formatter | ||
c = Converter("", "") | ||
self.assertEqual(c._pattern_to_formatter("*/test.js"), "{0}/test.js") | ||
self.assertEqual(c._pattern_to_formatter("*/test/*.js"), "{0}/test/{1}.js") | ||
|
||
def test_double_pattern(self): | ||
# should allow double patterns | ||
c = Converter("*/test/unit/*.spec.coffee", "*/lib/*.js") | ||
self.assertEqual(c.convert("/some/test/unit/aaa/b.spec.coffee"), "/some/lib/aaa/b.js") | ||
self.assertEqual(c.convert("/other/lib/abc.js"), "/other/test/unit/abc.spec.coffee") | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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,25 @@ | ||
import sublime, sublime_plugin | ||
import os.path | ||
from converter import Converter | ||
|
||
class OpenRelatedCommand(sublime_plugin.WindowCommand): | ||
def run(self): | ||
win = self.window | ||
view = win.active_view() | ||
current_file = view.file_name() | ||
|
||
for pattern in view.settings().get('open_related_patterns', []): | ||
file = Converter(pattern[0], pattern[1]).convert(current_file) | ||
if file and os.path.exists(file): | ||
if win.num_groups() > 1: | ||
win.focus_group((win.active_group() + 1) % win.num_groups()) | ||
self.window.open_file(file) | ||
return | ||
|
||
sublime.status_message("Cannot find related file !") | ||
|
||
def is_enabled(self): | ||
return self.window.active_view() != None | ||
|
||
def description(self): | ||
return "Open related file." |
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,5 @@ | ||
{ | ||
"version": "0.0.1", | ||
"url": "https://github.com/vojtajina/sublime-OpenRelated", | ||
"description": "Configure relations between files and navigate quickly." | ||
} |