Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
User name:
Password:
Save Password
Forgot your password?

 All Forums
 Visual Assist
 Technical Support
 Switch from Enum
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Dusan
Whole Tomato Software

Slovakia
172 Posts

Posted - Oct 10 2013 :  1:29:36 PM  Show Profile  Reply with Quote
Hello, I just want to ask... what is with case 19137?
Currently I am working with project where such refactoring tool would be handy.

feline
Whole Tomato Software

United Kingdom
15980 Posts

Posted - Oct 10 2013 :  2:42:26 PM  Show Profile  Reply with Quote
Unfortunately no progress yet, but we are hoping to get to this fairly soon.

As a short term solution, have you considered doing a regular expression find and replace? For a simple one line enum like this:

enum testConvertToSwitchWithFind { TEST_CONVERT1, TEST_CONVERT2, TEST_CONVERT3, TEST_CONVERT4 };

select just this line, then run an IDE regular expression find and replace across only the selected text, searching for:

{<[^ ]+>},

and replacing with:
case \\1:\\nbreak;\\n

and use Replace all. The result is the slightly messy:

enum testConvertToSwitchWithFind {case TEST_CONVERT1:
break;
case TEST_CONVERT2:
break;
case TEST_CONVERT3:
break;
TEST_CONVERT4 };

but this is much closer to the switch statement than it was before, and it should be possible to improve the regular expression search string with a bit of thought.

zen is the art of being at one with the two'ness
Go to Top of Page

accord
Whole Tomato Software

United Kingdom
3163 Posts

Posted - Oct 10 2013 :  2:56:12 PM  Show Profile  Reply with Quote
Alternatively, VS2012 and above, you can use the switch IDE snippet for this:

Edit -> Intellisense -> Insert Snippet...

Here, you type switch, press enter, type the variable name (in the editor), and press enter again, and the cases will be added.
Go to Top of Page

Dusan
Whole Tomato Software

Slovakia
172 Posts

Posted - Oct 10 2013 :  3:34:06 PM  Show Profile  Reply with Quote
Nothing applicable, but thanks...
I am now working in VS2005 and enums look like:

enum feature_type
{
ft_point_2d = 1,
ft_line_string_2d = 2,
ft_polygon_2d = 3,
ft_point_3d = 101,
ft_line_string_3d = 102,
ft_polygon_3d = 103,
ft_point_4d = 201,
ft_line_string_4d = 202,
ft_polygon_4d = 203,
};

and so on...

Edited by - Dusan on Oct 10 2013 3:49:46 PM
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
15980 Posts

Posted - Oct 10 2013 :  4:51:25 PM  Show Profile  Reply with Quote
In this case, try the following IDE regular expression find and replace.

Find: {[ \\t]+}{<.*>} =.*
Replace: \\1case \\2:\\n\\1break;

The formatting is not perfect, but you can simply select the block and use:

IDE Edit menu -> Advanced -> Format Selection

afterwards to clean up the formatting, using the IDE default formatting. This is working well for me in VS2005 with your sample enum. Obviously use on a copy of the enum declaration, since it is converting the enum into the body of a switch statement. You still need to add the switch line at the top.

zen is the art of being at one with the two'ness
Go to Top of Page

Dusan
Whole Tomato Software

Slovakia
172 Posts

Posted - Oct 11 2013 :  01:33:29 AM  Show Profile  Reply with Quote
quote:
Originally posted by feline

In this case, try the following IDE regular expression find and replace.

Find: {[ \\t]+}{<.*>} =.*
Replace: \\1case \\2:\\n\\1break;


Thanks, I will try this, but VA X snippet would be more handy :)

Snippet "swenum" definition could look like:
switch ($ENUM_VARIABLE$)
{
  $LIST_BEGIN$
  case $ENUM_MEMBER$:
  {
  }
  break;
  $LIST_END$
}


then, lets say I would have in code:
feature_type ft = ft_point_2d;
I will write "swenum" and VA X suggests to use snippet...
that would be great... :)

also it would be fine if there is option to make such snippets with some sort of hint, what to consider as member.
in case of defines like:

#define ft_point_2d 1
#define ft_line_string_2d 2
#define ft_polygon_2d 3
#define ft_point_3d 101
#define ft_line_string_3d 102
#define ft_polygon_3d 103
#define ft_point_4d 201
#define ft_line_string_4d 202
#define ft_polygon_4d 203


Example of snippet to handle this:
$MEMBER=MACRO;REGEX="ft_.*?"$
switch ($VARIABLE$)
{
  $LIST_BEGIN$
  case $MEMBER$:
  {
  }
  break;
  $LIST_END$
}


BTW, where can I find a list of directives (such as $SymbolContext$) supported in VA X snippets?



quote:
The formatting is not perfect, but you can simply select the block and use:

IDE Edit menu -> Advanced -> Format Selection

BTW, for this exists shortcut combination in IDE: CTRL+K, CTRL+F


Edited by - Dusan on Oct 11 2013 01:49:30 AM
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
15980 Posts

Posted - Oct 11 2013 :  12:28:10 PM  Show Profile  Reply with Quote
The IDE find and replace is just intended as a temporary work around, until we add the ability to generate a switch statement from an enum to VA.

Your snippet suggestion to control the generated switch statement makes sense and is interesting, I have put a note onto case=19137 about this.

I like the idea of generating a switch from a set of #define's, but I am not sure that a regular expression is a good approach. While I am a great fan of regular expressions personally, having to work out and test a one off regular expression for each switch statement does not sound very practical. I think it would make much more sense to just select the #define's you want to use, and then trigger a refactoring command on the selected text.

Does this make sense, or do you have a lot of situations where this is not going to work?

Before you suggest it, you cannot just use the IDE find feature to test your regular expression unless VA also uses the same regular expression syntax as the IDE find dialog, which I am not sure about, since it is a custom syntax rather different from any other regular expression syntax I am familiar with

A list of the tokens that work in VA Snippets is given here:

http://www.wholetomato.com/products/features/vasnippets.asp

The default keyboard short cuts in the IDE are very useful, but also tend to vary between different IDE's. I like keyboard shortcuts personally, but spend so much time changing IDE's I loose track of which keyboard shortcuts work where, so menu items are often easier and more reliable

zen is the art of being at one with the two'ness
Go to Top of Page

Dusan
Whole Tomato Software

Slovakia
172 Posts

Posted - Oct 11 2013 :  3:14:28 PM  Show Profile  Reply with Quote
quote:
I am not sure that a regular expression is a good approach. While I am a great fan of regular expressions personally, having to work out and test a one off regular expression for each switch statement does not sound very practical.

I see what you mean and you are right, that way it would be not practical.

quote:
I think it would make much more sense to just select the #define's you want to use, and then trigger a refactoring command on the selected text.

Does this make sense, or do you have a lot of situations where this is not going to work?

Well, but this will require me to copy defines from source to current code and then trigger a command... It does not help much and does not avoid manual searching for #defines. But better than nothing :) Now I have to do all manually or using regular expressions which I am not happy with...
The best option would be to ask for regular expression ( or SQL like pattern, which is perhaps more strightforward ) in input box as it is done for some snippets already - perhaps list of matches could be updated in a list in real time or after click on Preview button. But snippets with LIST functionality will not be limited only to switch, but also usable for if (){} else if {} and perhaps some other options could be found if there will be also the $index$ token :)

quote:
A list of the tokens that work in VA Snippets is given here:
http://www.wholetomato.com/products/features/vasnippets.asp

Thanks... this helped...

Edited by - Dusan on Oct 12 2013 02:27:03 AM
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
15980 Posts

Posted - Oct 12 2013 :  11:05:45 AM  Show Profile  Reply with Quote
SQL, as in database query language? I have not done much with this in years, but I don't see how this is relevant here.

In general, any form of search is unreliable, or unwieldy. If you don't use a fully qualified name for your enum, what about namespaces and classes? The same enum name can easily turn up in different scopes.

#defines can, and often do have multiple definitions for the same name, using #if #else #endif blocks. This is before someone gets clever and has different #defines in different files, with the same names.

None of this means it cannot be done, but its a lot of overhead, for limited benefit. If you have to type in enough of the enum or #define to uniquely identify it, and then confirm your search term, you might as well just use VA's Find Symbol dialog, a bit of Alt-G, or an IDE regular expression find to locate the actual definition you want to work on.

I am torn on the LIST idea in Snippets. I see what you are doing here, but #define's don't come in list form. Enum's do though.

Do you have actual examples beyond a if, else if, end if block?

Enum's and #define's are going to require different handling, so I am wondering about a different approach. We are already looking at create a switch statement from an enum. Allow this to work on selected #define's, and then add a separate command to convert a switch to a if, elseif block. Would this do what you are looking for?

I know this is not as powerful as turning Snippets into a programming language, which is the direction you are moving in, but IDE macros were removed since hardly anyone used them. I used them, and found them very helpful, but from experience most of our users were simply not interested in IDE macros. So sadly I have to wonder at the usefulness of making Snippets into a programming language.

zen is the art of being at one with the two'ness
Go to Top of Page

Dusan
Whole Tomato Software

Slovakia
172 Posts

Posted - Oct 12 2013 :  2:23:08 PM  Show Profile  Reply with Quote
I meant SQL LIKE operator, where "car%" matches everything that starts by "car", but only as a more straightforward way to identify list members. I understand what you mean and agree with you in general.

Regarding to if, else if, etc...

Lets say snippet:

if ()
{
}
$LIST_START[$Size$]$
else if ( )
{
}
$LIST_END$

would with $Size$ = 2 generate

if ()
{
}
else if ( )
{
}
else if ( )
{
}

or snippet:

$LIST_START[$Size$]$
$EVAL($LIST_INDEX$ >= 1 ? "else " : "")$if ($Variable$ == $LIST_INDEX$)
{
}
$LIST_END$

would with $Size$ = 3 && $Variable$ = m_num result in:

if (m_num == 0)
{
}
else if (m_num == 1)
{
}
else if (m_num == 2)
{
}

$EVAL(expression)$ would simply preprocess expression, then evaluate it and place the result as text. I thing that it should not be that hard to implement.

And yes, all users of VA X are programmers, so they perhaps like to programm something :) Snippets with scripting capability would be much more powerful than a macro recorder. BTW, I do not think that users of Visual Studio did not use macros (statistics are perhaps made only on monitored users). My friends use macros every day, because they do not use VA X or anything similar. I do not as much... I use VA X where possible, but still, for attaching debugger to correct application I use macros as well, so I will miss that functionality. However, now I work in VS2005.

Edited by - Dusan on Oct 12 2013 3:19:31 PM
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
15980 Posts

Posted - Oct 12 2013 :  5:12:26 PM  Show Profile  Reply with Quote
*ah* I see, this is no longer about converting enum's into switch statements, now we are actually into turning snippets into a programming language. Personally I tend to agree with you, but I am just not sure how much use this will get. Working in support, and using IDE macros as a tool to help solve problems gives me an interesting perspective here.

I use IDE macros daily in my work, and will be very reluctant to give them up. At the same time, when I suggest them as a possible solution, and even provide a partial macro solution this is normally greeted by a great silence... it seems most of our users are not interested in IDE macros. To be fair, the learning curve for IDE macros is rather steep.

I have put in a feature request to select several #define lines and convert them into a switch statement:

case=77589

and a feature request to convert a switch statement into an if / else if statement:

case=77590

We are considering making VA snippets into more of a scripting language, but don't hold your breath on this one:

case=68740

zen is the art of being at one with the two'ness
Go to Top of Page

sean
Whole Tomato Software

USA
2640 Posts

Posted - Aug 21 2015 :  12:27:51 AM  Show Profile  Reply with Quote
case=19137 is implemented in build 2073 as the command "Add Missing Case Statements"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
© 2019 Whole Tomato Software, LLC Go To Top Of Page
Snitz Forums 2000