Sublime Forum

Is there extension to create folder tree from current project

#1

I often draw my project folder structure like this

app
| controller
| view
| asset
| css
| js

which is a pain to do from time to time. I there a sublime plugin that can do this for us?

0 Likes

#2

I did a little searching around but didn’t find anything dedicated to this particular task; I may not have been searching for the correct keywords though, so it’s possible that there is some plugin that does this along with other things and I just didn’t find it.

In any case, I’m always up for an exercise to learn a little more about sublime, so I did a bit of poking and came up with the following simple plugin (the code that actually generates the tree I got from this stackoverflow post):


import sublime, sublime_plugin, os

# Append a tree view of a root path to the current view;
# probably you don't want to bind a key to this one
class DisplayTreeCommand(sublime_plugin.TextCommand):
    def run(self, edit, rootPath):
        for root, dirs, files in os.walk(rootPath):
            level = root.replace(rootPath, '').count(os.sep)
            indent = ' ' * 4 * (level)
            self.view.insert (edit, self.view.size (),
                '{}{}/\n'.format(indent, os.path.basename(root)))
            subIndent = ' ' * 4 * (level + 1)
            for f in files:
                self.view.insert (edit, self.view.size (),
                    '{}{}\n'.format(subIndent, f))

# Create a new scratch buffer and insert a textual tree of
# the current project path into it
class DisplayProjectTreeCommand(sublime_plugin.WindowCommand):
    def run(self):
        # Only do stuff if there is a project
        varList = self.window.extract_variables ()
        if "project_path" in varList:
            # Create a new scratch file to put the tree in
            outView = self.window.new_file ()
            outView.set_scratch (True)
            outView.set_name ("Project Tree")

            # Do it
            outView.run_command ('display_tree', {"rootPath": varList["project_path"]})
        else:
            self.window.status_message ("No project defined")

Stored in Packages/User/display_project_tree.py (use Preferences > Browse Packages... to find that location if needed).

A sample key binding would be:

    {"keys": ["ctrl+alt+shift+t"], "command": "display_project_tree"}

This uses the naive approach of just displaying the complete tree rooted at the location where the project is stored. In particular, it does not handle extra paths added or excluded from the project, handle the case where the project file is in a different location outside of the project tree and it (probably) doesn’t display the tree in exactly the format that you want, but it should be a good starting point.

2 Likes

#3

window.extract_variables()[ "project_path" ] returns the path that contains project data ( sublime-project & sublime-workspace files ), rather than the actual project path.

The updates below should fix that:

class DisplayProjectTreeCommand( sublime_plugin.WindowCommand ):
	def run( self ):

		projectData = self.window.project_data()
		if not projectData:
			self.window.status_message( "No Project Defined" )
			return

		projectPath = next( folderDict for index, folderDict in enumerate( projectData[ "folders" ] ) if "path" in folderDict )[ "path" ]
		projectName = self.window.extract_variables()[ "project_base_name" ]

		outView = self.window.new_file()
		outView.set_scratch( True )
		outView.set_name( projectName + " (Folder Tree)" )
		outView.run_command( 'display_tree', { "projectPath": projectPath } )
1 Like

#4

@OdatNurd I copied the code and create the plugin. It doesn’t seem to work, pressing the shortcut combination will not happening anything.

we save the plugin in the package folder or user folder?

0 Likes

#5

You should save it in the Packages/User folder. The name doesn’t matter as long as it has the .py extension.

1 Like

#6

that’s cool

0 Likes