6
$\begingroup$

I'm refactoring some old code. I used really short names for local variables, which I'm coming to regret. Is there an easy way to rename local variables in scope using Wolfram workbench?

Here is an example

How could I change d inside Test, but not in the Print statement?

Test[d_]:=d+1;
(*Some other code*)
Print[d];

Or the same for l inside Module?

Test1[x_]:=Module[{l},l=x+1];
(*Some other code*)
Print[l];

There is a Ctrl+1 QuickFix rename variable, which I discovered while researching this question, but it seems to work only on the whole file...

Using WB 2.0 (anybody even still uses WB 1.1?)

$\endgroup$
3
  • $\begingroup$ @b.gatessucks Because the the d in test and the d in some other module have different functions. This is a simplified example. In real life I have several "functions", which all have the variable data. But in each function I would like to change data to some specific, more meaningful, name. $\endgroup$
    – Ajasja
    Jan 16, 2013 at 15:51
  • $\begingroup$ I would suggest a two stage procedure: first use the refactoring functionality to replace the symbol with one with a longer, unique name for the whole file. Then select each of the Modules and use the standard eclipse search and replace functionality to (string-)replace the unique longer symbol name with the desired new name which will let you act on the selected lines only... $\endgroup$ Jan 16, 2013 at 22:42
  • $\begingroup$ @AlbertRetey Well, what I'm doing at the moment is copying each module to a temp .m file, do the replacements there and copy back. $\endgroup$
    – Ajasja
    Jan 17, 2013 at 8:37

1 Answer 1

3
$\begingroup$

I don't know if this works in all cases: If you highlight a region with the mouse and press Ctrl+F for the "find and replace", this will only search for words inside the highlighted area. This also works for the replace then. I tried it with a small example and it worked.

$\endgroup$
3
  • $\begingroup$ Yes, this works in all cases (you have to make sure that scope is set to selected lines). Also whole word and perhaps match case should be set under the options in the find/replace dialog. Then it mimics the rename quite well. $\endgroup$
    – Ajasja
    Jan 17, 2013 at 8:35
  • $\begingroup$ Perfect. I wonder if there is some other possibility using "Refactor". I tried to use the Refactor > Pattern Replace on a code selection, but it applies to the whole code instead of only the selection... $\endgroup$
    – g3kk0
    Jan 17, 2013 at 8:40
  • $\begingroup$ Well, I guess I'll accept the Ctrl+F as the best that can be done. $\endgroup$
    – Ajasja
    Jan 21, 2013 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.