get current file name from sublime_plugin.WindowCommand

╄→гoц情女王★ 提交于 2019-12-07 20:14:43

问题


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

absolute1 = self.window.view.file_name()

... where self is sublime_plugin.WindowCommand

But fail:

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

Full code of plugin:

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

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

fileName = self.view.file_name()

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

self.window.open_file(absolute2)

回答1:


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.




回答2:


For Sublime Text 3 the command that worked for me was:

self.view.window().active_view().file_name()



回答3:


For sublime Text 3 i think using the following :

myCompleteName = self.view.file_name()

can be a solution,i've tried it with sublime_plugin.TextCommand and it works just fine



来源:https://stackoverflow.com/questions/19524926/get-current-file-name-from-sublime-plugin-windowcommand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!