从sublime_plugin.WindowCommand获取当前文件名

Max*_*mov 2 python plugins sublimetext2 sublimetext3

我开发插件sublime text 3.并希望获得当前打开的文件路径...

absolute1 = self.window.view.file_name()
Run Code Online (Sandbox Code Playgroud)

...这里selfsublime_plugin.WindowCommand

但失败了:

AttributeError: 'Window' object has no attribute 'view'
Run Code Online (Sandbox Code Playgroud)

完整的插件代码:

import sublime, sublime_plugin
import re, os, os.path

class OpenrelCommand(sublime_plugin.WindowCommand):

    def run(self):
        relative = sublime.get_clipboard()
        absolute1 = self.window.view.file_name()
        absolute2 = os.path.normpath(os.path.join(os.path.dirname(absolute1), relative))
        self.window.open_file(absolute2)

    def is_enabled(self):
        return bool(sublime.get_clipboard().strip())
Run Code Online (Sandbox Code Playgroud)

如果self是,sublime_plugin.TextCommand我可以没有问题得到当前的文件路径:

fileName = self.view.file_name()
Run Code Online (Sandbox Code Playgroud)

...但self必须是sublime_plugin.WindowCommand因为我想使用方法open_file:

self.window.open_file(absolute2)
Run Code Online (Sandbox Code Playgroud)

sku*_*oda 5

请查看API(http://www.sublimetext.com/docs/3/api_reference.html#sublime.Window).self是一个窗口对象.所以你需要做的self.window.active_view()就是获得视图.

  • 伙计我今天刚遇到问题.哈哈.`self.window.active_view()` (2认同)

pcu*_*ite 5

对于Sublime Text 3,对我有用的命令是:

self.view.window().active_view().file_name()
Run Code Online (Sandbox Code Playgroud)