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

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

Click to expand

{
    "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",
            ...
        ],
        ...
    },
    "new_file_syntax": "",
    "working_scope": "^text.plain\\b",
    "try_filename_remove_exts": [
        ".backup",
        ".bak",
        ".default",
        ".dist",
        ".example",
        ".inc",
        ".include",
        ".local",
        ".sample",
    ],
}
  • 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.
  • new_file_syntax
    • The partial path of a syntax file. This syntax would be applied when creating a new file. Nothing would happen if this is a empty string.
  • working_scope
    • The scope that this plugin should work (regular expression). Leave it blank to match any scope.
  • try_filename_remove_exts
    • For text.plain scope, try to remove these file extensions from the file name and may set a syntax corresponding syntax by the stripped file name.

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.