gpt4 book ai didi

python - 从 sublime_plugin.WindowCommand 获取当前文件名

转载 作者:行者123 更新时间:2023-11-28 19:49:27 24 4
gpt4 key购买 nike

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

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

如果 selfsublime_plugin.TextCommand 我可以毫无问题地获取当前文件路径:

fileName = self.view.file_name()

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

self.window.open_file(absolute2)

最佳答案

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

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

关于python - 从 sublime_plugin.WindowCommand 获取当前文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19524926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com