Author Topic: Beautify code in SE2008  (Read 13747 times)

Christoph

  • Senior Community Member
  • Posts: 114
  • Hero Points: 6
Beautify code in SE2008
« on: April 04, 2008, 02:36:20 pm »
The primary (and currently only) language I am coding using SE is PHP.  I've gone through all the settings I could find for beautification (Tools -> Options -> Languages -> Web Authoring Languages -> PHP -> Indent, etc) and set it to how I wanted SE to behave.  I looked for a specific settings for beautify but was unable to find one, though the help file shows screenshots of a "C/C++ Beautifier" dialog...?

In any case, the option to beautify my code is disabled.  Tools -> Beautify is dimmed out and when I try running "c-beautify-selection" or "c-beautify" from the command, I get the message saying that "Command is disabled".  Trying to run "gui-beautify" results in the same message.  So how can I enable it for my language? 

thnx,
Christoph

Christoph

  • Senior Community Member
  • Posts: 114
  • Hero Points: 6
Re: Beautify code in SE2008
« Reply #1 on: April 04, 2008, 03:03:20 pm »
Ok, so I ran across the following in the help:

Quote
You are here: Introduction > Supported Languages and Environments

Feature                           
          Languages
 
Code Beautifier
          ActionScript, Ada, C, C#, C++, CFML, HTML, Java, JavaScript, JSP, Slick-C, XML, XSD

Javascript is supported but PHP isn't?  Nor some of the other web development languages that have a C/C++ like syntax?  Why?  This was working just fine in VSE9.x, why take it out of SE2k8?  All I had to do was tell VSE9.x that it should treat PHP like C++ and off I went.  But now I don't even have that option?  That seems like going backwards to me. :(

thnx,
Christoph

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Beautify code in SE2008
« Reply #2 on: April 04, 2008, 03:25:39 pm »
By any chance, is the file readonly?
The Beautify command is enabled for me in a writable PHP file, but it wants to run the HTML beautifier.

It sounds like in the past you had told SE that php should behave like c (i.e. php refers to c).
In 2008 you can do the same thing:
Go to Tools->Options,
Click the Language node,
Click the Application Languages node,
Click the C/C++ node,
Click the General node,
Click the Edit These Extensions button,
Click the + button,
Type "php" in the box,
Click OK on the dialogs to dismiss them.

It won't take effect on files that are already open, though.  You will need to close/reopen them.

Christoph

  • Senior Community Member
  • Posts: 114
  • Hero Points: 6
Re: Beautify code in SE2008
« Reply #3 on: April 04, 2008, 03:39:48 pm »
By any chance, is the file readonly?

No.

The Beautify command is enabled for me in a writable PHP file, but it wants to run the HTML beautifier.

Why does it want to run the HTML beautifier?  Because you've performed the steps below but have added the extension to HTML instead of C/C++?

It sounds like in the past you had told SE that php should behave like c (i.e. php refers to c).

Actually, no.  In VSE9.x, going into "Tools -> Options -> File Extension Setup", both ".inc" and ".php" are set to "Refers to -> phpscript".  Going to the phpscript extension, it refers to nothing.

Also, in VSE9.x, the beautify option is never disabled.  Selecting it brings up a dialog that allows you to specify a language you are using for beautification.  I just normally selected "C/C++".  I find it a little ironic that I can create a brand new JS file, paste a bunch of PHP code and beautify straight off.  It makes little sense to me that specify a language in SE2k8 as you could in VSE9.x

In 2008 you can do the same thing:
Go to Tools->Options,
Click the Language node,
Click the Application Languages node,
Click the C/C++ node,
Click the General node,
Click the Edit These Extensions button,
Click the + button,
Type "php" in the box,
Click OK on the dialogs to dismiss them.

It won't take effect on files that are already open, though.  You will need to close/reopen them.

In doing this, it changes the association for my extension from PHP to C++?  It seems like that's what it's doing.  And I'll need to set up the C/C++ language settings to mirror my PHP language settings?

thnx,
Christoph

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Beautify code in SE2008
« Reply #4 on: April 04, 2008, 05:26:56 pm »
The Beautify command is enabled for me in a writable PHP file, but it wants to run the HTML beautifier.

Why does it want to run the HTML beautifier?  Because you've performed the steps below but have added the extension to HTML instead of C/C++?
On my SE 2008 installation the php extension was listed in the HTML node.  I didn't touch that, so I assume it is the default.  Perhaps it's a typo in the defaults.


It sounds like in the past you had told SE that php should behave like c (i.e. php refers to c).

Actually, no.  In VSE9.x, going into "Tools -> Options -> File Extension Setup", both ".inc" and ".php" are set to "Refers to -> phpscript".  Going to the phpscript extension, it refers to nothing.
Ah.  Then I suppose during code restructuring along the way from v9 to v13, something accidentally broke this part of Beautify.


Also, in VSE9.x, the beautify option is never disabled.  Selecting it brings up a dialog that allows you to specify a language you are using for beautification.  I just normally selected "C/C++".  I find it a little ironic that I can create a brand new JS file, paste a bunch of PHP code and beautify straight off.  It makes little sense to me that specify a language in SE2k8 as you could in VSE9.x
I doubt it's an intentional change.  Seems like a bug that is probably pretty easy for the Slick team to fix.  I tried spending 10 minutes earlier this morning to see if I could come up with a temporary fix for you, but I ran out of time.  I think I found one for you now, though.

In the macro code, it looks like SE is smarter now and only pops up the language chooser dialog when it doesn't "know" what language the current file uses.  You could tweak cformat.e to make it always pop up the language chooser.  Or you can get more sophisticated and only pop up the language chooser for PHP, or even automatically redirect PHP to the C beautifier.  The tweak I show below is the simple one that just always pops up the language chooser dialog:

cformat.e, circa line 151 in gui_beautify() (SE 13.0.0 revision):
Code: [Select]
   lang=p_LangId;
   orig_lang=lang;
   index=find_index("_"lang"_beautify_form",oi2type(OI_FORM));
   int lastModified=p_LastModified;
   if ( true||!index ) {   // CHRISANT: Inserted true|| to always execute this.
      if ( true||BeautifyCheckSupport(lang) ) {   // CHRISANT: Inserted true|| to always execute this.
         lang=show('-modal _beautify_extension_form');
         if ( lang=='' ) {
            // User cancelled
            return('');
         }

Christoph

  • Senior Community Member
  • Posts: 114
  • Hero Points: 6
Re: Beautify code in SE2008
« Reply #5 on: April 04, 2008, 05:47:38 pm »
Also, in VSE9.x, the beautify option is never disabled.  Selecting it brings up a dialog that allows you to specify a language you are using for beautification.  I just normally selected "C/C++".  I find it a little ironic that I can create a brand new JS file, paste a bunch of PHP code and beautify straight off.  It makes little sense to me that specify a language in SE2k8 as you could in VSE9.x
I doubt it's an intentional change.  Seems like a bug that is probably pretty easy for the Slick team to fix.

Hopefully they can (and will) fix it.

In the macro code, it looks like SE is smarter now and only pops up the language chooser dialog when it doesn't "know" what language the current file uses.  You could tweak cformat.e to make it always pop up the language chooser.  Or you can get more sophisticated and only pop up the language chooser for PHP, or even automatically redirect PHP to the C beautifier.  The tweak I show below is the simple one that just always pops up the language chooser dialog:

Ok, how do you use that?  I modified the cformat.e file and reloaded the module.  The "Beautify" menu option is still disabled.  I exited then re-started SE2k8 but nothing seemed to change.  I tried running gui-beautify, beautify and beautify-selection from the command line but I'm still getting the message "Command is disabled for this object".

What am I missing?

thnx,
Christoph

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Beautify code in SE2008
« Reply #6 on: April 04, 2008, 07:29:21 pm »
Comment out the following two lines in _OnUpdate_beautify() in cformat.e, circa line 208:

Code: [Select]
//   _str lang=target_wid.p_LangId;
//   if ( lang!="" && lang!="fundamental" && BeautifyCheckSupport(lang) ) return(MF_GRAYED);

Sorry, the earlier suggestion was sufficient on my install, but only because I had forgotten that my install still has the default config where the php extension refers to HTML instead of to PHP.  To finish the suggested workaround to work on your install, you need to also modify the _OnUpdate_beautify handler per above.

Or if you use a more sophisticated tweak (e.g. skipping the language selection dialog and going straight to the C beautifier for php files) then you might want to instead add " php=c phpscript=c " to the DEFAULT_BEAUTIFIER_EXT string circa line 2928.

Additional:  If you want beautify_selection() to also work then you probably need to update the DEFAULT_BEAUTIFIER_EXT string per above.  I encourage you to look through the cformat.e macro and see how it decides which beautifier to run.  Hopefully the macro code is pretty easy to follow.
« Last Edit: April 04, 2008, 07:33:53 pm by chrisant »

Christoph

  • Senior Community Member
  • Posts: 114
  • Hero Points: 6
Re: Beautify code in SE2008
« Reply #7 on: April 04, 2008, 08:17:05 pm »
Your suggestion above worked.  Thanks! :)

Now I just need to figure out why the beautifier is turning this:

  if( true )
  {
    indented_code_line_1
    indented_code_line_2
    indented_code_line_2
  }

into this:

  if( true )
  {
  not_indented_code_line_1
  not_indented_code_line_2
  not_indented_code_line_2
  }

None of the settings I change in the beautifier dialog seem to make much difference in this behavior.

thnx,
Christoph

Christoph

  • Senior Community Member
  • Posts: 114
  • Hero Points: 6
Re: Beautify code in SE2008
« Reply #8 on: April 07, 2008, 12:10:01 pm »
Your suggestion above worked.  Thanks! :)

Now I just need to figure out why the beautifier is turning this:

  if( true )
  {
    indented_code_line_1
    indented_code_line_2
    indented_code_line_2
  }

into this:

  if( true )
  {
  not_indented_code_line_1
  not_indented_code_line_2
  not_indented_code_line_2
  }

None of the settings I change in the beautifier dialog seem to make much difference in this behavior.

Ok, I've spent all weekend trying to figure out what settings I might need to change or what I might need to do to fix this and I've come up with nothing.  How can I fix it so that the beautifier properly indents the code it is beautifying?

thnx,
Christoph

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Beautify code in SE2008
« Reply #9 on: April 07, 2008, 04:04:31 pm »
Can you post a short sample file that reproduces the problem?
But you have a special config now (per previous posts), so we'll need to work on getting a general repro case.

Also, what happens if you rename the file to a *.cpp file and beautify it?
Does the problem still occur, or is it only when the file is *.php?

Christoph

  • Senior Community Member
  • Posts: 114
  • Hero Points: 6
Re: Beautify code in SE2008
« Reply #10 on: April 07, 2008, 04:25:03 pm »
Can you post a short sample file that reproduces the problem?
But you have a special config now (per previous posts), so we'll need to work on getting a general repro case.
Also, what happens if you rename the file to a *.cpp file and beautify it?
Does the problem still occur, or is it only when the file is *.php?
I've attached 2 sample files that show the behavior.  It seems like it is doing the same thing regardless of extension.

Incidentally, it seems like the changes I made to cformat.e were not loaded in after restarting SE2k8.  I couldn't beautify my PHP code until I went to Macro -> Load Module -> cformat.e (which is what I did on Friday).  I thought that once it was loaded, the .e file would get compiled into the .ex file and thus make it so I wouldn't have to reload each time I restarted SE?

thnx,
Christoph

hs2

  • Senior Community Member
  • Posts: 2737
  • Hero Points: 288
Re: Beautify code in SE2008
« Reply #11 on: April 07, 2008, 04:58:34 pm »
Did you save your settings when exiting SE ?
HS2
BTW: The compiled code is finally merged into vslick.sta.
So if you've changed 'vslick.sta' you lost add. compiled macro modules.

Christoph

  • Senior Community Member
  • Posts: 114
  • Hero Points: 6
Re: Beautify code in SE2008
« Reply #12 on: April 08, 2008, 11:49:09 am »
Did you save your settings when exiting SE ?
HS2
BTW: The compiled code is finally merged into vslick.sta.
So if you've changed 'vslick.sta' you lost add. compiled macro modules.
I wasn't queried as to whether or not I should save my settings when I exited SE.  I restarted the app this morning and it seems like the changes to cformat.e have kept.

But I'm still having the issue with the indention as illustrated in the files I attached above.  I still cannot find the settings that addresses that issue. :(

thnx,
Christoph

hs2

  • Senior Community Member
  • Posts: 2737
  • Hero Points: 288
Re: Beautify code in SE2008
« Reply #13 on: April 08, 2008, 12:18:45 pm »
The query is configurable. The default is 'just save all settings'.
HS2

BTW: IIRC you copied 'rescue.sta -> vslick.sta'. This caused the loss of the add. loaded macro.

Christoph

  • Senior Community Member
  • Posts: 114
  • Hero Points: 6
Re: Beautify code in SE2008
« Reply #14 on: April 08, 2008, 12:46:24 pm »
The query is configurable. The default is 'just save all settings'.

Ok.

BTW: IIRC you copied 'rescue.sta -> vslick.sta'. This caused the loss of the add. loaded macro.

I did copy rescue.sta over vslick.sta.  However, before I did that, I made a backup of vslick.sta and, when I still had errors opening SE2k8 from the command line, I restored the backup.  Though I did that, I don't discount the copying/recopying of vslick.sta as the loss of the cformat.e changes.

Hopefully one of the SE developers will chime in about how to fix my indention/beautification problem.

thnx,
Christoph