【发布时间】:2015-07-21 02:21:09
【问题描述】:
我正在为ST3 创建一个插件,并且需要所有已定义范围的列表。我知道点击ctrl+alt+shift+p 会在状态栏中显示当前范围,但我不能为每个文件扩展名都这样做。
编辑:
除了简单的.tmLanguage 文件之外,我还提取.sublime-package 文件并从内部读取.tmLanguage 文件。这在列表中添加了一些条目,例如 source.php。但是source.python 还是不见了!
其实python代码是:(这是针对Python 3.3的)
import sublime, sublime_plugin, os, subprocess, glob, tempfile, plistlib
from zipfile import ZipFile
def scopes_inside(d):
result = []
for k in d.keys():
if k == 'scopeName':
result = result + [ s.strip() for s in d[k].split(',') ]
elif isinstance(d[k], dict):
result = result + scopes_inside(d[k])
return result
scopes = set()
for x in os.walk(sublime.packages_path() + '/..'):
for f in glob.glob(os.path.join(x[0], '*.tmLanguage')):
for s in scopes_inside(plistlib.readPlist(f)):
scopes.add(s.strip())
for x in os.walk(sublime.packages_path() + '/..'):
for f in glob.glob(os.path.join(x[0], '*.sublime-package')):
input_zip = ZipFile(f)
for name in input_zip.namelist():
if name.endswith('.tmLanguage'):
for s in self.get_scopes_from(plistlib.readPlistFromBytes(input_zip.read(name))):
scopes.add(s.strip())
scopes = list(scopes)
它现在给出了这个列表:
"font",
"license",
"source.c++",
"source.cmake",
"source.coffee",
"source.css",
"source.d",
"source.disasm",
"source.dockerfile",
"source.gdb.session",
"source.gdbregs",
"source.git",
"source.gradle",
"source.groovy",
"source.gruntfile.coffee",
"source.gruntfile.js",
"source.gulpfile.coffee",
"source.gulpfile.js",
"source.ini",
"source.ini.editorconfig",
"source.jade",
"source.jl",
"source.js",
"source.json",
"source.json.bower",
"source.json.npm",
"source.jsx",
"source.less",
"source.php",
"source.procfile",
"source.puppet",
"source.pyjade",
"source.qml",
"source.rust",
"source.sass",
"source.scss",
"source.shell",
"source.stylus",
"source.swift",
"source.yaml",
"source.zen.5a454e6772616d6d6172",
"text.html.basic",
"text.html.mustache",
"text.html.ruby",
"text.html.twig",
"text.slim",
"text.todo"
但我在此列表中找不到像 python 这样的语言。我猜其他的存储在安装文件夹中某处的一些二进制文件中。如果是这样,那么如何解析这些文件?
【问题讨论】:
标签: python sublimetext3 sublime-text-plugin