我正在尝试编写一个插件,它允许我一次性打开一组相关文件。前提是:
- 该插件向用户提供目录列表(表面上是自包含的 UI 组件)
- 用户选择一个目录
- 该插件创建了一个新的 3 列布局
- 在所选目录的第一列中打开 .js 文件
- 在所选目录的第二列中打开一个 .html 文件
- 在所选目录的第三列中打开一个 .scss 文件
到目前为止,我已经让插件向用户展示了目录选择,并创建了三列布局,但我不知道如何遍历三列布局以在新 View 中打开文件
import sublime, sublime_plugin, os
class OpenSesameCommand(sublime_plugin.TextCommand):
def run(self, edit):
#This is the directory where the components are kept
self.thedir = '/Users/tom/Documents/Tradeweb/tradeweb-uscc/src/js/components'
self.window = sublime.active_window()
#Get all directories in the component directory
self.listings = [ name for name in os.listdir(self.thedir) if os.path.isdir(os.path.join(self.thedir, name)) ]
#Show all directories in the quick panel
self.window.show_quick_panel(self.listings, self.open_component, sublime.MONOSPACE_FONT)
def open_component(self, index):
# Generate file paths to the relevant files
compName = self.listings[index]
jsFile = self.create_file_ref(compName, 'js')
htmlFile = self.create_file_ref(compName, 'html')
sassFile = self.create_file_ref(compName, 'scss')
#create a new layout
self.window.set_layout({
"cols": [0.0, 0.3, 0.6, 1.0],
"rows": [0.0, 1.0],
"cells": [ [0, 0, 1, 1], [1, 0, 1, 1], [2, 0, 2, 1]]
})
# ??? how can I set the focus on different columns
#open files
#self.window.open_file(htmlFile)
#self.window.open_file(jsFile)
#self.window.open_file(sassFile)
def create_file_ref(self, component, type):
componentDir = self.thedir + '/' + component + '/'
return componentDir + component + '.' + type
浏览 API我的印象是它与 Window
对象上的 view
和 group
相关方法有关,但我不能我的生活把它拼凑起来。
如果有人可以指出我如何打开第三列中的文件,我确信我可以从那里获取它。
顺便说一句:这是我第一次使用 python,所以请原谅任何不好的做法(但请指出)。
最佳答案
你会想使用
self.window.focus_group(0)
self.window.open_file(htmlFile)
self.window.focus_group(1)
self.window.open_file(jsFile)
self.window.focus_group(2)
self.window.open_file(sassFile)
或者,等价地,
for i, file in enumerate([htmlFile, jsFile, sassFile]):
self.window.focus_group(i)
self.window.open_file(file)
关于python - 如何使用 sublime 插件 api 创建新布局并在每个单元格中打开一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21121050/