如何在 Sublime Text 中重构?(Ruby、Rails、JavaScript)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18718579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to refactor in Sublime Text? (Ruby, Rails, JavaScript)
提问by Daryll Santos
I used Netbeans before. How do I do some refactoring (changing variable names, make method out of code, etc) in Sublime Text 2 on a Mac? What I'm doing right now is "select next instance of a word", but that's only because I'm using only one file
我以前使用过 Netbeans。如何在 Mac 上的 Sublime Text 2 中进行一些重构(更改变量名称、使用代码生成方法等)?我现在正在做的是“选择一个单词的下一个实例”,但这只是因为我只使用一个文件
采纳答案by Daryll Santos
What happened here was that I just did refactoring via grepping and find/replace. I'm on Vim now and substitute/grepping is still my method when I need to refactor. I guess this is one of the features that an IDE provides that a text editor doesn't.
这里发生的事情是我只是通过 grepping 和 find/replace 进行了重构。我现在在 Vim 上,当我需要重构时,替代/grepping 仍然是我的方法。我想这是 IDE 提供的功能之一,而文本编辑器则没有。
回答by Stephan Ahlf
I wrote this plugin for JavaScript refactoring https://github.com/s-a/sublime-text-refactor
我为 JavaScript 重构写了这个插件 https://github.com/sa/sublime-text-refactor
I guess there are a lot more out there supporting RoR.
我想还有更多的支持 RoR。
回答by CarlosRos
What works for me is the "Find all" option (Ctrl+F and Alt+Enter).
对我有用的是“查找全部”选项(Ctrl+F 和 Alt+Enter)。
This way you can edit the text and all the matches will be edited at same time.
通过这种方式,您可以编辑文本,同时编辑所有匹配项。
回答by Faysal Ahmed
Alongside ctrl+click you can use ctrl+D also. Works for me.
除了 ctrl+click,您还可以使用 ctrl+D。对我来说有效。
回答by eloone
The sublime-text-refactorplugin works as long as you need to refactor variables. If you need to refactor plain strings it doesn't work. For example if you need to replace 'components'in file paths 'file/*/components'by 'sections'the plugin will not help you because it expects to rename variables (console indicates when I try to refactor: unable to locate componentsvariable).
sublime-text-refactor只要您需要重构变量,该插件就可以工作。如果您需要重构纯字符串,则它不起作用。例如,如果您需要通过插件替换'components'文件路径将无济于事,因为它期望重命名变量(控制台指示我何时尝试重构:无法定位变量)。'file/*/components''sections'components
My answer is not related to sublime text but I was led to this thread when I found the adapted solution so it might help people in the same case. Sublime text is not necesarily the solution for refactoring.
我的答案与 sublime text 无关,但是当我找到经过调整的解决方案时,我被带到了这个线程,因此它可能会在同一情况下帮助人们。崇高的文本不一定是重构的解决方案。
In NodeJs, what I did to ensure refactor across files was to create a gulp task that replaces the value and rewrites the file:
在 NodeJs 中,我为确保跨文件重构所做的是创建一个 gulp 任务来替换值并重写文件:
var gulpReplace = require('gulp-replace');
gulp.task('refactor', function(){
gulp.src(['app/**/*.js'], { base: './' })
.pipe(gulpReplace(/components/g, 'sections'))
.pipe(gulp.dest('./'));
});
$gulp refactor
What this does is that, in all js files under the appdirectory, I replace the string 'components'by the string 'sections'. In my case I needed plain string replacement, it was not variable renaming so the need was very straightforward and this method is very efficient for that. Adapt this method to your case and back up your code before you refactor, as it is the equivalent to find/replace via grep, there is no prior check, be sure of what you're doing.
这样做的作用是,在目录下的所有 js 文件中app,我将字符串替换'components'为字符串'sections'。在我的情况下,我需要简单的字符串替换,它不是变量重命名,所以需要非常简单,而且这种方法非常有效。将此方法适应您的情况并在重构之前备份您的代码,因为它相当于通过 grep 查找/替换,没有事先检查,请确保您在做什么。
I advise to delete the gulp task once you are done with your refactoring as it can be very dangerous ! CAUTION
我建议在完成重构后删除 gulp 任务,因为它可能非常危险!警告
回答by Edmund Sulzanok
What I do is select multiple variables with ctrl+click and the once you type, all selected strings are changes. The only difference is that you need to select them manually. But then again -- once you select the first one, all matching variables are highlighted
我所做的是使用 ctrl+click 选择多个变量,一旦您键入,所有选定的字符串都会发生变化。唯一的区别是您需要手动选择它们。但话又说回来——一旦你选择了第一个,所有匹配的变量都会突出显示

