SlickEdit Community

SlickEdit Product Discussion => SlickEdit® => Topic started by: Christoph on April 04, 2008, 02:36:20 pm

Title: Beautify code in SE2008
Post by: Christoph 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
Title: Re: Beautify code in SE2008
Post by: Christoph 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
Title: Re: Beautify code in SE2008
Post by: chrisant 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.
Title: Re: Beautify code in SE2008
Post by: Christoph 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
Title: Re: Beautify code in SE2008
Post by: chrisant 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('');
         }
Title: Re: Beautify code in SE2008
Post by: Christoph 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
Title: Re: Beautify code in SE2008
Post by: chrisant 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.
Title: Re: Beautify code in SE2008
Post by: Christoph 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
Title: Re: Beautify code in SE2008
Post by: Christoph 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
Title: Re: Beautify code in SE2008
Post by: chrisant 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?
Title: Re: Beautify code in SE2008
Post by: Christoph 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
Title: Re: Beautify code in SE2008
Post by: hs2 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.
Title: Re: Beautify code in SE2008
Post by: Christoph 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
Title: Re: Beautify code in SE2008
Post by: hs2 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.
Title: Re: Beautify code in SE2008
Post by: Christoph 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
Title: Re: Beautify code in SE2008
Post by: chrisant on April 08, 2008, 06:46:17 pm
Hm, I opened your beautify.cpp file and ran Tools->Beautify on it.
All the lines seem to get properly indented.

You might want to carefully inspect all of the configuration options in the Beautify dialog, and make sure they are all set the way you intend.
Title: Re: Beautify code in SE2008
Post by: Christoph on April 08, 2008, 07:08:00 pm
Hm, I opened your beautify.cpp file and ran Tools->Beautify on it.
All the lines seem to get properly indented.

You might want to carefully inspect all of the configuration options in the Beautify dialog, and make sure they are all set the way you intend.

Perhaps a step by step of the settings you have?  Because that's my problem....  I've looked through all the settings, made various changes to each but it never really solves the issue.

thnx,
Christoph
Title: Re: Beautify code in SE2008
Post by: chrisant on April 08, 2008, 08:56:40 pm
If I post my settings, it will take me a while to compile them (and unfortunately I have a lot of day job stuff I need to get done and I don't know when I'll have time to compile a list of all my settings), but no one else can offer any insight -- you're the only one who can try to diagnose it.  On the other hand, if you post all your settings then it enables other knowledgeable people to take a look at your settings and offer insight.  That's probably the best next step.
Title: Re: Beautify code in SE2008
Post by: Christoph on April 09, 2008, 12:04:24 pm
If I post my settings, it will take me a while to compile them (and unfortunately I have a lot of day job stuff I need to get done and I don't know when I'll have time to compile a list of all my settings), but no one else can offer any insight -- you're the only one who can try to diagnose it.  On the other hand, if you post all your settings then it enables other knowledgeable people to take a look at your settings and offer insight.  That's probably the best next step.

As I stated before, I've tried a number of mix-n-match options to try to address the issue.  Nothing I've tried seemed to work. 

The following are the settings I currently have that were used in the attached file.  The results here are not that dissimilar to those that I posted previously.  The settings that I indicate for each tab of the beautifier gui are for those that are checked.  If the item isn't listed here, it is unchecked.

Begin/end style tab:
* Style 2
* Apply to function braces

Indenting tab:
* Continuation indent: 2
* Indent for each level: 2
* Tab size: 2
* Original tab size: 2

Comments tab:
* Indent stand alone comments
* Indent column 1 comments
* Original relative indent

Other tab:
* Remove padding

thnx,
Christoph
Title: Re: Beautify code in SE2008
Post by: chrisant on April 09, 2008, 06:16:44 pm
With those settings, here is what I get when I run Beautify on the beautify.cpp file you posted:

Code: [Select]
<?php
  
class BeforeBeautify
{
public function 
_construct()
  {
    if (
TRUE)
    {
      switch (
$var)
      {
      case 
1:
        foreach( 
$array as $key => $val )
        {
          echo 
"$key => $val";

        }
        break;
      case 
2:
        break;
      }
    }
  }
}

class 
AfterBeautify
{
public function 
_construct()
  {
    if (
TRUE)
    {
      switch (
$var)
      {
      case 
1:
        foreach( 
$array as $key => $val )
        {
          echo 
"$key => $val";

        }
        break;
      case 
2:
        break;
      }
    }
  }
}

?>


The lines appear indented properly to me.
You're going to need to try running the beautifer on the beautify.cpp file in a stock config.
Try running "vs -sc c:\tmpdir" (may need to create the tmpdir first, not sure) to have SE rebuild a new stock config in tmpdir, then load beautify.cpp and use the beautifier in the stock config.  That should work, and if it does then it means something about your custom config is interfering with the beautifier.  For example it could be that you have a custom macro with a function name that conflicts with a stock function name and replaces the stock function, and ends up interfering with beautify in a roundabout way.
Title: Re: Beautify code in SE2008
Post by: Christoph on April 10, 2008, 12:14:39 pm
With those settings, here is what I get when I run Beautify on the beautify.cpp file you posted:

Code: [Select]
<?php
class BeforeBeautify
{
  public function 
_construct()
  {
    if (
TRUE)
    {
      switch (
$var)
      {
        case 
1:
          foreach( 
$array as $key => $val )
          {
            echo 
"$key => $val";

          }
          break;
        case 
2:
          break;
      }
    }
  }
}

class 
AfterBeautify
{
public function 
_construct()
  {
    if (
TRUE)
    {
      switch (
$var)
      {
      case 
1:
        foreach( 
$array as $key => $val )
        {
          echo 
"$key => $val";

        }
        break;
      case 
2:
        break;
      }
    }
  }
}

?>


The lines appear indented properly to me.

The lines appear properly indented to you?  It's proper for the function definition to appear on the same indent level as the class definition?  The "case" statements to appear on the same indent level as the switch() statement?  That may seem proper to you but it does not seem proper to me and I'm trying to change the settings so that in my examples above, the before and after look the same.

You're going to need to try running the beautifer on the beautify.cpp file in a stock config.
Try running "vs -sc c:\tmpdir" (may need to create the tmpdir first, not sure) to have SE rebuild a new stock config in tmpdir, then load beautify.cpp and use the beautifier in the stock config.  That should work, and if it does then it means something about your custom config is interfering with the beautifier.  For example it could be that you have a custom macro with a function name that conflicts with a stock function name and replaces the stock function, and ends up interfering with beautify in a roundabout way.

I just tried that in trying to track down another issue I'm having with comment blocks.  I was finally able to get SE2k8 started using

vs.exe -sc c:\temp\seconfig

after having removed my VSE9.x directory from the path and replacing vslick.sta with rescue.sta.  The comment issue seemed to be fixed (but that's another thread) but when attempting to beautify the code, SE did so in exactly the same way it did with my other settings.

thnx,
Christoph
Title: Re: Beautify code in SE2008
Post by: chrisant on April 10, 2008, 05:50:27 pm
Ahhhh.

1.  I did not look closely at the class names, and you did not mention that only the second half of the file was the repro case.  I misunderstood what you expected to happen.  Furthermore, the indentation appeared inconsistent to me in the "Before" chunk, and I naturally assumed you had shifted lines as a means of testing whether Beautify was touching them.  It turns out that you are using spaces to indent in some places, and using tabs to indent in other places, and my tab settings are 4, but your tab settings are 2.
2.  In your original statement, you showed that the lines within an "if" did not get indented.  So I ran Beautify, and verified that the lines within the "if" DO get indented.  That was as far as I went.

You need to be more specific about your expectations and observations.

Responding to your most recent questions:

Quote
It's proper for the function definition to appear on the same indent level as the class definition?
In C/C++ the "public" keyword does not work the way you've used it in your sample code.  I assume that is a PHP-specific difference.  In C/C++, the "public:" keyword needs a colon after it, and generally goes on its own line (technically newlines are never needed in C/C++, but I've never seen anything but comments on the same line after a "public:").
So, keep in mind that you are using the C/C++ beautifier to beautify a different language that is merely similar to C/C++.  Now, with that in mind, you said that you do not have "Indent access specifier" checked.  The "public" keyword is an access specifier.  You explicitly told it not to indent access specifiers.  So due to a syntax difference between C/C++ and PHP, that is why your functions aren't being indented.

Quote
The "case" statements to appear on the same indent level as the switch() statement?
Well, yes.  Reread the settings you posted.  You said you do not have "Indent CASE from SWITCH" checked.  You explicitly told Beautify not to indent the "case" statements, so naturally it shouldn't indent them.  (None of the Style 2 projects I've ever worked in indented the case, so from my perspective, I would that yes, from a stylistic perspective as well it appears correct to me for the case not to be indented.  ;))

It seems that if you check the "Indent access specifier", and "Indent CASE from SWITCH" checkboxes, then the things you complained about work properly.
Title: Re: Beautify code in SE2008
Post by: Christoph on April 10, 2008, 06:01:53 pm
It seems that if you check the "Indent access specifier", and "Indent CASE from SWITCH" checkboxes, then the things you complained about work properly.

Re: the CASE checkbox, fair enough.  I did have that checked before and it didn't seem to change anything w/r/t to the behavior but having now just tried it, it seems I wasn't paying enough attention before.  However, w/r/t the "Indent access specifier", I don't see that checkbox anywhere on my (PHP) beautifier dialog.  I don't see it on Indenting tab nor do I see it on the Other tab.  I do see it on the Indenting tab of the C++ Beautifier, however.  I'm curious why the two different dialogs?  Before I altered the cformat.e, I couldn't even get it to beautify my PHP file.  When I altered the cformat.e, I made it (or so I thought) so that the file extensions I'm working with would use the c beautifier.  Around approx line 2929, I changed the existing line to:

#define DEFAULT_BEAUTIFIER_EXT  "ada=ada c=c java=c js=c cs=c e=c html=html cfml=html xml=html xsd=html vpj=xml as=c phpscript=c php=c inc=c tpl=html"   /* Valid beautifier languages */

So why isn't it using the c beautifier?

thnx,
Christoph
Title: Re: Beautify code in SE2008
Post by: Phil Barila on April 10, 2008, 06:48:10 pm
If you want SE to use the c settings, including the beautifier, did you consider just changing the .php extension to refer to .c, instead of hacking at code?
Title: Re: Beautify code in SE2008
Post by: Christoph on April 10, 2008, 06:55:00 pm
If you want SE to use the c settings, including the beautifier, did you consider just changing the .php extension to refer to .c, instead of hacking at code?

Yep, I sure did.  However, in doing that, my color coding went completely out the window.  The IDE no longer color coded PHP's internal functions and, instead of going in and entering all several hundred into the C settings, I figured it'd be much easier just to get the beautifier turned on for the PHP extensions.

thnx,
Christoph
Title: Re: Beautify code in SE2008
Post by: chrisant on April 11, 2008, 12:23:43 am
You said the checkbox wasn't visible in the beautifier when the file extension is php.
So presumably something hid the checkbox.
I searched the "*.e" files in the "macros" directory for the text "access specifier", and sure enough:

In cformat.e, in onCreateIndenting, circa line 2149 is a line that says:
Code: [Select]
   _ctl_indent_access_specifier.p_visible = (_orig_lang == 'c');

Try adding ||_orig_lang == 'php' and see if that makes the checkbox show up.

I'd encourage you to poke around in the macro code.  It's not that hard to read, and the stuff that you've needed to tweak has been pretty easy to find.  Teach a man to fish, and all that.  ;D