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 2K
  • Win 1K
  • OS X 404
  • Linux 266
Dec 15 Dec 14 Dec 13 Dec 12 Dec 11 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
Windows 1 4 3 2 5 5 1 2 1 1 0 4 1 2 1 1 2 1 3 0 1 1 4 1 3 4 1 3 4 3 2 5 4 1 4 0 5 1 2 1 0 2 0 3 2 1
OS X 0 1 1 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 0 2 1 0 1 2 0 0 2 1 3 0 1 1 0 2 0 1 0 2 0 1 0 1 0 0 1 0
Linux 2 0 1 0 0 0 2 1 0 0 0 1 0 1 0 0 0 0 0 0 2 2 0 0 1 0 2 0 3 0 4 0 3 0 1 1 2 0 0 1 0 1 0 1 0 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

License

The MIT License (MIT)

Copyright © 2017 Jack Cherng

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Supporters

Thank you guys for sending me some cups of coffee.