Skip to content

Instantly share code, notes, and snippets.

@andymccurdy
Created September 23, 2013 18:05
Show Gist options
  • Save andymccurdy/6674513 to your computer and use it in GitHub Desktop.
Save andymccurdy/6674513 to your computer and use it in GitHub Desktop.
import os
import sublime_plugin
class NewFilePopupCommand(sublime_plugin.WindowCommand):
def run(self):
self.build_directory_tree()
self.window.show_quick_panel(self.labels, self.directory_selected)
def build_directory_tree(self):
self.labels = []
self.label_to_path = {}
for folder in self.window.folders():
root, relative = os.path.split(folder)
stack = [relative]
while stack:
relative = stack.pop(0)
if '.git' in relative:
continue
fullpath = os.path.join(root, relative)
self.labels.append(relative)
self.label_to_path[relative] = fullpath
base, dirs, files = os.walk(fullpath).next()
dirs = map(lambda d: os.path.join(relative, d), sorted(dirs))
stack.extend(dirs)
def directory_selected(self, selected_index):
if selected_index != -1:
v = self.window.new_file()
label = self.labels[selected_index]
fullpath = self.label_to_path[label]
v.settings().set('default_dir', fullpath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment