How to avoid inserting extra space between operators on using Artistic Style for PHP?

How to avoid inserting extra space between operators on using Artistic Style for PHP?

14
Basic UserBasic User
14

    Jan 07, 2015#1

    I have been able to get the Artistic Style to ALMOST work with PHP5... only a couple issues that I need a bit of help with:

    Given the following

    Code: Select all

    <?php
    error_reporting (E_ALL);
    if ( php_sapi_name() !== 'cli' ) {
    the artistic style returns this:

    Code: Select all

    <? php
    error_reporting (E_ALL);
    if ( php_sapi_name() != = 'cli' ) {
    The extra space in <?php and in !== both break the code.
    Any idea how I can suppress this?

    6,686585
    Grand MasterGrand Master
    6,686585

      Jan 08, 2015#2

      Artistic Style Formatter is designed for C/C++, C# and Java, but not for PHP. Therefore it produces a wrong result on combination of operators which does not exist in those languages like <? or !== on using option --pad-oper.

      I get with option oper checked in Space padding group and using default for Mode the same output as you on running Artistic Style Formatter on a *.php file with your example block.

      But I get following also wrong output with option oper checked in Space padding group and using java for Mode:

      Code: Select all

      < ?php
      error_reporting (E_ALL);
      if ( php_sapi_name() != = 'cli' ) {
      I think the best solution is to go without option --pad-oper, i.e. do not check oper in Space padding group.

      Another solution would be using option oper checked in Space padding group and using java for Mode and run after reformat a Perl regular expression replace with search string (?<=[<=]) (?=[?=]) and an empty replace string to remove a single space between operators.

      (?<=[<=]) is a positive look-behind checking if left to a space character there is either < or =. And (?=[?=]) is a positive look-ahead checking if after the space character there is either ? or =. Put in the square brackets everything needed to correct every wrong inserted space between operators.
      Best regards from an UC/UE/UES for Windows user from Austria

      14
      Basic UserBasic User
      14

        Jan 08, 2015#3

        I will look into the method you recommended

        I must note though, ===, !==, and .== are all JavaScript operators(strict compares) and not limited to just PHP, so it might be nice to (sometime in the future) work them into the list of valid operators...

        6,686585
        Grand MasterGrand Master
        6,686585

          Jan 08, 2015#4

          JavaScript is not Java. And Artistic Style is an open source project. So feel free to contribute to the developer team and enhance this tool for PHP.
          Best regards from an UC/UE/UES for Windows user from Austria

          14
          Basic UserBasic User
          14

            Jan 09, 2015#5

            Thanks for the help.
            (I mean it, you are an EXCELLENT resource for UltraEdit....)

            I am now, on a riff from above, experiencing an additional wee bit of an issue

            So, I tried to put this into a Macro...
            Seemed to be a simple concept:
            Macro:
            perform an Artistic Style ordering
            perform the perl find, with lookbehind for [\.<=] & lookahead for[?=]
            replace with ""

            The second part was easy

            Code: Select all

            InsertMode
            ColumnModeOff
            HexOff
            PerlReOn
            Find RegExp "(?<=[\.<=]) (?=[?=])"
            Replace All ""
            but the first part... Is there any way to "call" the artistic style from a Macro?

            (and yes, I am going to talk to the AStyle authors about making this tweak for PHP)

            6,686585
            Grand MasterGrand Master
            6,686585

              Jan 10, 2015#6

              All commands of UltraEdit which open a dialog for the user to choose settings are not available as macro or scripting command with the exception of sort, find, replace, find in files and replace in files.

              However, Artistic Style is just an executable installed with UltraEdit in subdirectory GNU. So it is possible to configure a user tool or a project tool to run Artistic Style on active file using as command line:

              "UltraEdit program files directory\GNU\astyle.exe" options "%f"

              The options to use can be seen in Artistic Style Formatter dialog. They can be also stored in an options file and this options file is specified on command line.

              UltraEdit prepends additionally the options --ascii and -sX for default indent width whereby X specifies the number of spaces configured for active file type according to tab stop / indent value setting.

              astyle.exe can be run with command line option --help in a command prompt window or from within UltraEdit via Advanced - DOS Command to get help.

              The macro can run the configured user or project tool and then runs the Perl regular expression on reloaded file reformatted by Artistic Style.

              It is of course also possible to output reformatted contents to console captured by UltraEdit to a new file, making the replaces on this new file, copying the reformatted contents to clipboard, closing new file containing captured output without saving, and pasting reformatted code over original file contents selected before. That would be perhaps better as it makes it possible to undo the changes by using Undo command to undo the paste over entire selected file contents.

              Let me know if you need more help on configuring user tool running astyle.exe and UltraEdit macro to run this tool and correct wrong inserted spaces.
              Best regards from an UC/UE/UES for Windows user from Austria