打开当前打开的文件文件夹左侧面板的快捷方式

Shortcut to open the currently opened file's folder left panel

提问人:Basj 提问时间:2/25/2022 最后编辑:Basj 更新时间:5/31/2022 访问量:249

问:

我知道 Sublime 中的“文件>打开文件夹...”对话框。

问题是:

  • 它首先打开一个“文件选取器”对话框
  • 选择正确的文件夹后,它会在新的 Sublime Text 窗口中打开该文件夹,而不是当前窗口

如何在当前Sublime窗口的左侧“文件夹视图”中打开当前文件的文件夹,而没有任何弹出窗口?(我想为此绑定一个键盘快捷键)。注意:我仍然使用 Sublime 2。

sublimetext2 sublimetext

评论

0赞 OdatNurd 5/28/2022
对于 ST3 和 ST4,这是一个简单的插件。ST2 没有用于更改窗口中打开的文件夹的插件 API,只能查看它们。因此,需要一个在给定文件夹上调用的插件。不过还是可行的。您是否已设置系统,以便可以从终端使用?subl -asubl
0赞 Basj 5/30/2022
@OdatNurd谢谢!我没在 ST2 上看过?这是什么?我对调用此工具的解决方案感兴趣:)sublsubl -a

答:

0赞 Basj 5/31/2022 #1

用 @OdatNurd 的想法解决了 ST2:

class OpenthisfolderCommand(sublime_plugin.TextCommand): 
    def run(self, edit):
        current_dir = os.path.dirname(self.view.file_name())
        subprocess.Popen('"%s" -a "%s"' % ("c:\path\to\sublime_text.exe", current_dir))

例如,使用以下命令添加密钥绑定:

{ "keys": ["ctrl+shift+o"], "command": "openthisfolder"}
3赞 OdatNurd 5/31/2022 #2

菜单项将提示您输入文件夹的名称,然后将其添加到当前窗口,而不是创建一个新窗口。与名称相反,即使您没有明确直接使用文件,这也将始终有效。Project > Add Folder to project...sublime-project

为了在没有任何提示的情况下执行此操作,需要一个插件来调整当前在窗口中打开的文件夹列表。

在 Sublime Text 3 及更高版本中,有 API 支持直接修改窗口中打开的文件夹列表,而 Sublime Text 2 只有查询文件夹列表的 API。

所有版本的 Sublime 都有一个命令行帮助程序,可用于与 Sublime 的运行副本(通常称为 )进行交互,它可以做的一件事是通过添加一个额外的文件夹来增加窗口中的文件夹列表。在 Sublime Text 2 中,帮助程序只是主要的 Sublime Text 可执行文件本身。sublsubl

下面是一个可以在 Sublime Text 2 及更高版本中使用的插件,它将执行适当的操作来获取当前文件的路径以在侧边栏中打开。如果您不确定如何使用插件,请观看此视频,了解如何安装它们

import sublime
import sublime_plugin

import os


# This needs to point to the "sublime_text" executable for your platform; if
# you have the location for this in your PATH, this can just be the name of the
# executable; otherwise it needs to be a fully qualified path to the
# executable.
_subl_path = "/home/tmartin/local/sublime_text_2_2221/sublime_text"

def run_subl(path):
    """
    Run the configured Sublime Text executable, asking it to add the path that
    is provided to the side bar of the current window.

    This is only needed for Sublime Text 2; newer versions of Sublime Text have
    an enhanced API that can adjust the project contents directly.
    """
    import subprocess

    # Hide the console window on Windows; otherwise it will flash a window
    # while the task runs.
    startupinfo = None
    if os.name == "nt":
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

    subprocess.Popen([_subl_path, "-a", path], startupinfo=startupinfo)


class AddFileFolderToSideBarCommand(sublime_plugin.WindowCommand):
    """
    This command will add the path of the currently focused file in the window
    to the side bar; the command will disable itself if the current file does
    not have a name on disk, or if it's path is already open in the side bar.
    """
    def run(self):
        # Get the path to the current file and add it to the window
        self.add_path(self.get_current_path())

    def is_enabled(self):
        """
        The command should only be enabled if the current file has a filename
        on disk, and the path to that file isn't already in the list of folders
        that are open in this window.
        """
        path = self.get_current_path()
        return path is not None and path not in self.window.folders()

    def get_current_path(self):
        """
        Gather the path of the file that is currently focused in the window;
        will return None if the current file doesn't have a name on disk yet.
        """
        if self.window.active_view().file_name() is not None:
            return os.path.dirname(self.window.active_view().file_name())

        return None

    def add_path(self, path):
        """
        Add the provided path to the side bar of this window; if this is a
        version of Sublime Text 3 or beyond, this will directly adjust the
        contents of the project data to include the path. On Sublime Text 2 it
        is required to execute the Sublime executable to ask it to adjust the
        window's folder list.
        """
        if int(sublime.version()) >= 3000:
            # Get the project data out of the window, and then the list of
            # folders out of the project data; either could be missing if this
            # is the first project data/folders in this window.
            project_data = self.window.project_data() or {}
            folders = project_data.get("folders", [])

            # Add in a folder entry for the current file path and update the
            # project information in the window; this will also update the
            # project file on disk, if there is one.
            folders.append({"path": path})
            project_data["folders"] = folders
            self.window.set_project_data(project_data)
        else:
            # Run the Sublime executable and ask it to add this file.
            run_subl(path)

这将定义一个名为的命令,该命令会将当前文件的路径添加到侧边栏;如果当前文件在磁盘上没有名称,或者该路径已在侧栏中打开,则该命令将自行禁用。add_file_folder_to_side_bar

如果您使用的是 Sublime Text 2,请注意,您需要调整顶部的变量以指向 Sublime 副本的安装位置(包括程序本身的名称,如示例代码所示),因为插件需要能够调用它来调整侧边栏。

为了触发该命令,您可以使用键绑定,例如:

{ "keys": ["ctrl+alt+a"], "command": "add_file_folder_to_side_bar"},

您还可以创建一个在包中命名的文件(与放置插件的位置相同),其中包含以下内容,以便为此也有一个上下文菜单项:Context.sublime-menuUser

[
    { "caption": "-", "id": "file" },
    { "command": "add_file_folder_to_side_bar", "caption": "Add Folder to Side Bar",}
]

评论

0赞 Basj 1/23/2023
嗨@OdatNurd ST 大师:),您有 stackoverflow.com/questions/75156299/ 的想法吗......没有任何第三方软件包,但也许只有一小块插件,几行类似于这个问题?
1赞 OdatNurd 1/24/2023
MattDMo 已经通过编辑他的答案来解决这个问题(我也会这样做)。
0赞 Basj 1/24/2023
是的,确实很棒!