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 if possible.

Details

Installs

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

Readme

Source
raw.​githubusercontent.​com

Sublime-AutoSetSyntax

Package Control GitHub tag (latest SemVer) Project license GitHub stars

This plugin automatically sets the syntax for your file if possible. The original thought comes from here.

Installation

This package is available on Package Control by the name of AutoSetSyntax.

Examples

Guess the Syntax After Stripping Unimportant File Extensions

  1. config_gitlab.yml.example -> config_gitlab.yml -> Ah! .yml should use the YAML syntax.
  2. See try_filename_remove_exts settings for details.

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 \\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

Click to expand

{
    // When should this plugin work?
    "event_listeners": {
        // called when a view gains input focus
        "on_activated_async": true,
        // called when a view is cloned from an existing one
        "on_clone_async": true,
        // called when the file is finished loading
        "on_load_async": true,
        // called after changes have been made to a view
        "on_modified_async": true,
        // called when a new buffer is created
        "on_new_async": true,
        // called after there is a paste operation
        "on_post_paste": true,
        // called just before a view is saved
        "on_pre_save_async": true,
    },
    // The max lookup length for the first line.
    // A negative number means no limitation.
    "first_line_length_max": 80,
    // How detailed log messages should be?
    // "CRITICAL" (very few), "ERROR", "WARNING", "INFO", "DEBUG" (most tedious) or "NOTHING" (no log)
    "log_level": "INFO",
    /**
     * The syntax maaping rules.
     *
     * @key The partial (or full) resource path of a syntax file.
     * @value Regexes to match the first line.
     */
    "syntax_mapping": {
        // "Packages/PHP/PHP.sublime-syntax": [
        //     "<\\?php",
        //     "<\\?=",
        // ],
    },
    // The partial (or full) resource path of the syntax file used when creating a new file.
    // Nothing would happen if this is a empty string.
    "new_file_syntax": "",
    // The scope that this plugin should work (regex).
    // Leave it blank will result in matching any scope.
    "working_scope": "^text\\.plain\\b",
    // Try to remove these file extensions from the file name
    // so a syntax may be assigned due to a stripped file name.
    "try_filename_remove_exts": [
        "-dev",
        "-development",
        "-dist",
        "-prod",
        "-production",
        "-test",
        ".backup",
        ".bak",
        ".default",
        ".dist",
        ".example",
        ".inc",
        ".include",
        ".local",
        ".sample",
        ".test",
        ".tpl",
    ],
}

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. Construct the syntax mappings.

    1. Read all syntax definition files.
    2. Try to find following informations.
- `first_line_match` in `.sublime-syntax`s and `firstLineMatch` in `.tmLanguage`s.
  - `file_extensions` in `.sublime-syntax`s and `fileTypes` in `.tmLanguage`s.
  1. Merge syntax_mapping with results from the previous step.

    1. If the scope is text.plain, try to remove some extensions from the file name basing on try_filename_remove_exts. A syntax may be assigned due to a stripped file name matches.

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 extensions with the file name.
  2. Match the first line with file content.
  3. 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