利用webstrom的宏,保存文件自动格式化代码

来源:互联网 发布:庚子赔款 知乎 编辑:程序博客网 时间:2024/06/06 00:00


这个git的截图大家是不是很熟悉,一个项目多人协作。有人提交前,格式化了代码,有人没有,进仓库的时候,各种合并!真正修改的几行,散落在这些无用的代码中,不方便快速定位!

如果大家都用同样的IDE,IDE在保存文件的时候,能自动格式化,就能最大限度的规避这种问题!

很幸运,webstorm正好有这个功能!

PS: go的fmt已经在语言级别实现保存自动格式代码了!不需要在ide设置宏!

参考资料:http://stackoverflow.com/questions/21217791/how-to-auto-format-code-in-webstorm

Record the macro

  1. Edit > Macros > Start Macro recording
  2. Press Ctrl+Alt+L, and then Ctrl+Alt+S
  3. Stop recording the macro clicking on the Stop button on the bottom right of the page.
  4. Give this macro a name like "Format and Save"

Assing Ctrl+S to "Format and Save"

  1. open WebStorm->Preferences;
  2. search for "keymap" and open it;
  3. search "Format and Save" and double click the action "Format and Save";
  4. select "Add Keyboard Shortcut";
  5. select "Cmd+S" as first stroke.
  6. it will report conflicts. Ignore it and click OK button
  7. WebStorm will show a warning "The shortcut is already assigned to other actions. Do you want to remove other assignments?" Click "Remove" button

That's it.


0 0