feat(SublimeText2.EditorPackages): cache packages
This commit is contained in:
1
EthanBrown.SublimeText2.EditorPackages/tools/PackageCache/Related Files/.gitignore
vendored
Normal file
1
EthanBrown.SublimeText2.EditorPackages/tools/PackageCache/Related Files/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.pyc
|
@@ -0,0 +1,3 @@
|
||||
[
|
||||
{ "caption": "Related Files", "command": "related_files" }
|
||||
]
|
@@ -0,0 +1,3 @@
|
||||
[
|
||||
{ "keys": ["ctrl+super+p"], "command": "related_files"}
|
||||
]
|
@@ -0,0 +1,27 @@
|
||||
[
|
||||
{
|
||||
"caption": "Preferences",
|
||||
"mnemonic": "n",
|
||||
"id": "preferences",
|
||||
"children":
|
||||
[
|
||||
{
|
||||
"caption": "Package Settings",
|
||||
"mnemonic": "P",
|
||||
"id": "package-settings",
|
||||
"children":
|
||||
[
|
||||
{
|
||||
"caption": "RelatedFiles",
|
||||
"children":
|
||||
[
|
||||
{ "command": "open_file", "args": {"file": "${packages}/Related Files/RelatedFiles.sublime-settings"}, "caption": "Settings – Default" },
|
||||
{ "command": "open_file", "args": {"file": "${packages}/User/RelatedFiles.sublime-settings"}, "caption": "Settings – User" },
|
||||
{ "caption": "-" }
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
@@ -0,0 +1,36 @@
|
||||
# Sublime Text 2 - Related Files
|
||||
|
||||

|
||||
|
||||
This plugin provides a quick list of related files to the currently open file.
|
||||
|
||||
My main use case is to list related files under a Ruby on Rails project. For example, for an opened "app/controllers/examples_controller.rb", related files would be "app/helpers/examples_helper.rb", "app/views/examples/**", and "spec/controllers/examples_controller_spec.rb".
|
||||
|
||||
This plugin was inspired by the existing [Open Related](https://github.com/vojtajina/sublime-OpenRelated) and [Rails Related Files](https://github.com/luqman/SublimeText2RailsRelatedFiles).
|
||||
|
||||
I wanted something between the two of them (a quick list of results that could be setup for any kinds of projects, not only Rails), so I created my own.
|
||||
|
||||
# Key Shortcut
|
||||
|
||||
The default shortcut is mapped to "ctrl+super+p". To change it to something more suitable for your needs, you can easily change that by copying the following and replacing the "keys" to your desired key combination:
|
||||
|
||||
```json
|
||||
{ "keys": ["ctrl+super+p"], "command": "related_files"}
|
||||
```
|
||||
|
||||
# Configuration
|
||||
|
||||
The plugins comes configured to lookup Rails related files, but you can add your own setups. Let's see an existing example:
|
||||
|
||||
```json
|
||||
// Test/specs for ruby files
|
||||
".+\/(app|lib)\/(.+).rb":
|
||||
[
|
||||
"spec/$2_spec.rb",
|
||||
"test/$2_test.rb"
|
||||
]
|
||||
```
|
||||
|
||||
The configuration has two parts: the key, which is a regular expression to match against the currently open file, and a list of globs to map the related files.
|
||||
|
||||
You can use the $1, $2, etc. on the glob strings to be replace by the extracted parts from the regex.
|
@@ -0,0 +1,84 @@
|
||||
{
|
||||
"patterns": {
|
||||
// Test/specs for ruby files
|
||||
".+\/(app|lib)\/(.+).rb":
|
||||
[
|
||||
"spec/$2_spec.rb",
|
||||
"test/$2_test.rb"
|
||||
],
|
||||
|
||||
// Ruby files for test/specs
|
||||
".+\/(test|spec)\/(.+)_(test|spec).rb":
|
||||
[
|
||||
"app/$2.rb",
|
||||
"lib/$2.rb"
|
||||
],
|
||||
|
||||
// Rails controllers
|
||||
".+\/app\/controllers\/(.+)_controller.rb":
|
||||
[
|
||||
"app/views/$1/**",
|
||||
"app/helpers/$1_helper.rb",
|
||||
"config/routes.rb",
|
||||
"spec/requests/$1_spec.rb",
|
||||
"spec/routing/$1_routing_spec.rb"
|
||||
],
|
||||
|
||||
// Rails helpers
|
||||
".+\/app\/helpers\/(.+)_helper.rb":
|
||||
[
|
||||
"app/views/$1/**",
|
||||
"app/controllers/$1_controller.rb",
|
||||
"config/routes.rb",
|
||||
"spec/requests/$1_spec.rb"
|
||||
],
|
||||
|
||||
// Rails views
|
||||
".+\/app\/views\/(.+)\/[^\/].+":
|
||||
[
|
||||
"app/views/$1/**",
|
||||
"app/controllers/$1_controller.rb",
|
||||
"app/helpers/$1_helper.rb",
|
||||
"config/routes.rb",
|
||||
"spec/controllers/$1_spec.rb",
|
||||
"spec/requests/$1_spec.rb"
|
||||
],
|
||||
|
||||
// Rails routes
|
||||
".+\/config\/routes.rb":
|
||||
[
|
||||
"spec/routing/**"
|
||||
],
|
||||
|
||||
// Rails libs
|
||||
".+\/(lib)\/(.+).rb":
|
||||
[
|
||||
"spec/lib/$2_spec.rb",
|
||||
"test/lib/$2_test.rb"
|
||||
],
|
||||
|
||||
// Rails controllers specs
|
||||
".+/spec/controllers/(.+)_controller_spec.rb":
|
||||
[
|
||||
"app/controllers/$1_controller.rb",
|
||||
"app/helpers/$1_helper.rb",
|
||||
"app/views/$1/**",
|
||||
"config/routes.rb"
|
||||
],
|
||||
|
||||
// Rails request specs
|
||||
".+/spec/requests/(.+)_spec.rb":
|
||||
[
|
||||
"app/controllers/$1_controller.rb",
|
||||
"app/helpers/$1_helper.rb",
|
||||
"app/views/$1/**",
|
||||
"config/routes.rb"
|
||||
],
|
||||
|
||||
// Rails libs specs
|
||||
".+/spec/lib/(.+)_spec.rb":
|
||||
[
|
||||
"lib/$1.rb"
|
||||
]
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
class ExamplesController < ApplicationController
|
||||
|
||||
end
|
@@ -0,0 +1 @@
|
||||
{"url": "https://github.com/fabiokr/sublime-related-files", "version": "2013.03.15.08.29.58", "description": "A Sublime Text 2 plugin to list related files"}
|
@@ -0,0 +1,86 @@
|
||||
import os
|
||||
import re
|
||||
import glob
|
||||
import itertools
|
||||
|
||||
|
||||
class Related(object):
|
||||
# Initializes the RelatedFiles object.
|
||||
#
|
||||
# file_path - the file to look related files for
|
||||
# patterns - a dictionary of patterns in the following format:
|
||||
# {"(.+)_controller.rb": ["*/the/paths/$1/**", "*/test/$1_controller_test.rb"]}
|
||||
#
|
||||
# The glob paths will have their $i replaced by the matched groups within the file name
|
||||
# matcher.
|
||||
def __init__(self, file_path, patterns, folders):
|
||||
self.__file_path = file_path
|
||||
self.__patterns = patterns
|
||||
self.__root = self.__root(folders)
|
||||
self.__files = []
|
||||
self.__descriptions = []
|
||||
self.__build()
|
||||
|
||||
# # Retrieves a list of all related descriptions.
|
||||
def descriptions(self):
|
||||
return self.__descriptions
|
||||
|
||||
# # Retrieves a list of all related files paths.
|
||||
def files(self):
|
||||
return self.__files
|
||||
|
||||
# Builds a list with all related files and sets self.descriptions and
|
||||
# self.files.
|
||||
def __build(self):
|
||||
files = set()
|
||||
|
||||
file_path = self.__to_posixpath(self.__file_path)
|
||||
|
||||
# for each matching pattern
|
||||
for regex, paths in self.__patterns.iteritems():
|
||||
match = re.compile(regex).match(file_path)
|
||||
if match:
|
||||
# returns a flattened file list
|
||||
files.update(self.__files_for_paths(regex, match, paths))
|
||||
|
||||
# sorts items
|
||||
files = list(files)
|
||||
files.sort()
|
||||
|
||||
self.__files = files
|
||||
self.__descriptions = [self.__file_without_root(file) for file in files]
|
||||
|
||||
# Returns the root folder for the given file and folders
|
||||
def __root(self, folders):
|
||||
for folder in folders:
|
||||
if self.__file_path.startswith(os.path.join(folder, "")):
|
||||
return folder
|
||||
|
||||
# Retrieves a list of files fot the given match and paths
|
||||
def __files_for_paths(self, regex, match, paths):
|
||||
paths = [self.__replaced_path(match, path) for path in paths]
|
||||
|
||||
files = [glob.glob(os.path.join(self.__root, path)) for path in paths]
|
||||
flattened = [self.__to_posixpath(path) for path in list(itertools.chain.from_iterable(files))]
|
||||
|
||||
# Ignores current file
|
||||
if self.__file_path in flattened:
|
||||
flattened.remove(unicode(self.__file_path))
|
||||
|
||||
return flattened
|
||||
|
||||
# Retrieves the file name without the root part.
|
||||
def __file_without_root(self, file):
|
||||
return os.path.basename(self.__root) + file[len(self.__root):]
|
||||
|
||||
# Retrieves a path with its interpolation vars replaces by the found groups
|
||||
# on match.
|
||||
def __replaced_path(self, match, path):
|
||||
replaced_path = path
|
||||
for i, group in enumerate(match.groups()):
|
||||
replaced_path = replaced_path.replace("$%s" % (i + 1), group)
|
||||
return replaced_path
|
||||
|
||||
# Converts paths to posixpaths.
|
||||
def __to_posixpath(self, path):
|
||||
return re.sub("\\\\", "/", path)
|
@@ -0,0 +1,39 @@
|
||||
import sublime
|
||||
import sublime_plugin
|
||||
from related import *
|
||||
|
||||
|
||||
class RelatedFilesCommand(sublime_plugin.WindowCommand):
|
||||
def run(self, index=None):
|
||||
active_file_path = self.__active_file_path()
|
||||
|
||||
if active_file_path:
|
||||
# Builds a list of related files for the current open file.
|
||||
self.__related = Related(active_file_path, self.__patterns(), sublime.active_window().folders())
|
||||
|
||||
self.window.show_quick_panel(self.__related.descriptions(), self.__open_file)
|
||||
else:
|
||||
self.__status_msg("No open files")
|
||||
|
||||
# Opens the file in path.
|
||||
def __open_file(self, index):
|
||||
if index >= 0:
|
||||
self.window.open_file(self.__related.files()[index])
|
||||
else:
|
||||
self.__status_msg("No related files found")
|
||||
|
||||
# Retrieves the patterns from settings.
|
||||
def __patterns(self):
|
||||
return sublime.load_settings("RelatedFiles.sublime-settings").get('patterns')
|
||||
|
||||
# Returns the activelly open file path from sublime.
|
||||
def __active_file_path(self):
|
||||
if self.window.active_view():
|
||||
file_path = self.window.active_view().file_name()
|
||||
|
||||
if file_path and len(file_path) > 0:
|
||||
return file_path
|
||||
|
||||
# Displays a status message on sublime.
|
||||
def __status_msg(self, message):
|
||||
sublime.status_message(message)
|
@@ -0,0 +1,52 @@
|
||||
import unittest
|
||||
import os
|
||||
from related import *
|
||||
|
||||
|
||||
class RelatedTest(unittest.TestCase):
|
||||
|
||||
def test_descriptions_with_matches(self):
|
||||
self.assertEqual(self.__related().descriptions(), [
|
||||
"example1/app/helpers/examples_helper.rb",
|
||||
"example1/app/views/examples/index.html",
|
||||
"example1/app/views/examples/show.html",
|
||||
"example1/test/controllers/examples_controller_test.rb"
|
||||
])
|
||||
|
||||
def test_descriptions_without_matches(self):
|
||||
self.assertEqual(self.__related_without_match().descriptions(), [])
|
||||
|
||||
def test_files_with_matches(self):
|
||||
self.assertEqual(self.__related().files(), [
|
||||
self.__expand("fixtures/example1/app/helpers/examples_helper.rb"),
|
||||
self.__expand("fixtures/example1/app/views/examples/index.html"),
|
||||
self.__expand("fixtures/example1/app/views/examples/show.html"),
|
||||
self.__expand("fixtures/example1/test/controllers/examples_controller_test.rb")
|
||||
])
|
||||
|
||||
def test_files_without_matches(self):
|
||||
self.assertEqual(self.__related_without_match().files(), [])
|
||||
|
||||
def __patterns(self):
|
||||
return {
|
||||
".+\/app\/controllers\/(.+)_controller.rb": ["app/views/$1/**", "app/helpers/$1_helper.rb"],
|
||||
".+\/app\/(.+).rb": ["test/$1_test.rb"]
|
||||
}
|
||||
|
||||
def __file(self):
|
||||
return self.__expand("fixtures/example1/app/controllers/examples_controller.rb")
|
||||
|
||||
def __folders(self):
|
||||
return [self.__expand("fixtures/example1"), self.__expand("fixtures/example2")]
|
||||
|
||||
def __expand(self, path):
|
||||
return os.path.join(os.path.dirname(os.path.realpath(__file__)), path)
|
||||
|
||||
def __related(self):
|
||||
return Related(self.__file(), self.__patterns(), self.__folders())
|
||||
|
||||
def __related_without_match(self):
|
||||
return Related("/should/not/match", self.__patterns(), self.__folders())
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Binary file not shown.
After Width: | Height: | Size: 22 KiB |
Reference in New Issue
Block a user