Author |
Topic |
|
Dusan
Whole Tomato Software
Slovakia
177 Posts |
Posted - Sep 04 2013 : 02:33:12 AM
|
I would like to see a refactoring tool to replace selected text with define preprocessor.
Let's say I have a code which contains many times the same string:
std::string my_text = "Hello World!";
...
return "Hello World!";
...
if (my_text.contains("Hello World!"))
return true;
... Now, I would like to select a "Hello World!" and click Refactor (VA X)->Create define. VAX opens a dialog similar to its Rename dialog, so I can chose name for new macro (#define) and also can select instances to be replaced by it. Let's say I keep it all selected and name for macro is "MyLib_HELLO_WORLD".
Result after I click OK would be:
// at top of the file
#define MyLib_HELLO_WORLD "Hello World!"
...
std::string my_text = MyLib_HELLO_WORLD;
...
return MyLib_HELLO_WORLD;
...
if (my_text.contains(MyLib_HELLO_WORLD))
return true;
...
Also it would be fine to make an option to select/create a header file to which add this define and if VAX would also #include this header in all files, where replaced an instace.
How about it?
|
Edited by - Dusan on Sep 04 2013 02:43:00 AM |
|
feline
Whole Tomato Software
United Kingdom
19014 Posts |
Posted - Sep 04 2013 : 10:21:09 PM
|
In situations like this, does the string tend to exist only in one file or function, or does it tend to be more spread out?
We are considering adding a refactoring command to replace a constant, a string or a number, with a variable, which could then be easily converted into a #define if you preferred.
case=28761 |
zen is the art of being at one with the two'ness |
|
|
Dusan
Whole Tomato Software
Slovakia
177 Posts |
Posted - Sep 05 2013 : 01:16:21 AM
|
quote: In situations like this, does the string tend to exist only in one file or function, or does it tend to be more spread out?
I do not mean only strings. It could create a macro from ANY selected text.
For example if there is the function that returns the active instance, such as: MyObj* getActiveMyObj(); Let's say, that this is used in multiple files inside of project/solution and I want a macro for it named ACTIVE_OBJECT in header file "my_macros.h", so I could select a code getActiveMyObj(), click "Create define", set name of new header to "my_macros.h", set name of macro to ACTIVE_OBJECT, modify check boxes and click OK. VAX should create "my_macros.h" file if it does not exist, puts line '#define ACTIVE_OBJECT getActiveMyObj()' into it, replaces all chosen instances of text "getActiveMyObj()" by "ACTIVE_OBJECT" in project/solution and adds line '#include "my_macros.h"' to all files where it was replaced, where it is not already there. |
Edited by - Dusan on Sep 05 2013 01:18:06 AM |
|
|
feline
Whole Tomato Software
United Kingdom
19014 Posts |
Posted - Sep 05 2013 : 3:21:14 PM
|
This helps, I have a clearer sense of what you are after here. Some of the building blocks for this are already present in existing VA refactoring commands, which helps a bit.
This might be a silly question, but why do you want to create so many macros? Replacing a literal string, or other value with a constant makes a lot of sense. But I am not so sure why you are thinking about replacing general pieces of existing code with macros. Macros can complicate things, and complex macros are often involved in VA bug reports, so I have learned to be wary of them. They do have good uses, but I try to minimise them personally
You could do quite a bit of this just by using the IDE's Find and Replace command, since it looks like a simple find and replace would be quite reliable, based on your examples so far, followed up by using Add Include, once the macro has been defined in a header file. |
zen is the art of being at one with the two'ness |
Edited by - feline on Sep 05 2013 3:22:22 PM |
|
|
Dusan
Whole Tomato Software
Slovakia
177 Posts |
Posted - Sep 06 2013 : 05:21:02 AM
|
Ofcorse, wrongly named macro is a hell, but if macros are named in format _ProjectName_MacroName, all should be fine - and such macro is modification is moch more human friendly than using of Find and replace + Add include.
Well, I can do anything manually, but VAX could do it much more faster and perhaps more precisely than me. Imagine how long it could take to do such operation in project like QT :) or something similar. My currect project contains more than 100 of .h/.cpp files containing tons of hardcoded strings. Now imagine that I would need to localize such project... my IDE night mare...
What I'm looking for in general is a C/C++ alternative to "Extract to resource". |
|
|
feline
Whole Tomato Software
United Kingdom
19014 Posts |
Posted - Sep 07 2013 : 1:34:53 PM
|
This all makes sense. I am a little wary of adding more macro's to solutions, since people sometimes do strange things with macros but replacing the hard coded strings is a very solid argument. I have put in a feature request to see what our developers make of this:
case=76695 |
zen is the art of being at one with the two'ness |
|
|
Dusan
Whole Tomato Software
Slovakia
177 Posts |
Posted - Sep 08 2013 : 03:13:34 AM
|
Thanks |
|
|
|
Topic |
|