when i create a new file i just write the code am about to use so something like
<?php
i wish if ST can automatically turn the view syntax to php,
or
html
which will then turn the view syntax into html on its own,
etc…
so could this be done ?
when i create a new file i just write the code am about to use so something like
<?php
i wish if ST can automatically turn the view syntax to php,
or
html
which will then turn the view syntax into html on its own,
etc…
so could this be done ?
Yes, you could write a simple Python plugin to do it - see the API, in particular view.set_syntax_file
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.
Source: Syntax
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.
what makes you think that the file has to be saved for the code I provided to set the syntax? have you tried it?
I provide a manual tip tested on ST 3109 Win.
This works for all syntaxes which define a first_line_match
.
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.
that works but not always
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 .
I just posted an answer @ StackOverflow the other day that implements a dictionary.
You can implement a similar dictionary pattern with the code @kingkeith provided above.
Edit:
@kingkeith, I just noticed that you used
syntaxes = [('<?php', 'Packages/PHP/PHP.sublime-syntax')]
for text, file in syntaxes:
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" ) ]
?
I was in the same boat about a year ago when I first started working with Python & SublimeText.
I’ve managed to cover a good amount of ground since then, and have compiled a list of some helpful resources to get started with.
How about
first_line_match
s with their corresponding syntaxessyntaxes = [('<?php', 'Packages/PHP/PHP.sublime-syntax'), ...]
automatically (customization could be a plus)1~2 are one-time jobs upon starting ST.
It would be a good plugin to be published and even better to be a ST built-in function…
That’s definitely the way to go for a solid plugin, the dict method I mentioned above was me being lazy but still helpful
Should be pretty straightforward to implement:
[1]
syntaxFiles = sublime.find_resources ( "*.sublime-syntax" )
for file in syntaxFiles:
print ( file )
[2]
Parse syntaxFiles
for first_line_match
[3]
matchFound = False
loop @ Plugin.sublime-settings/CustomSyntaxDictionary
if matchFound still == False
loop @ syntaxFiles
fico said:
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.
great idea, I might make it a proper plugin and submit it on Package Control. I already did a similar task to loop through each keymap file the other day on SO.
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.