Reputation: 1039
I have a project that's in Mason. How do I tell ST2 to open .html files as Mason for just that project?
More generically, how do I tell ST2 to reassign filetypes on a per-project basis?
Upvotes: 3
Views: 731
Reputation: 4515
For those interested in a solution for ST3, I wrote this blog post which I'll excerpt here.
First off, you'll need to save the python code below to a file in your Packages/User
directory. Where this directory is located depends on your system. On MacOS, it's at ~/Library/Application Support/Sublime Text 3/Packages/User
. Give the file a name such as project_specific_file_syntax.py
.
import os.path
import re
import sublime_plugin
class ProjectSpecificFileSyntax(sublime_plugin.EventListener):
def on_load(self, view):
filename = view.file_name()
if not filename:
return
syntax = self._get_project_specific_syntax(view, filename)
if syntax:
self._set_syntax(view, syntax)
def _get_project_specific_syntax(self, view, filename):
project_data = view.window().project_data()
if not project_data:
return None
syntax_settings = project_data.get('syntax_override', {})
for regex, syntax in syntax_settings.items():
if re.search(regex, filename):
return syntax
return None
def _set_syntax(self, view, syntax):
syntax_path = os.path.join('Packages', *syntax)
view.set_syntax_file('{0}.tmLanguage'.format(syntax_path))
Now, you just need to add a syntax_override
section to your .sublime-project
file, like so.
{
...
"syntax_override": {
"\\.html$": [ "HTML Underscore Syntax", "HTML (Underscore)" ]
}
}
The syntax_override
section can contain as many key/value pairs as you like. The key should be a regular expression that will be matched against the name of the file. The value should be an array containing two strings. The first string is the name of the package containing the syntax file and the second is the name of the syntax. Root around in Sublime Text's directory structure to find files that end with .tmLanguage
. The names of these files (minus the .tmLanguage
extension) are what you would use for the second string.
I've only tested this with Sublime Text 3, but I'm sure it could be easily adapted to work with Sublime Text 2.
Upvotes: 3
Reputation: 68820
There's no native per-project syntax settings in Sublime Text 2.
Currently, I'm using the Modelines plugin as a workaround, but it requires to add this modeline on top (or bottom) of each .html
file in your project :
<!-- sublime: x_syntax Packages/Mason/Syntaxes/Mason.tmLanguage
-->
Note the carriage return, -->
must not be on the same line, or it'll be interpreted as a parameter.
Another track, I read lots of comments about DetectSyntax plugin. I quickly tried to try it, without success, but maybe you'll be more patient than me ! Anyway, I'm interested by a feedback if you can make it work.
Upvotes: 1