【问题标题】:How to copy current file full path without filename using right click menu?如何使用右键菜单复制没有文件名的当前文件完整路径?
【发布时间】:2020-11-29 09:02:35
【问题描述】:
import sublime
import sublime_plugin
import os
class copypathCommand(sublime_plugin.WindowCommand):
    def run(self,edit):
        # view = sublime.active_window().active_view()
            vars = self.window.extract_variables()
            working_dir = vars['file_path']
            base_name=os.path.basename(working_dir)
            sublime.set_clipboard(base_name)
            print ("get clipboard" + sublime.get_clipboard())

Context.sublime-menu

[
    { "caption": "copypath",  "command": "copypath"},
]

这个脚本不起作用。 当我使用 ctrl-v 时,它会粘贴“working_dir”。 我认为问题出在sublime_plugin.WindowCommand 参数或self.window.extract_variables() 因为我只从其他正常工作的copyfilename 脚本中更改了这两行。 menu button turns grey when add if statement

【问题讨论】:

    标签: sublimetext3 sublime-text-plugin


    【解决方案1】:

    您的插件包含几个错误和可以改进代码的地方。

    1. os.path 类不会使用import os 自动导入。您需要使用import os.path

    2. 您的班级名称class copypathCommand 必须以大写C 开头以创建copypath 命令,即class CopypathCommand,请参阅How to set or find the command name of a Sublime Text plugin

    3. 您从sublime_plugin.WindowCommand 继承,但随后在run() 方法中使用了edit 参数,该参数只能在从sublime_plugin.TextCommand 继承的类中使用。最好为此插件使用TextCommand,因为它已经将活动视图设置为self.view。这样就得到了class CopypathCommand(sublime_plugin.TextCommand)

    4. 不需要使用extract_variables() 来获取文件路径。 Sublime Text APIview 类中提供了 file_name() 方法,该方法返回完整的文件路径或 None 如果视图/缓冲区未保存(即没有文件路径可返回)。因此,可以通过一次调用file_path = self.view.file_name() 来检索完整路径。

    5. 您使用os.path.basename(),但您的问题标题显示您想要“当前文件完整路径没有文件名”,这需要使用os.path.dirname()。这就是这两种方法在返回内容上的不同之处:

    os.path.basename("/full/path/to/folder/filename") --> "filename"
    os.path.dirname("/full/path/to/folder/filename")  --> "/full/path/to/folder"
    

    将所有这些放在一起作为一个工作插件会得到以下代码:

    # Save in ST config folder: ../Packages/User/CopypathToClipboard.py
    
    import os.path, sublime, sublime_plugin
    
    class CopypathCommand(sublime_plugin.TextCommand):
    
        def run(self, edit):
            file_path = self.view.file_name()
            if not file_path:
                sublime.status_message("The current buffer is not saved")
                return
            dir_path = os.path.dirname(file_path)
            sublime.set_clipboard(dir_path)
            sublime.status_message("Copied folder to the clipboard: " + dir_path)
    
        # is_enabled() enables the plugin only if the view contains a saved file.
        # If this returns false the 'copypath' command will not be available,
        # e.g. command palette captions hidden, context menu captions greyed out.
        def is_enabled(self):
            return True if self.view.file_name() else False
    

    您的Context.sublime-menu 代码没有任何问题——尽管添加id 行,正如我在下面所做的那样,意味着命令的标题Copy Folder Path 将显示在与Copy File Path 相同的菜单部分中右键单击上下文菜单。

    // Save in ST config folder: ../Packages/User/Context.sublime-menu
    [
        { "caption": "-", "id": "file" },
        { "caption": "Copy Folder Path",  "command": "copypath"}
    ]
    

    【讨论】:

    • 谢谢,这对我来说是一个很好的答案。还有一个问题:有时按钮会变成灰色。上下文菜单中的“命令”值应该是小写的。对吗?
    • 当没有可用的路径时,要在上下文菜单中使命令变灰,需要实现 is_enabled() 方法 - 请参阅上面我编辑的插件。是的,上下文菜单中的 "command" 值应该是小写的,所有 Sublime Text 命令都是小写的,请参阅 How to set or find the command name of a Sublime Text plugin
    • 另一个问题是有时我按照你的代码添加if statement,菜单立即变成灰色,当我删除时,按钮又变成黑色再次正常运行。@ 有什么问题吗987654355@
    • 回复:“另一个问题”——我不明白你的问题。请让英语流利的人帮你写另一条评论来解释你的意思。
    • 对不起,由于评论格式限制,我不太清楚这个问题,我在问题描述中更新了一个 gif!欢迎查看!
    猜你喜欢
    • 2022-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 2014-09-26
    • 2021-04-24
    • 2012-12-29
    • 1970-01-01
    相关资源
    最近更新 更多