Sublime Forum

Refactor->Rename

#1

Hello,

Is there a way to “rename” a variable through Sublime Text 2? In many IDE’s, you can press ctrl-shift-r and rename a variable. Sublime Text 2 appears to recognize variables; when you click on a variable, all variables that appear on that page are outlined in a box.

Thanks,

0 Likes

#2

What you’re seeing is just Sublime highlighting all occurrences of the selected word. The program doesn’t differentiate between variables, functions, or just plain text, so functionality as you’re describing isn’t possible. I thought about implementing this as a plugin, where Ctrl+Shift+R does a find/replace for all matches, but that may not be desirable if you use variable names across functions. Your best bet is just Find/Replace within a selected region.

0 Likes

#3

Argh, that’s a shame! Well, thanks for letting me know!

0 Likes

#4

Wouldn’t it be possible to differentiate between functions, variables, strings, etc… when performing a refactor? The absence of this feature is a deal breaker for larger teams that work on big code bases.

0 Likes

#5

In order to fully differentiate between functions, variable, strings, etc. you need to compile the source code. Even then, renaming has to respect scopes:

int i;

void f() {
int i;
}

Those two i’s are two different variables, and renaming one of them must not rename the other.

0 Likes

#6

Yes that’s exactly what the editor needs to do, understand the scope of the refactor so you don’t unintentionally change variable names outside of your scope. Imaging having a code-base with 20 references to a particular variable in multiple javascript files that for whatever reason need to be refactored. Then having to do it to many functions and variables across the project. Performing this manually is not fun and makes my head hurt when you miss something. This is in other editors such as Eclipse, IntelliJ, etc…

0 Likes

#7

It’s not in other editors, it’s in IDEs. The difference being that an IDE has a compiler, hence has deep knowledge of the structure of the text. An editor that uses regular expressions to tease apart the text simply is not capable of the analysis needed for refactoring.

0 Likes

#8

Wouldn’t it be possible to write a plugin that calls ctags?

0 Likes

#9

I am coming from Code::Blocks and variable renaming seems to work rather well in it. At least in C-language projects.
I do not know how it works there, but I do not use C::B with a compiler (I compile the code manually), so there is no compilation involved.
So far I like Sublime and it would be great if it offered the same variable renaming possibility as C::B.

0 Likes

#10

I would love this feature as well and would be perfectly happy if it only recognized scope within the file being edited. Speed is important, and going beyond the single file would likely result in too much performance degradation on large projects.

0 Likes

#11

STRICTLY speaking:

Not true.

Yes.

Not true.

Entirely possible.

OTOH, this would currently only be recommended in the current file, or perhaps a set of open files (i.e., the end-user would want to be extremely vigilant about what they’re doing).


What makes this possible is far simpler than one might imagine. Textmate, and therefore ST, uses scopes for highlighting. Yes, it’s that easy. We would still need someone to script up something which can examine the scope already assigned to a particular piece of code (assuming one is using an existing highlighter; if not, one would need to be created–unfortunately, this isn’t trivial–or “imported” from TM) and go from there, but it should be nearly trivial.

Info on all of this is available in the Textmate and (to a lesser extent) ST docs[1]. I don’t have time right now to scrape together all of the links.

I’m not certain about differences between ST2 & 3, but my sense is that this functionality has not changed significantly.

[1] More specifically, the “unofficial” ST docs.

0 Likes