MasterID
MasterID

Reputation: 1510

Keeping Argument/Parameters in same line as much as possible

I'm trying to match my clang-format as close as possible to our eclipse formatter. In this migration I have realised that eclipse prefers to keep the parameters/arguments in same line as much as possible: Example: enter image description here

If I change BinPackParameters i will either get:

enter image description here

or:

enter image description here

I want to have the same behaviour as eclipse: "Keep putting parameters in same line as function/method name then wrap when column limit is reached"

How can I achieve this?

Edit: I'm extending Google style

Upvotes: 0

Views: 1254

Answers (1)

Eric Backus
Eric Backus

Reputation: 1924

A few thoughts:

  1. You shouldn't use images to show us code. No big deal, but it makes it hard for somebody to cut and paste your example code to investigate the problem.
  2. I think the setting you're looking for is AlignAfterOpenBracket: DontAlign. Given the two inputs you show above, the command clang-format -style='{BasedOnStyle: Google,AlignAfterOpenBracket: DontAlign}' gives:
      virtual int *XXXXXXXXXXXXX(int *parentaaaa, unsigned int x.unsigned int y,
          int index, int resolution, int zoom);
      virtual int XXXXXXXXXXXXX(int *attrasdasdasd, char *outstr,
          unsigned char mode, unsigned int minlen, unsigned int maxlen);
    
  3. However, there are cases with AlignAfterOpenBracket: DontAlign where clang-format will move all parameters to the next line. You may be seeing one of those cases, and I don't believe there is any good workaround for this. Because of this, you may want to use AlignAfterOpenBrackets: Align (which is the default for Google style). This doesn't exactly match your Eclipse example, but it is reasonably close most of the time. For example, the command clang-format -style='{BasedOnStyle: Google}' gives:
      virtual int* XXXXXXXXXXXXX(int* parentaaaa, unsigned int x.unsigned int y,
                                 int index, int resolution, int zoom);
      virtual int XXXXXXXXXXXXX(int* attrasdasdasd, char* outstr,
                                unsigned char mode, unsigned int minlen,
                                unsigned int maxlen);
    
  4. Your eclipse example (the first example in your question) has the continuation lines indented further. To exactly match that, you would use ContinuationIndentWidth: 8. So the command clang-format -style='{BasedOnStyle: Google,AlignAfterOpenBracket: DontAlign, ContinuationIndentWidth: 8}' gives:
      virtual int *XXXXXXXXXXXXX(int *parentaaaa, unsigned int x.unsigned int y,
              int index, int resolution, int zoom);
      virtual int XXXXXXXXXXXXX(int *attrasdasdasd, char *outstr,
              unsigned char mode, unsigned int minlen, unsigned int maxlen);
    
  5. For more information about these style options, see the documentation.
  6. A good place to experiment with style options, and to see what is different between different versions of clang-format, is the configurator.

Upvotes: 2

Related Questions