Sublime Forum

[SOLVED] How to get automatic indentation in custom language

#1

I have started writing a package for EViews and am well underway with the syntax file. But I would like for Sublime to indent the code automatically for e.g. if statements, for loops and while loops. I guess that I should write a plugin for this, but it would be great to get a confirmation of that. Also, how do I make sure that a plugin is only activated when the syntax is set to EViews?

1 Like

#2

you don’t need to write a plugin for automatic indentation, you just need to create a tmPreferences metadata file with the indentation rules:
http://docs.sublimetext.info/en/latest/reference/metadata.html?highlight=indentation

3 Likes

#3

Much better! If I decide to write a plugin later on, how do I ensure that is only activated for the EViews syntax?

0 Likes

#4

technically a plugin is always active if it is enabled, but when it receives events like view activated, or selection changed, the plugin efficiently and quickly makes a check to see if it should ignore the event or not.

For example, your plugin could check which syntax is active for the view:

>>> view.settings().get('syntax')
'Packages/Python/Python.sublime-syntax'

or, more commonly, only operate when the scope matches the base scope defined in your syntax file:

>>> view.match_selector(0, 'source.python')
True

same idea with keybindings, only operate in a context where the scope selector matches.

2 Likes

#5

Great! Thank you! Now, the final question: how do I tick off this topic as [SOLVED] in the forum? #newbie

0 Likes

#6

you can rename the topic title and put [SOLVED] before your original title :slight_smile:

1 Like

#7

You may also be interested in looking into existing indentation rules e.g. from python

3 Likes