ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

Auto​Set​Syntax

by jfcherng ST3

This plugin automatically sets the syntax for your file depending on its first line if an event is triggered.

Details

Installs

  • Total 3K
  • Win 2K
  • OS X 596
  • Linux 419
Sep 24 Sep 23 Sep 22 Sep 21 Sep 20 Sep 19 Sep 18 Sep 17 Sep 16 Sep 15 Sep 14 Sep 13 Sep 12 Sep 11 Sep 10 Sep 9 Sep 8 Sep 7 Sep 6 Sep 5 Sep 4 Sep 3 Sep 2 Sep 1 Aug 31 Aug 30 Aug 29 Aug 28 Aug 27 Aug 26 Aug 25 Aug 24 Aug 23 Aug 22 Aug 21 Aug 20 Aug 19 Aug 18 Aug 17 Aug 16 Aug 15 Aug 14 Aug 13 Aug 12 Aug 11 Aug 10
Windows 1 2 1 3 1 2 0 3 1 3 2 3 4 5 1 1 0 2 4 4 5 1 5 2 2 7 1 3 2 1 2 2 6 3 2 1 1 1 0 3 2 2 1 0 3 3
OS X 0 1 1 0 1 0 0 1 0 0 0 1 1 3 0 0 0 0 0 0 2 2 0 1 0 0 0 0 1 1 2 0 1 1 0 0 2 0 0 1 2 3 1 1 1 1
Linux 0 1 0 1 0 0 3 2 0 0 0 0 2 1 0 2 0 0 0 1 0 0 1 1 1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0

Readme

Source
raw.​githubusercontent.​com

Sublime-AutoSetSyntax

This repository is a plugin for Sublime Text 3. It automatically sets the syntax for your file if an event is triggered. The original thought comes from here.

Examples

PHP Tag

  1. Create a new tab.
  2. Type <?php.
  3. The syntax will be set to PHP automatically. (triggered by on_modified_async)

Colored Git Log

  1. Prerequisites: ANSIescape and SideBarGit.
  2. Set your colored git log command. I personally set git config --global alias.l "log --graph --date=short --color --pretty=format:'%C(yellow bold)%h%Creset%C(auto)%d%Creset - %s %C(green bold)[%an]%Creset %C(blue bold)(%ad, %cr)%Creset'".
  3. Add "ANSIescape/ANSI.tmLanguage": ["^\\s*\\[SideBarGit@.*\\] git l\\b"] to syntax_mapping.
  4. Add source.diff to working_scope like "working_scope": "(?x)^(text.plain | source.diff)\\b".
  5. Execute your customized git log command. In this example, it is git l as set in the previous step.
  6. The output syntax will be set to ANSI which provides ANSI color rendering. (triggered by on_modified_async)

More Creative Usages To Share?

Feel free to create an issue or a pull request.

User Settings

{
    "event_listeners": {
        "on_activated_async": true,
        "on_clone_async": true,
        "on_load_async": true,
        "on_modified_async": true,
        "on_new_async": true,
        "on_post_paste": true,
        "on_pre_save_async": true,
    },
    "first_line_length_max": 80,
    "log_level": "INFO",
    "syntax_mapping": {
        "PHP/PHP.": [
            "<\\?php",
            ...
        ],
        ...
    },
    "working_scope": "^text.plain\\b"
}
  • event_listeners
    • on_activated_async": Called when a view gains input focus.
    • on_clone_async": Called when a view is cloned from an existing one.
    • on_load_async": Called when the file is finished loading.
    • on_modified_async": Called after changes have been made to a view.
    • on_new_async": Called when a new buffer is created.
    • on_post_paste": Called after there is a paste operation.
    • on_pre_save_async": Called just before a view is saved.
  • first_line_length_max
    • >= 0: The maximum length to lookup in the first line.
    • < 0: No limitation.
  • log_level
    • Determine how detailed log messages are. The value could be “CRITICAL” (very few), “ERROR”, “WARNING”, “INFO”, “DEBUG” (most detailed) and “NOTHING” (nothing).
  • syntax_mapping
    • key: The partial path of a syntax file. Of course, you can use a full path like Packages/PHP/PHP.sublime-syntax.
    • value: Regular expressions to match the first line.
  • working_scope
    • The scope that this plugin should work (regular expression). Leave it blank to match any scope.

Commands

You may disable all event_listeners in your user settings and add a key binding to set syntax.

{ "keys": ["ctrl+alt+s", "ctrl+alt+s"], "command": "auto_set_syntax" },

How It Works

When this plugin is loaded:

  1. Read all syntax definition files.
  2. Try to find first_line_match in .sublime-syntaxs (if ST >= 3084) and firstLineMatch in .tmLanguages.
  3. Merge syntax_mapping with results from the previous step.

When an event is triggered:

  1. May check conditions like cursor counts, cursor position and etc…
  2. Make sure working_scope matches the scope of the first character.
  3. Call command auto_set_syntax.

When command auto_set_syntax is called:

  1. Match the first line with results we found while loading plugin.
  2. If there is any luck, set the corresponding syntax for the user.

Debug

Debug messages are printed to your Sublime Text console (Ctrl+`), which looks like

AutoSetSyntax: [ERROR] regex compilation failed in user settings "working_scope": ^text.plain\b+
AutoSetSyntax: [WARNING] "event_listeners -> on_pre_save_async" is not set in user settings (assumed true)
AutoSetSyntax: [INFO] match syntax file "php-grammar/PHP." with "Packages/php-grammar/PHP.sublime-syntax"

See Also

Supporters

Thank you guys for sending me some cups of coffee.