WindowCommand获取当前文件名

WindowCommand获取当前文件名

本文介绍了从sublime_plugin.WindowCommand获取当前文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为sublime text 3开发了插件.并想要获取当前打开的文件路径...

I develop plugin for sublime text 3. And want to get currently opened file path ...

absolute1 = self.window.view.file_name()

...,其中selfsublime_plugin.WindowCommand

但是失败:

AttributeError: 'Window' object has no attribute 'view'

插件的完整代码:

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())

如果self将是sublime_plugin.TextCommand,我可以毫无问题地获得当前文件路径:

If self would be sublime_plugin.TextCommand I could get current file path without a problem:

fileName = self.view.file_name()

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

... but self must be sublime_plugin.WindowCommand because I want to use method open_file :

self.window.open_file(absolute2)

推荐答案

看看API( http://www.sublimetext.com/docs/3/api_reference.html#sublime.Window ). self是一个窗口对象.因此,您需要执行self.window.active_view()来获取视图.

Take a look at the API (http://www.sublimetext.com/docs/3/api_reference.html#sublime.Window). self is a window object. So you need to do self.window.active_view() to get the view.

这篇关于从sublime_plugin.WindowCommand获取当前文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 14:34