From acbc7bfa9907cf8a75d6370dbe80a05f8625265d Mon Sep 17 00:00:00 2001 From: Joel Kemp Date: Thu, 10 Jul 2014 13:50:23 -0400 Subject: [PATCH] Jump to a dependency. Fixes #8 --- Dependents.py | 88 ++++++++++++++++++++++++++++++++++++++------ changelogs/1.1.0.txt | 25 +++++++++++++ messages.json | 3 +- readme.md | 28 ++++++++++++-- 4 files changed, 128 insertions(+), 16 deletions(-) create mode 100644 changelogs/1.1.0.txt diff --git a/Dependents.py b/Dependents.py index b3662619..74f2a3ed 100644 --- a/Dependents.py +++ b/Dependents.py @@ -2,16 +2,31 @@ import subprocess import threading import os +import re from subprocess import Popen, PIPE +MODES = { + # find/jump-to the module dependent on the current module + 'DEPENDENTS': 'dependents', + # jump-to the module identified by the string under your cursor + 'DEPENDENCY': 'dependency' +} + class DependentsCommand(sublime_plugin.WindowCommand): - def run(self, root=''): - self.window.root = root; + def run(self, root, mode=MODES['DEPENDENTS']): + self.window.root = root; + self.window.mode = mode; + print('got mode', mode) + thread = DependentsThread(self.window) + thread.start(); + + if mode == MODES['DEPENDENTS']: + msg = 'Finding dependents' + elif mode == MODES['DEPENDENCY']: + msg = 'Looking for that file' - thread = DependentsThread(self.window) - thread.start(); - ThreadProgress(thread, 'Finding dependents', '') + ThreadProgress(thread, msg, '') class DependentsThread(threading.Thread): """ @@ -26,23 +41,35 @@ def __init__(self, window): self.window = window self.view = window.active_view() + self.filename = self.view.file_name() + # The part of the path before the root + self.path = self.filename[:self.filename.index(self.window.root)] + # The path of the path after the root + self.pathWithinRoot = self.filename[self.filename.index(self.window.root) + len(self.window.root):] threading.Thread.__init__(self) def run(self): - filename = self.view.file_name() + mode = self.window.mode - if filename.find(self.window.root) == -1: - print('Dependents: ' + filename + ' is not in the root directory') + if mode == MODES['DEPENDENTS']: + self.find_dependents() + elif mode == MODES['DEPENDENCY']: + self.find_dependency() + + def find_dependents(self): + """ + Finds the dependents of the current file and jumps to that file or shows a panel of dependent files + """ + if self.filename.find(self.window.root) == -1: + print('Dependents: ' + self.filename + ' is not in the root directory') return - # The part of the path before the root - self.path = path = filename[:filename.index(self.window.root)] - if (not os.path.exists(self.path + "/node_modules/dependents")): + if not os.path.exists(self.path + '/node_modules/dependents'): show_error('\nYou need to install the node tool "dependents" \n\nRun "npm install dependents" in your terminal') return - cmd = ["/usr/local/bin/node", path + "node_modules/dependents/bin/dependents.js", filename, path + "public/assets/js"] + cmd = ['/usr/local/bin/node', self.path + 'node_modules/dependents/bin/dependents.js', self.filename, self.path + 'public/assets/js'] dependents = Popen(cmd, stdout=PIPE).communicate()[0] self.dependents = dependents.decode('utf-8').split('\n') @@ -54,6 +81,38 @@ def run(self): else: sublime.set_timeout(self.show_quick_panel, 10) + def find_dependency(self): + """ + Jumps to the file identified by the string under the cursor + """ + selections = self.view.sel() + + if selections: + region = selections[0] + + if region.a == region.b: + region = self.view.line(region) + + highlighted = self.view.substr(region).strip() + highlighted = re.sub('[\'",]', '', highlighted) + + # Handle relative paths, if any + if (highlighted.find('..') == 0 or highlighted.find('.') == 0): + fileDir = os.path.dirname(self.pathWithinRoot) + highlighted = os.path.normpath(os.path.join(fileDir, highlighted)) + + if (highlighted[0] == '/'): + highlighted = highlighted[1:] + + extension = os.path.splitext(highlighted)[1] + + if (not extension): + extension = os.path.splitext(self.filename)[1] + + file_to_open = highlighted + extension + + self.open_file(file_to_open) + def show_quick_panel(self): if not self.dependents: show_error('\nCan\'t find any file that depends on this file') @@ -80,6 +139,11 @@ def open_file(self, dependent): # We removed the root originally when populating the dependents list filename = self.path + self.window.root + '/' + dependent + if not os.path.isfile(filename): + print('Dependents: could not open ' + filename) + show_error('Can\'t find that file') + return + def open(): self.window.open_file(filename) diff --git a/changelogs/1.1.0.txt b/changelogs/1.1.0.txt new file mode 100644 index 00000000..a95d28f7 --- /dev/null +++ b/changelogs/1.1.0.txt @@ -0,0 +1,25 @@ +New Feature +--- + +Jump to the module under your cursor + +How to activate it: +1. Add the new key binding below to Preferences -> Key Bindings - User +2. Change the "root" within the key binding if you need to +3. Within a JS file, place your cursor over the dependency path you want to go to +4. Press cmd + option + right (or the key combination you defined) to jump to that file + +New key binding + +Add this as a new key binding in Preferences -> Key Bindings - User: + +{ + "keys": ["super+alt+right"], + "command": "dependents", + "args": { + "root": "public/assets/js", + "mode": "dependency" + } +} + +This key binding should **not** replace the key binding you have for finding the dependents diff --git a/messages.json b/messages.json index b0a0b453..bfb2a736 100644 --- a/messages.json +++ b/messages.json @@ -1,5 +1,6 @@ { "install": "readme.md", "1.0.8": "changelogs/1.0.8.txt", - "1.0.9": "changelogs/1.0.9.txt" + "1.0.9": "changelogs/1.0.9.txt", + "1.1.0": "changelogs/1.1.0.txt" } diff --git a/readme.md b/readme.md index 2e0d3116..01a8c2c7 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,11 @@ ### Sublime Dependents -Sublime Text 3 plugin for finding JavaScript modules that depend on the current JavaScript module. +Sublime Text 3 plugin for navigating JavaScript codebases. + +Features: + +* Find JavaScript modules that depend on the current JavaScript module. +* Jump to a dependency Current scope: AMD and CommonJS applications @@ -41,14 +46,27 @@ Add the following to your User defined keyboard bindings: `Preferences` -> `Key { "keys": ["super+alt+up"], "command": "dependents", - "args": {"root": "public/assets/js"} + "args": { + "root": "public/assets/js" + } + }, + { + "keys": ["super+alt+right"], + "command": "dependents", + "args": { + "root": "public/assets/js", + "mode": "dependency" + } } ] - ``` +* You won't need the opening and closing square brackets ([]) if you have pre-existing key bindings + ### Usage +#### Find the dependents of the current file + Use the `keys` value above, `cmd + option + up`, to trigger finding the dependents. * If dependents are found, you'll see them in a quick panel (i.e., dropdown). @@ -56,3 +74,7 @@ Use the `keys` value above, `cmd + option + up`, to trigger finding the dependen * If there's only one dependent, you'll be taken to that dependent file directly. * If no dependents are found a popup will be shown +#### Jump to a dependency + +1. Within a JS file, place your cursor over the dependency path you want to go to +2. Press cmd + option + right (or the key combination you defined) to jump to that file