Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtajina committed Feb 27, 2012
0 parents commit c526c6e
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.sublime-workspace
*.pyc
3 changes: 3 additions & 0 deletions Default (Linux).sublime-keymap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
{ "keys": ["super+t"], "command": "open_related" }
]
3 changes: 3 additions & 0 deletions Default (OSX).sublime-keymap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
{ "keys": ["super+t"], "command": "open_related" }
]
3 changes: 3 additions & 0 deletions Default (Windows).sublime-keymap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
{ "keys": ["super+t"], "command": "open_related" }
]
28 changes: 28 additions & 0 deletions OpenRelated.sublime-project
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
}
}
9 changes: 9 additions & 0 deletions Preferences.sublime-settings
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"]
]
}
34 changes: 34 additions & 0 deletions converter.py
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
30 changes: 30 additions & 0 deletions converter_test.py
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()
25 changes: 25 additions & 0 deletions open_related.py
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."
5 changes: 5 additions & 0 deletions package-metadata.json
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."
}

0 comments on commit c526c6e

Please sign in to comment.