Author Topic: c++11/17 and beautify of Pro 2016/2017 Beta  (Read 3181 times)

millerkp

  • Community Member
  • Posts: 16
  • Hero Points: 0
c++11/17 and beautify of Pro 2016/2017 Beta
« on: October 10, 2017, 02:52:30 AM »
It may seem a little picky, but for me, there is nothing more annoying then running a beautifier and having it mess up the formatting of just one junk of code.  If it was really bad, I just wouldn't use it.  But when it is just sooooooo close, I end up spending a lot of time trying to fix it.  You could argue that I am lazy programmer/poor typist; which I admit to at least one.

Take the code sample below.  I was playing with C++11's uniform initialization syntax, which allows for the initialization of struct members from the constructor parameters.  Cool right?  How c/c++ has grown.

Beautify seems to be treating the UIS as a normal member initialization and a code block.  I even downloaded the 2017 RC4  to see if this had been considered; it does not seem so.

There are a lot of knobs, so maybe I am just missing something.  Thought I would put this note out there and see what comes back.

Thank you,

K. Miller

Before beautify (yes, the poor indentation is intentional) :
#include <iostream>

class test
{
public:
test(int a, int b, int c) :
p {a, b, c, 0}
{
}

private:
struct {
int a, b, c, d;
} p;

public:
void output(void)
{
  std::cout << p.a << " " << p.b << " " << p.c << " " << p.d << std::endl;
}

};

main()
{
test t1(5,4,3);
t1.output();
}

After beautify:
#include <iostream>

class test
{
public:
    test(int a, int b, int c) :
        p
    {
        a, b, c, 0
    }
    {
    }

private:
    struct
    {
        int a, b, c, d;
    } p;

public:
    void output(void)
    {
        std::cout << p.a << " " << p.b << " " << p.c << " " << p.d << std::endl;
    }

};

main()
{
    test t1(5, 4, 3);
    t1.output();
}

patrick

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1818
  • Hero Points: 151
Re: c++11/17 and beautify of Pro 2016/2017 Beta
« Reply #1 on: October 10, 2017, 03:58:37 PM »
Looks like a bug to me.  Initially I tried it with the default profile in 2017 rc4, and that looks fine, but changing to profiles with different brace styles caused some different flavors of wrong formatting. 

Also, for me, it's just for the uniform initialization syntax inside of a constructor initialization list that's wrong.  Uniform initialization in a local inside of a function doesn't get mangled. 

I'm looking into it.  The fix probably won't make it into the v22 release, but would be in the subsequent v22 point release.

millerkp

  • Community Member
  • Posts: 16
  • Hero Points: 0
Re: c++11/17 and beautify of Pro 2016/2017 Beta
« Reply #2 on: October 10, 2017, 06:36:53 PM »
OK.  Sounds good as far as that goes.

More and more multi national projects are relying on formatters like clang-format and indent.  Some even have them built into the version control system or worse just checkers that fail the commit on invalid formatting.  It would be nice to be able to completely turn off formatting ie. no automatic indenting/spacing/code completion.  Even better would be allowing for a third party formatter plug-in on (should just be a simple system call).

Thank you,

K. Miller

patrick

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1818
  • Hero Points: 151
Re: c++11/17 and beautify of Pro 2016/2017 Beta
« Reply #3 on: October 10, 2017, 07:20:09 PM »
You can turn off the editing features that try to be smart, but there's not a single switch you can throw to turn everything off. (we do have a feature request for that though).  Each feature has it's own setting that needs to be disabled, so it takes some legwork to get everything to quiet down.

As for third party formatters, if they can read from stdin, and write formatted results to stdout, it's pretty easy to make a command that you can bind to a key to quickly format the buffer.

This is an example I copied from a post about the 'astyle' formatter, only the filter_command() parameter would need to change for a different formatter.  Save this to a .e file, "load" it from the slickedit command line, and then the "reformat_buffer" command should be available to run, or bind to a keypress, menu, or toolbar button.

Code: [Select]
#include "slick.sh"
#import "fileman.e"
#import "util.e"

_command void reformat_buffer() name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_MARK)
{
   _save_pos2(auto saved);
   select_all();
   filter_command('astyle --style=allman');
   _restore_pos2(saved);
}

millerkp

  • Community Member
  • Posts: 16
  • Hero Points: 0
Re: c++11/17 and beautify of Pro 2016/2017 Beta
« Reply #4 on: October 11, 2017, 02:08:19 AM »
Thank you, that did the trick.  I knew there was a reason I love slickedit!

K. Miller

rowbearto

  • Senior Community Member
  • Posts: 2335
  • Hero Points: 132
Re: c++11/17 and beautify of Pro 2016/2017 Beta
« Reply #5 on: December 18, 2019, 05:25:08 PM »
When I use Patrick's macro and run the file through clang-format, the _restore_pos2() is not putting me back at the original position in the file, rather it puts me at the start of the file, not sure why _restore_pos2() doesn't work here?

I've updated the macro to the below so that it can do a java file (actually, any language, using --assume-filename, as without --assume-filename it assumes C++), and saving the old line and column numbers to be able to go back to approximately the old position:

Code: [Select]
_command void reformat_buffer() name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_MARK)
{
   //typeless saved;
   //_save_pos2(saved);
   auto oldLine = p_line;
   auto oldCol = p_col;
   select_all();
   auto cmd = 'clang-format --assume-filename=' p_buf_name ' --style=file';
   filter_command(cmd);
   //_restore_pos2(saved);
   p_line = oldLine;
   p_col = oldCol;
}
« Last Edit: December 18, 2019, 05:27:22 PM by rowbearto »

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6826
  • Hero Points: 526
Re: c++11/17 and beautify of Pro 2016/2017 Beta
« Reply #6 on: December 18, 2019, 09:20:13 PM »
 _save_pos2 won't work for this use case because you filter_command deletes the contents of the entire file first which puts the mark at the top of the file. Inserting the new text won't move the mark. You either must use save_pos/restore_pos (I think that will work) or just do what you are doing isn't bad either.

rowbearto

  • Senior Community Member
  • Posts: 2335
  • Hero Points: 132
Re: c++11/17 and beautify of Pro 2016/2017 Beta
« Reply #7 on: December 19, 2019, 12:18:12 AM »
Thanks Clark! save_pos() and restore_pos() are better than saving the line and col# because when I restore the line and col the line I'm on ends up near the bottom of the screen, whereas save_pos() and restore_pos() preserves the screen position.

So now I'm using:

Code: [Select]
_command void reformat_buffer() name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_MARK)
{
   typeless saved;
   save_pos(saved);
   select_all();
   auto cmd = 'clang-format --assume-filename=' p_buf_name ' --style=file';
   filter_command(cmd);
   restore_pos(saved);
}