Author Topic: Problem with C# Beautify  (Read 7527 times)

Wanderer

  • Senior Community Member
  • Posts: 557
  • Hero Points: 23
Problem with C# Beautify
« on: May 20, 2010, 11:49:23 AM »
When I try Tools->Beautify on this code (C#):
Code: [Select]
void main()
{
            try
            {
                            _values.WordSalad = new FoofSalad()
                                                      {
                                                          Brush = new SolidSaladBrush(Colors.Red),
                                                          Salad = SaladFromInt(Crazy.IntValue),
                                                          Name = Crazy.Name
                                                      };
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }
            }

I get this error:
Quote
7: Unexpected right brace, possibly missing ;

SlickEdit Version 15.0.0.6     Build Date: May 07, 2010
Emulation: Vim

OS: Windows Vista
OS Version: 6.00.6002  Service Pack 2

Project Type:
Language: .cs (C#)

Hotfixes:
C:\Users\Administrator\Documents\My SlickEdit Config\15.0.0\hotfixes\hotfix_se1500_3_cumulative.zip (Revision: 3)
C:\Users\Administrator\Documents\My SlickEdit Config\15.0.0\hotfixes\hotfix_se1500_4_cumulative.zip (Revision: 4)

Scott H

  • Senior Community Member
  • Posts: 240
  • Hero Points: 9
Re: Problem with C# Beautify
« Reply #1 on: May 20, 2010, 01:18:31 PM »
Hi wanderer, I see the same thing you're seeing, and I see this in v14 as well.

Before going to far, I have to ask if this is valid C#.  I've never seen new used the way you used it there, and it's the syntax that's throwing off the formatter.  I'm guessing that this example includes somewhere a class definition for FoofSalad that takes three parameters in the constructor; a brush named Brush, an int named Salad and a string(?) called Name.  Like I said, I've never seen this type of object initialization before, so please let me know if this is a part of C# I've missed (C# does have some crazy conventions that make me roll my eyes sometimes).

If I make a few tweaks to your example to make it valid C#, like so:
Code: [Select]
void main()
{
            try
            {
                            _values.WordSalad = new FoofSalad();
                                                      {
                                                          Brush b = new SolidSaladBrush(Colors.Red);
                                                          Salad s = SaladFromInt(Crazy.IntValue);
                                                          Name n = Crazy.Name;
                                                      }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }
            }

it beautifies fine.

vivitron

  • Senior Community Member
  • Posts: 162
  • Hero Points: 10
Re: Problem with C# Beautify
« Reply #2 on: May 20, 2010, 01:29:17 PM »
Scott,

I'm afraid that's not valid code to initialize the variable... Here is an article about C# 3.0 and new object initializers: http://msdn.microsoft.com/en-us/library/bb397680.aspx

I've had the same problem for a while now... Wanderer's code is valid and compiles fine...

So, given this class:
Code: [Select]
       class testClass
        {
            public string arg1;
            public string arg2;
            public string arg3;
        }

I can initialize like this:
Code: [Select]
           try
            {
                var WordSalad = new testClass()
                                                  {
                                                      arg1 = "test1",
                                                      arg2 = "test2",
                                                      arg3 = "test3"
                                                  };
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }

And it compiles fine... But fails C# Beautify...

Code: [Select]
           try
            {
                var WordSalad = new testClass();
                                                  {
                                                      arg1 = "test",
                                                      arg2 = "test",
                                                      arg3 = "test"
                                                  }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }
            

Will beautify, but not compile....

Fixing this issue would be huge... I've been ignoring it for a while myself...

Thanks!
« Last Edit: May 20, 2010, 01:32:37 PM by vivitron »

Scott H

  • Senior Community Member
  • Posts: 240
  • Hero Points: 9
Re: Problem with C# Beautify
« Reply #3 on: May 20, 2010, 01:50:25 PM »
Well, I've learned something new today.  I'll log a bug for this and we'll see if we can get it in for 15.0.1.  Thanks!

vivitron

  • Senior Community Member
  • Posts: 162
  • Hero Points: 10
Re: Problem with C# Beautify
« Reply #4 on: May 20, 2010, 01:56:00 PM »
Thank you!

Also, not sure if any support is necessary for named parameters or optional parameters, but those are new C# 4.0 features:
http://msdn.microsoft.com/en-us/library/dd264739.aspx




Scott H

  • Senior Community Member
  • Posts: 240
  • Hero Points: 9
Re: Problem with C# Beautify
« Reply #5 on: May 20, 2010, 01:58:24 PM »
Oh C#, why do you do stuff like this?  Thanks for the heads up vivitron, I'll add that to the bug entry.

Wanderer

  • Senior Community Member
  • Posts: 557
  • Hero Points: 23
Re: Problem with C# Beautify
« Reply #6 on: June 22, 2010, 06:59:48 PM »
Well, I've learned something new today.  I'll log a bug for this and we'll see if we can get it in for 15.0.1.  Thanks!
One of the irritating things about Beautify, and this problem highlights it, is that even when I select a region and use Tools->Beautify, the operation breaks on code outside the selected region.
That makes the Beautify op useless for many of my C# files...

Wanderer

  • Senior Community Member
  • Posts: 557
  • Hero Points: 23
Re: Problem with C# Beautify
« Reply #7 on: June 24, 2010, 06:19:23 PM »
... and when the Beautify operation breaks, it doesn't even take you back to the area you have selected...

vivitron

  • Senior Community Member
  • Posts: 162
  • Hero Points: 10
Re: Problem with C# Beautify
« Reply #8 on: June 24, 2010, 06:24:25 PM »
Are you referring to C# Regions or is a region a highlighted block of code?

Wanderer

  • Senior Community Member
  • Posts: 557
  • Hero Points: 23
Re: Problem with C# Beautify
« Reply #9 on: June 24, 2010, 07:46:34 PM »
Are you referring to C# Regions or is a region a highlighted block of code?
Selected block of code.  For example, a single method, from opening brace to closing brace inclusive, or a try block within its braces.


Scott H

  • Senior Community Member
  • Posts: 240
  • Hero Points: 9
Re: Problem with C# Beautify
« Reply #10 on: June 25, 2010, 04:27:16 AM »
Quick update here.  I fixed Wanderer's original problem of beautifying the code that initialized the class members in the braced block, and that will be included in 15.0.1.  I have not yet fixed the C# 4.0 named parameter initialization style that vivitron added, but I'm going to try to get that in for 15.0.1.  I've also not fixed this new issue of returning you to your original location if beautification can't complete, but I've logged a bug for it.  Just wanted to let you know that these things are not just getting lost in the thread.  Given the fix for Wanderer's original issue, hopefully it will "break" a lot less and this won't be as big of a problem.

Wanderer

  • Senior Community Member
  • Posts: 557
  • Hero Points: 23
Re: Problem with C# Beautify
« Reply #11 on: June 25, 2010, 11:09:04 AM »
Appreciate the update, Scott, thanks.