Sublime Forum

Automatically set view syntax according to first line

#1

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 :slightly_smiling: ?

0 Likes

#2

Yes, you could write a simple Python plugin to do it - see the API, in particular view.set_syntax_file

0 Likes

#3

the issue that am not good with python in anyway or shape :frowning:

0 Likes

#4

I had never used python before writing ST3 plugins, it is a really easy language to learn :slight_smile:
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…

2 Likes

#5

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

0 Likes

#6

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.

0 Likes

#7

this is for the saved files, not the unsaved ones which is what am after :slightly_smiling:

0 Likes

#8

what makes you think that the file has to be saved for the code I provided to set the syntax? have you tried it?

0 Likes

#9

I provide a manual tip tested on ST 3109 Win.

  1. select all (ctrl+a)
  2. cut (ctrl+x)
  3. paste (ctrl+v)
  4. done

This works for all syntaxes which define a first_line_match.

2 Likes

#10

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 :heart_eyes:

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 :sunglasses:

0 Likes

#11

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 :wink:,

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.

0 Likes

#12

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 :wink:,

Yes. Before your post, actually, I prefer ctrl+shift+p -> ssphp rather than using mouse. I hardly use the method I post here lol.

0 Likes

#14

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 :video_game: .

0 Likes

#15

 

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.

1 Like

#16

many thanx, will definitely give that a go, thanx again :8ball:

0 Likes

#17

How about

  1. read all existing syntax definition files (is this possible?)
  2. find out all possible first_line_matchs with their corresponding syntaxes
  3. build syntaxes = [('<?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…

1 Like

#18

That’s definitely the way to go for a solid plugin, the dict method I mentioned above was me being lazy but still helpful :grin:

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
2 Likes

#19

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 :slightly_smiling: I think I was too lazy to create a dictionary lol.

1 Like

#20

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.

1 Like

#21

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. :+1: Just posted a question for you on SO.

0 Likes