Sublime Forum

Change scope programmatically?

#1

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.

1 Like

API Suggestions
#2

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.

1 Like

#3

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.

1 Like

#4

Your so-called scope is called syntax. For example, to set syntax to CSS:

view.set_syntax_file('Packages/CSS/CSS.sublime-syntax')
3 Likes

#5

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?

0 Likes

#6

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 .

1 Like

#7

Syntax files are loaded using the “resource” system. This loads files from:

  1. folders in Packages/ in the “Data” folder
  2. .sublime-package files in Installed Packages/ in the “Data” folder
  3. .sublime-package files shipped in the Packages/ folder that is a sibling of the sublime_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:

  1. {Data}/Packages/My Package/My Syntax.sublime-syntax
  2. {Data}/Installed Packages/My Package.sublime-package/My Syntax.sublime-syntax
  3. {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().

4 Likes