Skip to content

Commit 499b7ad

Browse files
committed
support filetype tags
contains a dictionary to map form vim filetype identifiers, to the "scope" names Sublime uses in its Syntax files. This was generated half-automatically, and scope names cover those available in Sublime and the CMake plugin. The mapping from Sublime's "scope" names to package names if created the first time it is requested. both mappings should allow changes by the user (not implemented). Signed-off-by: Norbert Lange <nolange79@gmail.com>
1 parent b7d499b commit 499b7ad

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

lib/filetype.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# vim: set ts=4 sw=4 et fileencoding=utf-8:
2+
'''Vim filetype mappings to Sublime Text'''
3+
4+
FILETYPE_MAP = {
5+
'ant': 'text.xml',
6+
'antlr': 'source.java',
7+
'arduino': 'source.c++',
8+
'aspperl': 'text.html.basic',
9+
'aspvbs': 'text.html.basic',
10+
'automake': 'source.makefile',
11+
'bash': 'source.shell.bash',
12+
'bzl': 'source.python',
13+
'c': 'source.cmake.config.c',
14+
'cf': 'text.html.basic',
15+
'ch': 'source.cmake.config.c',
16+
'chaskell': 'text.tex.latex.haskell',
17+
'clojure': 'source.clojure',
18+
'cmake': 'source.cmake',
19+
'cmod': 'source.cmake.config.c',
20+
'conaryrecipe': 'source.python',
21+
'cpp': 'source.cmake.config.c',
22+
'cs': 'source.cs',
23+
'css': 'source.css',
24+
'cuda': 'source.c++',
25+
'cweb': 'text.tex',
26+
'cynlib': 'source.c++',
27+
'd': 'source.d',
28+
'diff': 'source.diff',
29+
'dosbatch': 'source.dosbatch',
30+
'dot': 'source.dot',
31+
'dsl': 'text.xml',
32+
'dtml': 'text.html.basic',
33+
'dtrace': 'source.cmake.config.c',
34+
'erlang': 'source.erlang',
35+
'esqlc': 'source.c++',
36+
'go': 'source.go',
37+
'groovy': 'text.html.basic',
38+
'gsp': 'text.html.basic',
39+
'haml': 'text.html.basic',
40+
'haskell': 'text.tex.latex.haskell',
41+
'html': 'text.html.basic',
42+
'htmlcheetah': 'text.html.basic',
43+
'htmldjango': 'text.html.basic',
44+
'htmlm4': 'text.html.basic',
45+
'htmlos': 'text.html.basic',
46+
'java': 'source.java',
47+
'javacc': 'source.java',
48+
'javascript': 'source.js',
49+
'javascriptreact': 'source.js',
50+
'json': 'source.json',
51+
'jsp': 'text.html.jsp',
52+
'kwt': 'source.c++',
53+
'less': 'source.css',
54+
'lhaskell': 'text.tex',
55+
'lisp': 'source.lisp',
56+
'lua': 'source.lua',
57+
'make': 'source.makefile',
58+
'markdown': 'text.html.markdown',
59+
'mason': 'text.html.basic',
60+
'matlab': 'source.matlab',
61+
'msql': 'text.html.basic',
62+
'nasm': 'source.cmake.config.c',
63+
'objc': 'source.objc',
64+
'objcpp': 'source.objc++',
65+
'ocaml': 'source.ocaml',
66+
'papp': 'text.xml',
67+
'pascal': 'source.pascal',
68+
'perl': 'source.perl',
69+
'php': 'text.html.basic',
70+
'phtml': 'embedding.php',
71+
'plp': 'text.html.basic',
72+
'pyrex': 'source.python',
73+
'python': 'source.python',
74+
'r': 'source.r',
75+
'rmd': 'text.html.markdown',
76+
'rnoweb': 'text.tex',
77+
'rpcgen': 'source.cmake.config.c',
78+
'rrst': 'text.restructuredtext',
79+
'rst': 'text.restructuredtext',
80+
'ruby': 'text.html.ruby',
81+
'rust': 'source.rust',
82+
'sass': 'source.css',
83+
'sbt': 'source.scala',
84+
'scala': 'source.scala',
85+
'sdc': 'text.html.tcl',
86+
'smarty': 'text.html.basic',
87+
'splint': 'source.cmake.config.c',
88+
'spyce': 'text.html.basic',
89+
'sql': 'source.sql',
90+
'svg': 'text.xml',
91+
'tcl': 'text.html.tcl',
92+
'template': 'text.html.basic',
93+
'tex': 'text.tex',
94+
'tt2html': 'text.html.basic',
95+
'tt2js': 'source.js',
96+
'typescript': 'source.json',
97+
'vue': 'text.html.basic',
98+
'webmacro': 'text.html.basic',
99+
'wml': 'text.html.basic',
100+
'wsh': 'text.xml',
101+
'xbl': 'text.xml',
102+
'xhtml': 'text.html.basic',
103+
'xml': 'text.xml',
104+
'xquery': 'text.xml',
105+
'xs': 'source.cmake.config.c',
106+
'xsd': 'text.xml',
107+
'xslt': 'text.xml',
108+
'yaml': 'source.yaml',
109+
'zsh': 'source.shell.bash'
110+
}

vimmodelines.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from itertools import chain
88
from .lib.encoding import ENCODING_MAP
9+
from .lib.filetype import FILETYPE_MAP
910
import re
1011
import sublime
1112
import sublime_plugin
@@ -79,6 +80,7 @@ class VimModelinesApplyCommand(Common, sublime_plugin.WindowCommand):
7980

8081
__attr_sep_RX = re.compile(r'[:\s]')
8182
__attr_kvp_RX = re.compile(r'([^=]+)=?([^=]*)')
83+
__syntax_map = None
8284

8385
def run(self):
8486
view = self.window.active_view()
@@ -128,6 +130,17 @@ def run(self):
128130
else:
129131
view.run_command('set_encoding',
130132
{'encoding': target_encoding})
133+
elif attr in ('filetype', 'ft'):
134+
# TODO: allow user to set vim -> package/syntax mapping in
135+
# settings.
136+
# package names will have path delimiters, so you can
137+
# figure out if a name is a package or syntax
138+
sublime_syntax = FILETYPE_MAP.get(value.lower())
139+
package_path = self.get_syntax_packages().get(sublime_syntax)
140+
141+
if package_path:
142+
view.settings().set('syntax', package_path +
143+
'.sublime-syntax')
131144

132145
@staticmethod
133146
def header_and_footer(view, line_count):
@@ -158,3 +171,32 @@ def parse_for_modeline(cls, line):
158171
cls.__attr_sep_RX.split(modeline))]
159172

160173
return attrs
174+
175+
@classmethod
176+
def get_syntax_packages(cls):
177+
'''Inspect sublime's installed syntax plugins and build a dictionary'''
178+
179+
if not cls.__syntax_map:
180+
syntax_map = {}
181+
for package in sublime.find_resources('*.sublime-syntax'):
182+
# Find the first scope entry, and ignore "hidden" syntax files.
183+
# Not using yaml parsers for avoiding that dependency and
184+
# more speed.
185+
186+
scope = None
187+
for line in sublime.load_resource(package).splitlines():
188+
line = line.strip()
189+
if line.startswith('hidden:'):
190+
if line[len('hidden:'):].strip().startswith('True'):
191+
break
192+
193+
if not scope and line.startswith('scope:'):
194+
scope = line[len('scope:'):].strip()
195+
196+
# TODO: What if multiple packages implement a syntax?
197+
if scope:
198+
syntax_map[scope] = package[:-len('.sublime-syntax')]
199+
200+
cls.__syntax_map = syntax_map
201+
202+
return cls.__syntax_map

0 commit comments

Comments
 (0)