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

  • 1.10.0
  • github.​com
  • github.​com
  • 2 months ago
  • 31 minutes ago
  • 3 years ago

Installs

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

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

{
    "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.