Is there any way to change the scope of the current file programmatically? I have already looked at several conversion plugins, but either none of them changes the scope after conversion or it’s simply not possible.
Change scope programmatically?
When you say “change the scope”, I’m not sure exactly what you mean. The scopes are assigned by a syntax. I am presuming you don’t just want to change the syntax.
Currently there is no way to change the scope of a token without changing the syntax that defines the scope. You can use add_regions()
to annotate sections of code with an outline/underline, etc, However you can’t change the scope of individual tokens.
I have a package that converts certain files, for instance XML to CSON. At the end of the conversion, I would love to display the result using the correct highlighting, hence change the scope.
Your so-called scope
is called syntax
. For example, to set syntax to CSS
:
view.set_syntax_file('Packages/CSS/CSS.sublime-syntax')
Thanks @jfcherng, works great!
Just to be certain: this only working for bundled and extracted packages, right? Or are there any means to access syntax definitions inside a .sublime-package
file or from the Cache folder?
You can use view.settings().get('syntax')
to get current syntax path.
It’s mentioned at https://www.sublimetext.com/docs/3/api_reference.html .
Syntax files are loaded using the “resource” system. This loads files from:
- folders in
Packages/
in the “Data” folder -
.sublime-package
files inInstalled Packages/
in the “Data” folder -
.sublime-package
files shipped in thePackages/
folder that is a sibling of thesublime_text
binary
For backwards compatibility, all files are referenced using Packages/My Package/My Syntax.sublime-syntax
. This is true whether they are stored in Packages/
or Installed Packages/
, and whether they are packed in a .sublime-package
file, or loose files in a folder.
As a concrete example, Packages/My Package/My Syntax.sublime-syntax
will look in:
{Data}/Packages/My Package/My Syntax.sublime-syntax
{Data}/Installed Packages/My Package.sublime-package/My Syntax.sublime-syntax
{Install Folder}/Packages/My Package.sublime-package/My Syntax.sublime-syntax
Some additional info:
- Files with the same name in
{Data}/Packages/My Package/
will override{Data}/Installed Packages/My Package.sublime-package
or{Install Folder}/Packages/My Package.sublime-package
- The file
{Data}/Installed Packages/My Package.sublime-package
will completely override{Install Folder}/Packages/My Package.sublime-package
These rules are true for the various file types: .sublime-syntax
, .tmPreferences
, .sublime-snippet
, etc. It also holds true for files loaded via sublime.load_resource()
and sublime.load_binary_resource()
.