1.全局安裝eslint相關
2.webstorm 配置 eslint Preference->Tools->External Tools, Click "+“
i. Cmd+Alt+L (使用editorconfig格式化代碼) ii. Cmd+Alt+S (保存) iii. Tools->External Tools->ESlint Fix
4.Assing Ctrl+S to "Format(editorconfig+eslint) and Save" 5.webstorm的自動格式化需要特殊設置:勾選 Keep when reformatting->Simple methods in one line
PS:每次cmd+s的時候,都要顯示ESlint Fix執行結果的命令行窗口,可以通過cmd+4關閉。
如果覺得每次都需要額外操作來關閉這個窗口,太麻煩,可以選擇將其固定在左側/右側,或者懸浮.
**20170827 更新 ***
最近公司有同事在使用VSCode之後,用了Prettier這個插件,引入了一個新的特性:
Bracket Spacing (Print spaces between brackets in object literals)
效果如下圖:
|
這個特性是EditorConfig不支持的,爲了解決這個問題,只好在Webstorm中引入Prettier,而且用Prettier的規則,覆蓋EditorConfig的規則,在文件保存的時候,只使用Prettier!
1.全局安裝prettier
npm install prettier -g
2.webstorm設置prettier
Preference->Tools->External Tools, Click "+“
更多參數說明和IDE集成說明,請查看https://github.com/prettier/prettier
3.Record the macro
Edit > Macros > Start Macro recording
Tools->External Tools->prettier,and then cmd+4, and then cmd+4 , and then cmd+alt+s
Stop recording the macro clicking on the Stop button on the bottom right of the page.
Give this macro a name like "Format(prettier) and Save"
4.Assing Ctrl+S to "Format(prettier) and Save"
open WebStorm->Preferences;
search for "keymap" and open it;
search "Format(prettier) and Save" and double click the action "Format(prettier) and Save";
select "Add Keyboard Shortcut";
select "Cmd+S" as first stroke.
it will report conflicts. Ignore it and click OK button
WebStorm will show a warning "The shortcut is already assigned to other actions. Do you want to remove other assignments?" Click "Remove" button
****下面是舊版,用的是EditorConfig********
這個git的截圖大家是不是很熟悉,一個項目多人協作。有人提交前,格式化了代碼,有人沒有,進倉庫的時候,各種合併!真正修改的幾行,散落在這些無用的代碼中,不方便快速定位! 如果大家都用同樣的IDE,IDE在保存文件的時候,能自動格式化,就能最大限度的規避這種問題! 很幸運,webstorm正好有這個功能! PS: go的fmt已經在語言級別實現保存自動格式代碼了!不需要在ide設置宏!
參考資料:http://stackoverflow.com/questions/21217791/how-to-auto-format-code-in-webstorm Record the macro
Assing Ctrl+S to "Format and Save"
That's it. PS: websotrm自帶的format其實是EditorConfig,可以在項目下新增一個.editorconfig 文件,覆蓋默認的規則:
|