www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Automated D code editing?

reply Lubos Pintes <lubos.pintes gmail.com> writes:
Hi,
I am still playing with DGUI library. Besides other things, I would like 
to convert enum names from 
"THIS_STUPID_NAMING_CONVENTION_WHICH_I_ABSOLUTELY_HATE" to "thisGoodOne".
Obviously I could do this by hand but it is a bit time consuming. Any 
tool / hack to help me with this?
Thank
Oct 12 2012
next sibling parent "Aziz K." <aziz.koeksal gmail.com> writes:
On Fri, 12 Oct 2012 19:51:02 +0200, Lubos Pintes <lubos.pintes gmail.com>
wrote:

 Hi,
 I am still playing with DGUI library. Besides other things, I would like  
 to convert enum names from  
 "THIS_STUPID_NAMING_CONVENTION_WHICH_I_ABSOLUTELY_HATE" to "thisGoodOne".
 Obviously I could do this by hand but it is a bit time consuming. Any  
 tool / hack to help me with this?
 Thank

Refactoring tools are not available for D as of yet. Your best bet is to use a bash command or your editor's Find & Replace in Files feature.
Oct 12 2012
prev sibling next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 10/12/12, Lubos Pintes <lubos.pintes gmail.com> wrote:
 Hi,
 I am still playing with DGUI library. Besides other things, I would like
 to convert enum names

You mean at compile-time? Try this: http://dpaste.dzfl.pl/06b95c3f Copied below: import std.conv; import std.string; import std.traits; private property string toCamelCase(E)() if (is(E == enum)) { string result; result ~= "enum CamelCase {\n"; foreach (Member; EnumMembers!E) { foreach (word; to!string(Member).split("_")) { result ~= word.capitalize; } result ~= " = " ~ Member.stringof ~ ",\n"; } result ~= "}"; return result; } template CamelCase(E) if (is(E == enum)) { mixin(toCamelCase!E); } enum SOME_ENUM { VAL_FIRST = -4, VAL_SECOND } alias CamelCase!SOME_ENUM SomeEnum; void main() { SomeEnum en1 = SomeEnum.ValFirst; SOME_ENUM en2 = SOME_ENUM.VAL_FIRST; assert(en1 == en2); }
Oct 12 2012
parent Lubos Pintes <lubos.pintes gmail.com> writes:
Although I thought about refactoring, which I know is not available yet, 
this was very interesting example (for me as newbye).
Dňa 12. 10. 2012 21:43 Andrej Mitrovic  wrote / napísal(a):
 On 10/12/12, Lubos Pintes <lubos.pintes gmail.com> wrote:
 Hi,
 I am still playing with DGUI library. Besides other things, I would like
 to convert enum names

You mean at compile-time? Try this: http://dpaste.dzfl.pl/06b95c3f Copied below: import std.conv; import std.string; import std.traits; private property string toCamelCase(E)() if (is(E == enum)) { string result; result ~= "enum CamelCase {\n"; foreach (Member; EnumMembers!E) { foreach (word; to!string(Member).split("_")) { result ~= word.capitalize; } result ~= " = " ~ Member.stringof ~ ",\n"; } result ~= "}"; return result; } template CamelCase(E) if (is(E == enum)) { mixin(toCamelCase!E); } enum SOME_ENUM { VAL_FIRST = -4, VAL_SECOND } alias CamelCase!SOME_ENUM SomeEnum; void main() { SomeEnum en1 = SomeEnum.ValFirst; SOME_ENUM en2 = SOME_ENUM.VAL_FIRST; assert(en1 == en2); }

Oct 13 2012
prev sibling next sibling parent reply Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
On Fri, 12 Oct 2012 19:51:02 +0200
Lubos Pintes <lubos.pintes gmail.com> wrote:

 Hi,
 I am still playing with DGUI library. Besides other things, I would
 like to convert enum names from 
 "THIS_STUPID_NAMING_CONVENTION_WHICH_I_ABSOLUTELY_HATE" to
 "thisGoodOne". Obviously I could do this by hand but it is a bit time
 consuming. Any tool / hack to help me with this?
 Thank

If you're on Windows and know Python, you could download Programmer's Notepad 2 <http://www.pnotepad.org/> which supports scripting in Python to make custom macros/tools/extensions/etc. Maybe someone already made such a PN2 script, I don't know. It does already have a built-in keyboard shortcut for "convert to lowercase" (ctrl-u), which might at least help. Then you could manually uppercase the letter after each _ and then regex search/replace to get rid of the _'s. IIRC, Visual Studio has a similar scripting feature, but not in Python (VBScript maybe? Or C#? I forget), so maybe you could do the same with Visual-D. Or, it would probably be pretty simple to write a little script in D that just does a regex search for say, `[A-Z_][A-Z_]+` and then converts each instance. Unfortunately I don't know offhand of a tool that would do it all automatically without code needing to be written. :(
Oct 12 2012
parent Lubos Pintes <lubos.pintes gmail.com> writes:
Blindly replacing identifiers is certainly not what I need, I want for 
example
FormStartPosition.CENTER_SCREEN to be written like 
FormStartPosition.centerScreen;
Enums in DGUI are written like
enum FormStartPosition {
   CENTER_SCREEN = SOME_WINDOWS_API_VALUE,
   ...
}
I need SOME_WINDOWS_API_VALUE unchanged. But it may be possible to do 
this with some script, I certainly check the notepad2.

Dňa 12. 10. 2012 22:36 Nick Sabalausky  wrote / napísal(a):
 On Fri, 12 Oct 2012 19:51:02 +0200
 Lubos Pintes <lubos.pintes gmail.com> wrote:

 Hi,
 I am still playing with DGUI library. Besides other things, I would
 like to convert enum names from
 "THIS_STUPID_NAMING_CONVENTION_WHICH_I_ABSOLUTELY_HATE" to
 "thisGoodOne". Obviously I could do this by hand but it is a bit time
 consuming. Any tool / hack to help me with this?
 Thank

If you're on Windows and know Python, you could download Programmer's Notepad 2 <http://www.pnotepad.org/> which supports scripting in Python to make custom macros/tools/extensions/etc. Maybe someone already made such a PN2 script, I don't know. It does already have a built-in keyboard shortcut for "convert to lowercase" (ctrl-u), which might at least help. Then you could manually uppercase the letter after each _ and then regex search/replace to get rid of the _'s. IIRC, Visual Studio has a similar scripting feature, but not in Python (VBScript maybe? Or C#? I forget), so maybe you could do the same with Visual-D. Or, it would probably be pretty simple to write a little script in D that just does a regex search for say, `[A-Z_][A-Z_]+` and then converts each instance. Unfortunately I don't know offhand of a tool that would do it all automatically without code needing to be written. :(

Oct 13 2012
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 10/13/12, Lubos Pintes <lubos.pintes gmail.com> wrote:
 Although I thought about refactoring, which I know is not available yet,
 this was very interesting example (for me as newbye).

Ah ok. Well it wouldn't be too difficult to write a small D script that does this on source files. You wouldn't need a full-fledged parser just to rename enums.
Oct 13 2012
prev sibling parent "Aziz K." <aziz.koeksal gmail.com> writes:
On Fri, 12 Oct 2012 21:43:07 +0200, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 You mean at compile-time? Try this: http://dpaste.dzfl.pl/06b95c3f

 Copied below:

 [SNIP]

That's pretty ingenious!
Oct 13 2012