Sublime Forum

Custom menu tab, how to know the file name?

#1

Hi everyone! I made a plugin that allows you to close all open tabs except those with a pin.

  1. was there a similar plugin already? https://github.com/iacoposk8/close-all-tab-except

  2. when you right-click on a tab you can put a pin, but as I did, the tab2pin variable will have the value of the open tab not the one I right-clicked, how can I solve?

    class PinTabCommand(sublime_plugin.TextCommand):
    def run(self, edit):
    tab2pin = self.view.file_name()

0 Likes

#2

pin_tab_command.py

import sublime
import sublime_plugin

VIEW_SETTING_IS_PINNED = "is_pinned"


def is_view_pinned(view: sublime.View) -> bool:
    return bool(view.settings().get(VIEW_SETTING_IS_PINNED))


def pin_view(view: sublime.View) -> None:
    view.settings().set(VIEW_SETTING_IS_PINNED, True)
    view.set_status(VIEW_SETTING_IS_PINNED, "đź“Ś")


def unpin_view(view: sublime.View) -> None:
    view.settings().erase(VIEW_SETTING_IS_PINNED)
    view.erase_status(VIEW_SETTING_IS_PINNED)


class PinTabCommand(sublime_plugin.WindowCommand):
    def is_visible(self, group: int, index: int) -> bool:
        return not is_view_pinned(self.window.views_in_group(group)[index])

    def run(self, group: int, index: int) -> None:
        pin_view(self.window.views_in_group(group)[index])


class UnpinTabCommand(sublime_plugin.WindowCommand):
    def is_visible(self, group: int, index: int) -> bool:
        return is_view_pinned(self.window.views_in_group(group)[index])

    def run(self, group: int, index: int) -> None:
        unpin_view(self.window.views_in_group(group)[index])


class CloseUnpinnedTabsCommand(sublime_plugin.WindowCommand):
    def run(self, no_ask: bool = False) -> None:
        if not no_ask and not sublime.ok_cancel_dialog("Close all unpinned tabs?"):
            return

        for view in self.window.views(include_transient=True):
            if not is_view_pinned(view):
                view.close()

Tab Context.sublime-menu

[
    {
        "caption": "Pin this tab…",
        "command": "pin_tab",
        "args": {"group": -1, "index": -1},
    },
    {
        "caption": "Unpin this tab…",
        "command": "unpin_tab",
        "args": {"group": -1, "index": -1},
    },
    {
        "caption": "Close unpinned tabs…",
        "command": "close_unpinned_tabs",
        // "args": {"no_ask": true},
    },
]
2 Likes