最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to refactor in Sublime Text? (Ruby, Rails, JavaScript) - Stack Overflow

programmeradmin18浏览0评论

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

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

Share Improve this question asked Sep 10, 2013 at 12:08 Daryll SantosDaryll Santos 2,0813 gold badges23 silver badges42 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 8

I wrote this plugin for JavaScript refactoring https://github.com/s-a/sublime-text-refactor

I guess there are a lot more out there supporting RoR.

What works for me is the "Find all" option (Ctrl+F and Alt+Enter).

This way you can edit the text and all the matches will be edited at same time.

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.

Alongside ctrl+click you can use ctrl+D also. Works for me.

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

The sublime-text-refactor plugin 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 components variable).

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.

In NodeJs, what I did to ensure refactor across files was to create a gulp task that replaces the value and rewrites the file:

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 app directory, 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.

I advise to delete the gulp task once you are done with your refactoring as it can be very dangerous ! CAUTION

发布评论

评论列表(0)

  1. 暂无评论