I had never used python before writing ST3 plugins, it is a really easy language to learn
how about something like:
import sublime, sublime_plugin
class SetSyntax(sublime_plugin.EventListener):
def on_modified_async(self, view):
if view.match_selector(0, 'text.plain'):
syntaxes = [('<?php', 'Packages/PHP/PHP.sublime-syntax')]
for text, file in syntaxes:
if view.size() == len(text):
if view.substr(sublime.Region(0, len(text))) == text:
view.set_syntax_file(file)
if you go to the Tools menu - Developer - New Plugin... and replace the contents of the template it generates with the above, and save it in the default folder, named as something like setsyntax.py then it will convert the syntax to php when the entire content of the file is just <?php (provided the current syntax is set to Plain Text)
maybe it is inefficient because the plugin runs on every change to a document, maybe you would prefer to bind a command to the enter key instead…
The header of the syntax file allows you to specify a first_line_match key:
When a file is opened without a recognized extension, the first line of the file contents will be tested against this regex, to see if the syntax should be applied.
this will when u save the file, what am after is to change the file syntax on the fly ,dynamically to what u write , without waiting for the file to be saved.
WOOOOOOOOT, thanx a million
at first it didn’t work and after some restarts & like 10 trials sublime start to pickup the plugin and everything works as expected, many many thanx for ur help, U R AWESOME
one last thing, is there a way to have like an if/else in that plugin to make it easier to include more definitions instead of creating one for each ?
maybe like an array for the [language => keywords] and the plugin simply iterate over that array and everything works
that works but not always, also i was more into just make a new tab and simply start typing & sublime automagically understand what language i want to write ,
i believe it makes coding more fluent and easier to get ur ideas out right away without going to the bottom corner everytime u wanna write something.
I am pretty curious about when it won’t work but your method works. I believe most syntax package developers would define first_line_match if it’s possible.
also i was more into just make a new tab and simply start typing & sublime automagically understand what language i want to write ,
Yes. Before your post, actually, I prefer ctrl+shift+p → ssphp rather than using mouse. I hardly use the method I post here lol.
lol, hopefully sublime would get more intelligent natively without the need for installing a bazillion plugin, and things start to work like a second nature without any wasted time into learning how the app works .
Is [ ( “string_1” , “string_2” ) ] similar to using a dictionary? I’ve never seen that pattern…
Can you use it like:
[ ( “string_1A” , “string_1B” ), ( “string_2A” , “string_2B” ) ] ?
In python, square brackets denote an array, and parenthesis denote a tuple - which is an immutable (unmodifiable) array. Yes, you can use it as per your example I think I was too lazy to create a dictionary lol.
Oh, cool! I didn’t know about tuples. Usually just work with dictionaries & arrays.
That will def come in handy for coordinate pairs & such, up until now I’ve always just made parallel arrays & use for-range instead of for-each so that I can index them both simultaneously. Tuples seem like they would help out with code readability.
Also, your key-binding log script is awesome. Just posted a question for you on SO.