Sublime Forum

Sitebar favorite folder

#1

Hi, is there any way to setup some folder ( e.g. /user/Documents/programming) to always shows up at startup in sitebar? I know I can drag and drop but wish to just have it by default
Thanks

0 Likes

#2

This can be done with a plugin.

Click on Tools > Developer > New Plugin…

Paste the following python code into the new view:

import sublime
import sublime_plugin
import operator
import os


_COOKIE = "__favorite__"


def _check(window: sublime.Window, favorite: str) -> None:
    data = window.project_data()
    if not isinstance(data, dict):
        data = {}
    folders = data.get("folders")
    if not isinstance(folders, list):
        folders = []
    for path in map(operator.itemgetter("path"), folders):
        if os.path.samefile(path, favorite):
            return
    folders.append({"path": favorite, _COOKIE: True})
    data["folders"] = folders
    window.set_project_data(data)


def _clear(window: sublime.Window) -> None:
    data = window.project_data()
    if not isinstance(data, dict):
        return
    folders = data.get("folders")
    if not isinstance(folders, list):
        return
    for index, folder in enumerate(folders):
        if not isinstance(folder, dict):
            continue
        if folder.get(_COOKIE):
            folders.pop(index)
            break
    data["folders"] = folders
    window.set_project_data(data)


def _clear_all() -> None:
    for window in sublime.windows():
        _clear(window)


def _check_all() -> None:
    favorite = _get_favorite_folder()
    if not favorite:
        return
    for window in sublime.windows():
        _check(window, favorite)


def _on_settings_changed() -> None:
    _clear_all()
    _check_all()


def _get_favorite_folder():
    global _settings
    if not _settings:
        return
    favorite = _settings.get("favorite_folder")
    if not isinstance(favorite, str) or not favorite:
        return None
    return favorite


_settings = None


def plugin_loaded() -> None:
    global _settings
    _settings = sublime.load_settings("Preferences.sublime-settings")
    _settings.add_on_change("favoritefolder", _on_settings_changed)
    _check_all()


def plugin_unloaded() -> None:
    _clear_all()
    global _settings
    _settings.clear_on_change("favoritefolder")
    _settings = None


class FavoriteFolderListener(sublime_plugin.EventListener):

    def on_new_window(self, window: sublime.Window) -> None:
        favorite = _get_favorite_folder()
        if not favorite:
            return
        _check(window, favorite)

    def on_new(self, view: sublime.View) -> None:
        if sublime.version() >= "4000":
            # use on_new_window for ST4
            return
        self.on_new_window(view.window())

Save the view with CTRL+S. A file dialog opens with your Packages/User/ folder. Save it in there as, for example, favoritefolder.py.

Now you can open your preferences by running Preferences: Settings from the command palette. Add a key/value pair like:

// Packages/User/Preferences.sublime-settings
{
    // ...
    "favorite_folder": "/user/Documents/programming",
    // ...
}

and that folder will show up in the sidebar for any window :slight_smile:

2 Likes

#3

Thanks for that but its not working - dont know why :confused: after restart sublime it still shows only current file

0 Likes

#4

Do you see any sort of error logged in the console after saving the file ?

1 Like

#5

Ok my bad :slight_smile: havent seen all code haha. Silly me. It works like i want to. Big thanks.
But I have question. It shows my folder but with some delay. Probably needs time to execute script. All plugins r just python scripts or its possible to compile it to plugin exe or something?
Anyway thanks

0 Likes