Sublime Forum

Syntax not applied

#1

I have a menu option that creates a file and at some point applies the syntax and colour schema to the file:

class ApplyFormatCommand(sublime_plugin.WindowCommand):
	def run(self):
self.window.active_view().settings().set("syntax", "Packages/xxx/xxx.sublime-syntax")
self.window.active_view().settings().set("color_scheme", "Packages/xxx/xxx.sublime-color-scheme")

The function works with no problem when called from the menu directly, on the opened file. But when called from my function that creates a file, it shows “Plain Text” on the right bottom while if I click on it the xxx syntax is “checked”. Is this a bug or do I need to refresh somehow?
st1
st2

This seems to only happening to me on Linux, not on Windows.

0 Likes

#2

what if you call your command after a short timeout? like

sublime.set_timeout(lambda: window.run_command('apply_format'), 1)

create file via ST’s API is async iirc.

1 Like

#3

As an additional note, you should use view.assign_syntax() to set the syntax for a buffer; if you change the syntax setting, the syntax will be applied but the syntax specific settings will not. This can lead to weird inconsistencies between files that seemingly use the same syntax and tends to cause future headaches.

2 Likes

#4

Thanks for the answers!!

sublime.set_timeout(lambda: self.window.run_command('apply_format'), 1)

made the trick. Thanks @jfcherng. For some reason this is not needed on my Windows computer.

Thanks @OdatNurd, I have changed:
self.window.active_view().settings().set("syntax", "Packages/xxx/xxx.sublime-syntax")

To:
self.window.active_view().assign_syntax("Packages/xxx/xxx.sublime-syntax")

Is it necessary to also change the color_scheme or is it implicit? If it is necessary, is there an “assign_color_scheme”? I can’t even find the assign_syntax here:
https://www.sublimetext.com/docs/3/api_reference.html#sublime.View

0 Likes

#5

There’s nothing similar for color schemes, no; only the Syntax.

The reasoning there is that the syntax can have syntax specific settings applied to it, and those need to be changed at the point where the syntax is changed. Sublime doesn’t monitor what settings you’re altering with the settings() API, so you need to tell it explicitly when the syntax changes.

0 Likes

#6

So, (forgive my ignorance), then it is necessary to do both, theassign_syntax() and the self.window.active_view().settings().set("color_scheme",..., isn’t it?

0 Likes

#7

If you’re always setting the color scheme the same every time, you can make your life easier by adding the color_scheme setting to the syntax-specific settings for whatever syntax you’re setting. Then any file that uses that syntax automagically gets that color scheme.

So, it’s either necessary or not depending on whether you always want it to happen/use the same color scheme.

0 Likes

#8

The colour scheme is always the same for that syntax, so I always want it to happen. Can this be done on the sublime-syntax file itself or is a settings specific for my user?

I can’t find asnything here: https://www.sublimetext.com/docs/3/syntax.html

I would like to send the whole folder to a friend and not having to deal with user specific settings, so if configuration is needed I’d rather use the line of code.

0 Likes

#9

What you do is open up a file of whatever type you want, make sure the syntax is set, then use Preferences > Settings - Syntax Specific. That will open the settings file applied to files of that particular type. All of the default global settings apply automatically, and any settings you put in that file will override everything else.

It’s documented here: https://www.sublimetext.com/docs/settings.html#syntax-specific_settings

0 Likes

#10

Yes, thanks, I know that. The issue with this is when I move the plugin folder to a new computer I will have to go to Preferences > Settings, etc. So I think in code will be better for my case.

By the way that the original problem happened again. This time the correct syntax was shown in the bottom right (not “Plain Text” as before) but the syntax was not applied, I had to reselect it.

0 Likes

#11

Well, presumably you also have to copy the plugin you’ve created too, so why not also copy the setting at the same time? If you’re putting your plugin in a package, you could include the appropriate settings file there too.

For your overall problem, it’s hard to say what’s going wrong without seeing what it is you’re trying to do. Can you provide a small self contained example of how you’re triggering the command above in a way that replicates the error for you so that we can troubleshoot better?

0 Likes

#12

I will try to isolate the issue in a way that can be reproduced. In the meantime, I will just reapply the syntax. Thanks

0 Likes