1. 程式人生 > >[SUBLIME TEXT] 使用 SUBLIMEASTYLEFORMATTER 套用程式碼樣式

[SUBLIME TEXT] 使用 SUBLIMEASTYLEFORMATTER 套用程式碼樣式

這真的滿神奇的小工具,AStyle Formatter 預設會讓第一個大括號停在 if 或 fuction 的同一行。如果想讓第1個左大括號下去單獨一行需要修改設定值。

SublimeAStyleFormatter is a simple code formatter plugin for Sublime Text. It provides ability to format C, C++, Cuda-C++, OpenCL, Arduino, C#, and Java files.

Mac 上的修改方式,先開啟檔案:

~/Library/Application Support/Sublime Text 3/Packages/SublimeAStyleFormatter/SublimeAStyleFormatter.sublime-settings:

{
// Please visit http://astyle.sourceforge.net/astyle.html for more information
"options_default": {
// Default bracket style
// Can be one of "allman", "bsd", "break", "java", "attach", "kr", "k&r",
// "k/r" "stroustrup", "whitesmith", "banner", "gnu", "linux", "horstmann",
// "1tbs", "otbs ", "google", "pico", "lisp", "python", "vtk", or null
// for default.
"style": "allman",
}
}

SublimeAStyleFormatter Plugin  的安裝方式:

用 Package Install 來安裝~

Sublime Text > Tools > Command Palette > Package Control: Install Package,

找到 SublimeAStyleFormatter 安裝~

SublimeAStyleFormatter Plugin  的使用方式:

選擇 Tools > Command Palette > SublimeAStyleFormatter: Format Current File,

就可以套用樣式了~

輸入 「format」就可以看到 command:

熱鍵是 Ctrl + Option + F

官方的貼心說明檔案:

Usage

Key Bindings

The default key bindings for this plugin:

Windows, Linux:

  • Ctrl+Alt+F: Format current file.
  • Ctrl+KCtrl+F: Format current selection.

OSX:

  • Ctrl+Alt+F: Format current file.
  • +K+F: Format current selection.

Command Palette

Open the command palette, it appears as SublimeAStyleFormatter: Format Current File and SublimeAStyleFormatter Format Current Selection.

Settings

Per-project Settings

Before starting, you may want to have a look at SublimeAStyleFormatter.sublime-settings.

To edit your project setting, select Project/Edit Project from main menu. A project setting contains per-project settings for SublimeAStyleFormatter should look like this:

{
    "settings":
    {
        "AStyleFormatter":
        {
        }
    }
}

For example, if you don’t want to inherit the default settings, instead, use your own astylerc file for C and C++ individually, then your project setting might look like this:

{
    // project folders, etc
    // ...
    // project settings
    "settings":
    {
        "AStyleFormatter":
        {
            "options_default":
            {
                // Use 2 spaces for indentation
                "indent": "spaces",
                "indent-spaces": 2
            },
            "options_c":
            {
                "use_only_additional_options": true,
                "additional_options_file": "/path/to/your/astylerc/for/c"
            },
            "options_c++":
            {
                "use_only_additional_options": true,
                "additional_options_file": "/path/to/your/astylerc/for/c++"
            }
        }
    }
}