Skip to content
Browse files

Initial Commit

  • Loading branch information...
0 parents commit a5db9023dfa4a64cd877def30bd1a38e0c934bec @edubkendo committed Aug 30, 2012
27 .gitignore
@@ -0,0 +1,27 @@
+*.py[co]
+
+# Packages
+*.egg
+*.egg-info
+dist
+build
+eggs
+parts
+bin
+var
+sdist
+develop-eggs
+.installed.cfg
+
+# Installer logs
+pip-log.txt
+
+# Unit test / coverage reports
+.coverage
+.tox
+
+#Translations
+*.mo
+
+#Mr Developer
+.mr.developer.cfg
4 Context.sublime-menu
@@ -0,0 +1,4 @@
+[
+ { "caption": "-" , "id":"side-bar-end-separator"},
+ { "caption": "Find Function Definition", "command": "go_function"}
+]
5 Default (Linux).sublime-keymap
@@ -0,0 +1,5 @@
+[
+ {
+ "keys": ["f8"], "command": "go_function"
+ }
+]
5 Default (OSX).sublime-keymap
@@ -0,0 +1,5 @@
+[
+ {
+ "keys": ["f8"], "command": "go_function"
+ }
+]
5 Default (Windows).sublime-keymap
@@ -0,0 +1,5 @@
+[
+ {
+ "keys": ["f8"], "command": "go_function"
+ }
+]
6 README.md
@@ -0,0 +1,6 @@
+sublime-find-function-definition
+================================
+
+This is an adaptation of Tim Douglass's [Find Function Definition](https://github.com/timdouglas/sublime-find-function-definition) to work with CoffeeScript. I don't really know python, so I didn't do much of the work myself. The check_dirs function which weeds out javascript directories was written by [@BlackDragonHunt](https://github.com/BlackDragonHunt). The regex by [@ACEfanatic02](https://github.com/ACEfanatic02). There could very easily be edge cases this doesn't catch, but for the most part, it should find your function for you. Just HIGHLIGHT the name of a function, press f8 (or set your own keybinding) and the plugin will open the file and take you to the function's definition.
+
+Sublime Text 2 plugin to find and open a function's declaration in your project
74 filter_dirs.py
@@ -0,0 +1,74 @@
+import os
+
+DIRS_TO_SKIP = [".git", "lib", "node_modules", "public", "vendor"]
+
+def check_dir(dir):
+ split = split_path(dir)
+
+ if [skip for skip in DIRS_TO_SKIP if skip in split]:
+ return False
+
+ else:
+ return True
+
+def filter_dirs(dirs):
+
+ filtered = []
+ for path in dirs:
+ split = split_path(path)
+
+ if [skip for skip in DIRS_TO_SKIP if skip in split]:
+ #print "Skipped", path
+ pass
+
+ else:
+ #print "Kept", path
+ filtered.append(path)
+
+ return filtered
+
+# Modified from http://stackoverflow.com/a/3167684
+def split_path(path):
+
+ folders = []
+ while True:
+ new_path, folder = os.path.split(path)
+
+ if folder != "":
+ folders.append(folder)
+
+ elif new_path == path:
+ break
+
+ path = new_path
+
+ folders.reverse()
+ return folders
+
+if __name__ == "__main__":
+ test_dirs = [
+ " ~/projects/projects/towerApps/towerBot/node_modules/pathfinder/node_modules/detective/node_modules/esprima/esprima.js",
+ " /home/projects/projects/towerApps/towerBot/node_modules/pathfinder/node_modules/detective/node_modules/esprima/esprima.js",
+ " home/projects/projects/towerApps/towerBot/node_modules/pathfinder/node_modules/detective/node_modules/esprima/esprima.js",
+ "/home/bt/.config/sublime-text-2/Packages/folder_test",
+ "/home/bt/.config/sublime-text-2/Packages/lib",
+ "/home/bt/.config/sublime-text-2/Packages/lib/subdir",
+ "lib",
+ "node_modules",
+ "/node_modules",
+ "/node_modules/",
+ "node_modules/",
+ "dont_skip",
+ "safe",
+ "alsosafe",
+ "not_safe/lib/wow/lib",
+ "safe/glib",
+ "safe/libg",
+ "safe/node_modules2",
+ "safe/2node_modules/",
+ "safe\\2node/modules\/test",
+ "//\\\/\\safe\\//\\2node/modules\/test",
+ "\\lib",
+ "lib\\",
+ "lib\\node_modules",
+ ]
99 go2function.py
@@ -0,0 +1,99 @@
+import sublime, sublime_plugin
+import subprocess
+import re
+import functools
+import os
+from filter_dirs import check_dir
+
+#borrowed from Git Plugin by David Lynch
+#https://github.com/kemayo/sublime-text-2-git/
+def do_when(conditional, callback, *args, **kwargs):
+ if conditional():
+ return callback(*args, **kwargs)
+ sublime.set_timeout(functools.partial(do_when, conditional, callback, *args, **kwargs), 50)
+
+#Gets current word and performs a grep on project folders
+#to see if it has a function definition or not
+class GoFunctionCommand(sublime_plugin.TextCommand):
+ def run(self, text):
+ view = self.view
+
+ #get current word
+ selection_region = view.sel()[0]
+ word_region = view.word(selection_region)
+ word = view.substr(word_region).strip()
+ word = re.sub('[\(\)\{\}\s]', '', word)
+
+ #get folders to search
+ window = sublime.active_window()
+ proj_folders = window.folders()
+
+ if word != "":
+ print "[Go2Function] Searching for 'function "+word+"'..."
+
+ for dir in proj_folders:
+ resp = self.doGrep(word, dir)
+
+ if len(resp) > 0:
+ self.openFileToDefinition(resp)
+ break
+
+ #if not found show error (ie loop ends without a break)
+ else:
+ print "[Go2Function] "+word+" not found"
+ sublime.error_message("could not find function definition for "+word)
+
+ #actually do the grep
+ #well, actually use the native python functions, not grep...
+ def doGrep(self, word, directory):
+ out = ()
+
+ for r,d,f in os.walk(directory):
+ #don't bother to look in git dirs
+ if check_dir(r):
+
+ for files in f:
+ fn = os.path.join(r, files)
+ search = open(fn, "r")
+ lines = search.readlines()
+
+ for n, line in enumerate(lines):
+ for find in self.getSearchTerms(word):
+ if re.search(find, line): #search using regex objects
+ out = (fn, n)
+ break
+
+ search.close()
+
+ if len(out) > 0:
+ break
+
+ if len(out) > 0:
+ break
+
+ return out
+
+ def getSearchTerms(self, word):
+ wordstr = str(word)
+ # compile regexes
+ regex1 = re.compile(wordstr + ur'\s[=]\s\(?.*\)?[-=]>')
+ regex2 = re.compile(wordstr + ur'[:]\s\(?.*\)?[-=]>')
+
+ return (regex1, regex2)
+
+
+ #open the file and scroll to the definition
+ def openFileToDefinition(self, response):
+ file, line = response
+
+ print "[Go2Function] Opening file "+file+" to line "+str(line)
+
+ line = line - 1
+
+ window = sublime.active_window()
+ new_view = window.open_file(file)
+
+ do_when(
+ lambda: not new_view.is_loading(),
+ lambda: new_view.set_viewport_position(new_view.text_to_layout(new_view.text_point(line, 0)))
+ )
1 package-metadata.json
@@ -0,0 +1 @@
+{"url": "https://github.com/edubkendo/", "version": "2012.08.07.00.01.00", "description": "Sublime Text 2 plugin to find and open a function's declaration in your coffeescript project"}

0 comments on commit a5db902

Please sign in to comment.
Something went wrong with that request. Please try again.