Skip to content

Commit

Permalink
Jump to a dependency. Fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Kemp committed Jul 10, 2014
1 parent db9ca62 commit acbc7bf
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 16 deletions.
88 changes: 76 additions & 12 deletions Dependents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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')

Expand All @@ -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')
Expand All @@ -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)

Expand Down
25 changes: 25 additions & 0 deletions changelogs/1.1.0.txt
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion messages.json
Original file line number Diff line number Diff line change
@@ -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"
}
28 changes: 25 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -41,18 +46,35 @@ 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).
* You can select any of the items in the panel to jump to that file
* 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

0 comments on commit acbc7bf

Please sign in to comment.