VSCode:在git staging时触发organizImports [英] VSCode: trigger organizeImports when git staging

查看:672
本文介绍了VSCode:在git staging时触发organizImports的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢VSCode中的自动整理功能,但是在每次保存时都这样做会给我带来一些麻烦。

  ... 
editor.codeActionsOnSave:{
source.organizeImports:true
},
...

我可以设置一些git钩子来组织导入(通过vscode或其他一些脚本/ lib)吗?



问题



如果我在使用导入之前过早点击保存(让它一直开始更漂亮)方法 - 然后它删除它,我必须再次写入导入。



如果我破坏了代码(.jsx),似乎没有使用某些东西,我点击了保存(为了触发更漂亮),然后删除导入。然后我必须再次导入它们。

解决方案

运行 git add :在 gitconfig .gitattributes 中定义的过滤器。



请参阅




  • 运行 git checkout 时:





您可以在 gitconfig 中定义一个过滤器,其中包含两个清理和涂抹的命令:

  $ git config --global filter.jsximports.clean fiximports 
$ git config --global filter.jsximports.smudge cat

并编辑 .gitattributes 在jsx文件上应用此过滤器的文件

  * .jsx filter = jsximports 






要申请的脚本可能是 tslint --fix ordered-imports 规则



实际上: tslint 's规则似乎有自己的实现,但它做了类似的事情(参见 https://github.com / palantir / tslint / pull / 4064


I like the automated organize feature in VSCode, but having it doing it on every save has given me some trouble.

...
"editor.codeActionsOnSave": {
    "source.organizeImports": true
  },
...

Is it possible to set up some git hooks that organize the imports (either via vscode or some other script/lib) when I stage them?

Issues

If I hit save too early (do it to kick off prettier all the time) before I have used the imported methods - then it removes it and I have to write the import again.

If I break the code (.jsx) and something appears to not be used and I hit save (to trigger prettier), then it removes the imports. I then have to import them again.

解决方案

There is some form of hook that can be applied when running git add : filters defined in gitconfig and .gitattributes.

See this section of git book for detailed explanations.


Here are the sketched from the documentation (it illustrates how you can configure a filter to run on *.txt files) :

  • when running git add :

  • when running git checkout :

You can define in your gitconfig a filter, which consists of two commands to "clean" and "smudge" :

$ git config --global filter.jsximports.clean fiximports
$ git config --global filter.jsximports.smudge cat

and edit the .gitattributes file to apply this filter on jsx files

*.jsx    filter=jsximports


The script to apply may be tslint --fix, with the ordered-imports rule.

Actually : tslint's rule seem to have its own implementation, but it does something similar (see https://github.com/palantir/tslint/pull/4064)

这篇关于VSCode:在git staging时触发organizImports的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆