简体   繁体   English

HLint可以自动进行建议编辑吗?

[英]Can HLint automatically do suggested edits?

HLint gives suggestion how one can improve source code. HLint给出了如何改进源代码的建议。 However, given the nature of the suggestions, I wonder whether it's possible to automatically apply those suggestions. 但是,鉴于建议的性质,我想知道是否可以自动应用这些建议。

Is it possible to automatically apply suggestions proposed by hlint ? 是否可以自动应用hlint提出的建议?

You have to use --refactor and have the refactor executable in your $PATH . 你必须使用--refactor并在$PATH使用refactor可执行文件。 See hlint's README for more information: 有关更多信息,请参阅hlint的自述文件

Automatically Applying Hints 自动应用提示

By supplying the --refactor flag hlint can automatically apply most suggestions. 通过提供--refactor标志,hlint可以自动应用大多数建议。 Instead of a list of hints, hlint will instead output the refactored file on stdout. 而不是提示列表,hlint将在stdout上输出重构文件。 In order to do this, it is necessary to have the refactor executable on you path. 为此,有必要在您的路径上执行refactor refactor is provided by the apply-refact package, it uses the GHC API in order to transform source files given a list of refactorings to apply. refactorapply-refact包提供,它使用GHC API来转换源文件,给出要应用的重构列表。 Hlint directly calls the executable to apply the suggestions. Hlint直接调用可执行文件来应用建议。

Note that hlint doesn't apply transformations recursively, so additional --refactor could be necessary. 请注意,hlint不会递归地应用转换,因此可能需要额外的--refactor That being said, make sure to commit/save before you apply hlint --refactor and test your code afterwards, since the changes might break your code, especially if you use Rank2Types or seq . 话虽如此,请确保在应用hlint --refactor之前提交/保存并在之后测试代码,因为更改可能会破坏您的代码,尤其是在使用Rank2Types或seq

To complement Zeta's answer, I use some additional flags to perform the refactoring in place: 为了补充Zeta的答案,我使用了一些额外的标志来执行重构:

hlint  --refactor --refactor-options="--inplace" path/to/your/File.hs

Sometimes you only want to apply a certain kind of hint, which can be done by passing the -o flag, eg: 有时您只想应用某种提示,这可以通过传递-o标志来完成,例如:

hlint  -o="Use fewer imports" --refactor --refactor-options="--inplace" 

As far as I know, the suggested refactorings need to be done in a per-file basis. 据我所知,建议的重构需要在每个文件的基础上完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM