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 4K
  • Win 3K
  • OS X 858
  • Linux 608
Aug 19 Aug 18 Aug 17 Aug 16 Aug 15 Aug 14 Aug 13 Aug 12 Aug 11 Aug 10 Aug 9 Aug 8 Aug 7 Aug 6 Aug 5 Aug 4 Aug 3 Aug 2 Aug 1 Jul 31 Jul 30 Jul 29 Jul 28 Jul 27 Jul 26 Jul 25 Jul 24 Jul 23 Jul 22 Jul 21 Jul 20 Jul 19 Jul 18 Jul 17 Jul 16 Jul 15 Jul 14 Jul 13 Jul 12 Jul 11 Jul 10 Jul 9 Jul 8 Jul 7 Jul 6 Jul 5
Windows 0 3 0 3 1 1 4 4 2 4 4 2 4 4 5 3 4 2 7 3 4 5 1 5 3 1 4 8 3 4 3 3 1 3 4 8 2 4 2 3 3 6 2 0 1 3
OS X 0 3 0 3 3 1 1 4 1 1 0 0 2 0 0 0 0 0 0 1 4 2 0 0 1 1 2 2 0 0 1 2 0 3 2 1 1 0 1 1 0 0 0 0 0 1
Linux 0 0 2 0 1 1 1 1 2 1 1 1 1 0 1 0 1 0 0 0 0 0 1 0 1 1 0 1 1 0 0 0 1 0 2 0 2 0 1 2 4 0 0 0 1 1

Readme

Source
raw.​githubusercontent.​com

Sublime-AutoSetSyntax

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

Supporters

Thank you guys for sending me some cups of coffee.