获取 Sublime Text 3 上的所有范围名称 [英] Get all scope names on Sublime Text 3

查看:43
本文介绍了获取 Sublime Text 3 上的所有范围名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 ST3 创建一个插件,并且需要所有已定义范围的列表.我知道点击 ctrl+alt+shift+p 会在状态栏中显示当前范围,但我不能对每个文件扩展名都这样做.

I am creating a plugin for ST3 and need the list of all defined scopes. I know that hitting ctrl+alt+shift+p shows the current scope in the status bar but I can't do it for every file extension.

除了简单的 .tmLanguage 文件之外,我还提取了 .sublime-package 文件并从内部读取 .tmLanguage 文件.这在列表中添加了一些条目,例如 source.php.但是 source.python 仍然缺失!

In addition to simple .tmLanguage files I am extracting the .sublime-package files and reading .tmLanguage files from inside. This added some entries like source.php to the list. But source.python is still missing !

实际上,python 代码是:(这是针对 Python 3.3 的)

Actually, the python code is: ( this is for 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)

它现在给出了这个列表:

And it gives this list now:

"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 这样的语言.我猜其他存储在安装文件夹中某处的一些二进制文件中.如果是这样,那么如何解析这些文件?

But I can't find some languages like python in this list. I guess other are stored within some binary files somewhere within the installation folder. If that's true so how the parse thoses files ?

推荐答案

我刚刚发现剩余的包都存储在安装目录中.所以给出所有范围名称的最终代码是:

I just found the remaining packages wich are stored within the installation directory. So the final code which gives all scope names is:

import sublime, sublime_plugin, os, subprocess, glob, tempfile, plistlib
from zipfile import ZipFile

# This function gives array of scope names from the plist dictionary passed as argument
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

# Using set to have unique values
scopes = set()
# Parsing all .tmLanguage files from the Packages directory
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())
# Parsing all .tmLanguage files inside .sublime-package files from the Installed Packages directory
for x in os.walk(sublime.installed_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())
# Parsing all .tmLanguage files inside .sublime-package files from the Installation directory
for x in os.walk(os.path.dirname(sublime.executable_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)

此代码可能会根据安装的包给出不同的结果(一些包添加了新的语法/范围名称).就我而言,结果是:

This code may give different results depending on Packages installed (some packages add new syntax/scope names). In my case, the result was :

font
license
source.actionscript.2
source.applescript
source.asp
source.c
source.c++
source.camlp4.ocaml
source.clojure
source.cmake
source.coffee
source.cs
source.css
source.d
source.diff
source.disasm
source.dockerfile
source.dosbatch
source.dot
source.erlang
source.gdb.session
source.gdbregs
source.git
source.go
source.gradle
source.groovy
source.gruntfile.coffee
source.gruntfile.js
source.gulpfile.coffee
source.gulpfile.js
source.haskell
source.ini
source.ini.editorconfig
source.jade
source.java
source.java-props
source.jl
source.js
source.js.rails
source.json
source.json.bower
source.json.npm
source.jsx
source.less
source.lisp
source.lua
source.makefile
source.matlab
source.nant-build
source.objc
source.objc++
source.ocaml
source.ocamllex
source.ocamlyacc
source.pascal
source.perl
source.php
source.procfile
source.puppet
source.pyjade
source.python
source.qml
source.r
source.r-console
source.regexp
source.regexp.python
source.ruby
source.ruby.rails
source.rust
source.sass
source.scala
source.scss
source.shell
source.sql
source.sql.ruby
source.stylus
source.swift
source.tcl
source.yaml
source.zen.5a454e6772616d6d6172
text.bibtex
text.haml
text.html.asp
text.html.basic
text.html.erlang.yaws
text.html.javadoc
text.html.jsp
text.html.markdown
text.html.markdown.multimarkdown
text.html.mustache
text.html.ruby
text.html.tcl
text.html.textile
text.html.twig
text.log.latex
text.plain
text.restructuredtext
text.slim
text.tex
text.tex.latex
text.tex.latex.beamer
text.tex.latex.haskell
text.tex.latex.memoir
text.tex.latex.rd
text.tex.math
text.todo
text.xml
text.xml.xsl

这篇关于获取 Sublime Text 3 上的所有范围名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆