Reputation: 2081
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
Upvotes: 18
Views: 54281
Reputation: 399
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.
Upvotes: 8
Reputation: 4354
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
Upvotes: 0
Reputation: 1973
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
Upvotes: 0
Reputation: 2081
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.
Upvotes: 5
Reputation: 3497
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.
Upvotes: 8