<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/127846>127846</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang-format] Support for parameters in matrix style
</td>
</tr>
<tr>
<th>Labels</th>
<td>
enhancement,
clang-format
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
SunBlack
</td>
</tr>
</table>
<pre>
If you currently want to display matrices so that they are also easily readable in the code, you must currently switch off Clang format so that the otherwise valid formatting rules do not apply at this point.
E.g., we have in our code:
```cpp
// clang-format off
const std::vector<float> matrix3x4 = {
-12.0F , 2.0F, 2.0F, 42.25F,
6.125F, -2.0F, 7.0F, 7.0F,
9.0F , 9.0F, -1337.0F, NAN
};
// clang-format on
```
Without turning it off, it would looks like (assuming no line break due to maximum line length):
```cpp
const std::vector<float> matrix3x4 = { -12.0F, 2.0F, 2.0F, 42.25F, 6.125F, -2.0F, 7.0F, 7.0F, 9.0F, 9.0F, -1337.0F, NAN };
```
The second is difficult to read, while the first is much easier to understand. The disadvantage of the first one is that other formatting errors are not noticed, such as the use of tabs between values where only spaces are allowed.
Therefore, it would be nice if Clang-Format would offer something to avoid having to switch off Clang-Format completely.
### Solution proposal
```cpp
// clang-format matrix(3, 4)
const std::vector<float> matrix4x4 =
{
-12.0F , 2.0F, 2.0F, 42.25F,
6.125F, -2.0F, 7.0F, 7.0F,
9.0F , 9.0F, -1337.0F, NAN
};
```
There could also be options for `.clang-format`:
```yml
MatrixStyle:
ColumnSize: EqualColumns
FillSpace: RightBeforeComma
DecimalSeparatorAlignment: true
```
Values explanation:
- `ColumnSize`:
- `PerColumn`: The column width is determined by the maximum cell width for each column independently.
```cpp
// clang-format matrix(3, 4)
const std::vector<float> matrix3x4 = {
-12.0F , 2.0F, 2.0F, 42.25F,
6.125F, -2.0F, 7.0F, 7.0F,
9.0F , 9.0F, -1337.0F, NAN
};
```
- `EqualColumns`: The column width is determined by the maximum cell width of all columns. The width of the first column is independent of the others, but it influences the following columns (to avoid unnecessary indentation at the beginning).
```cpp
// clang-format matrix(3, 4)
const std::vector<float> matrix3x4 = {
-12.0F , 2.0F, 2.0F, 2.25F,
6.125F, -2.0F, 7.0F, 7.0F,
9.0F , 9.0F, -1337.0F, NAN
};
```
- `FillSpace`:
- `RightBeforeComma`: The values are aligned to the left and the remaining space is filled with blanks before the comma.
```cpp
// clang-format matrix(3, 4)
const std::vector<float> matrix3x4 = {
-12.0F, 2.0F , 2.0F , 2.25F,
6.125F, -2.0F, 7.0F , 7.0F ,
9.0F , 9.0F , -1337.0F, NAN
};
```
- `RightAfterComma`: The values are aligned to the left and the remaining space is filled with blanks after the comma.
```cpp
// clang-format matrix(3, 4)
const std::vector<float> matrix3x4 = {
-12.0F, 2.0F, 2.0F , 2.25F,
6.125F, -2.0F, 7.0F , 7.0F,
9.0F, 9.0F, -1337.0F, NAN
};
```
- `Left`: The values are aligned to the right and the remaining space is filled with blanks.
```cpp
// clang-format matrix(3, 4)
const std::vector<float> matrix3x4 = {
-12.0F, 2.0F, 2.0F, 2.25F,
6.125F, -2.0F, 7.0F, 7.0F,
9.0F, 9.0F, -1337.0F, NAN
};
```
- `DecimalSeparatorAlignment`: If floating points are recognized, the numbers are also aligned to the decimal separator. (Note: So that it looks in the code as if you format a table in Latex with the [`dcolumn`](https://ctan.org/pkg/dcolumn) package)
- `true`:
```cpp
// clang-format matrix(3, 4)
const std::vector<float> matrix3x4 = {
-12.0F , 2.0F, 2.0F, 2.25F,
6.125F, -2.0F, 7.0F, 7.0F ,
9.0F , 9.0F, -1337.0F, NAN
};
```
- `false`:
```cpp
// clang-format matrix(3, 4)
const std::vector<float> matrix3x4 = {
-12.0F, 2.0F, 2.0F, 2.25F,
6.125F, -2.0F, 7.0F, 7.0F,
9.0F, 9.0F, -1337.0F, NAN
};
```
I had also briefly thought about whether you can specify the clang formation for types, so that you can set it e.g. for `glm::mat4x4` or `QVector4x4` that the rule always applies, but the clang format does not parse the code content like e.g. Clang-Tidy does, there are probably too many cases where Clang-Format would not recognize that this type is currently being used (e.g. if a `QVector4x4` is expected as a parameter, so that the type can only be recognized from the parsing of the called method).
I hope I haven't made any mistakes with the examples now, as I revised them while writing.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzMWF1vKjkS_TXNSykIDAnhgQeSTKRIs9HuZjT7XO2upr1x2722O4T59auyu6Eh5CZodjX3KroY_F11zqkqo_dqY4hW2fVddv0wwjZU1q1eWnOnUb6OclvsVk8l7GwLsnWOTNA72KIJECwUyjcad1BjcEqSB28hVBggVLQDdASovQVCr_QOHGGBuSZQhgeAtAVl4j6uXbc-DDbwWxVkBbYs4V6j2UBpXY1huD7YUJHbKk_whloV3ZCgzAZcq8lDYcHYANg0egdxkvLQWGXCOJuss8n6l_FmzAfYElT4Fs9lW5fONeMB2c0k_cmm4a_iMROPIPlEV92JbFlmk7W0xgfwoeB5s_UbyWBdNrsvtcWQzX5JFnqfvc8hmz1AtrjLJmu4morx5BGAzwDcjA04tOdiLK65xaMB4GY8Td_hajh8sW93LR6-PCy97PqvprPZYSw8r5_5UouHbHb32e3M0ArJav9SobJtgNA6w8ZWyQjinltb2-oCtLWvHrR6JcjELXrf1jzSWNDKEOSO8BWKlhhDNb6ruq1TjyazCVUmlmftf7mVOxPz4U4_97Y9Z9XFyefy5HNoyef1MxyMeGyr3yoCT9KaApSHQpWlkq2O3GE2RPBVSlMEdKmcDzyubmUVSUOOR7amIOcDmmIMvGChPBZvaAJuCGw5mGsN8fxIkUiPISnIOet8ZCXTwtigJMUjeN4PfVyo9WlNzD3kFLZEhvnVkodtRY7AGuZng0z3xHBtt1SM9xd2VFpHR3jICYySBKqj89VjglfqtWVJDrytKVR80GAB36wqmJTd91M96BeQtm40BdK7bv9MzNIfvFjdBmUNNM421qOOHDpBFBPkI-oTjDJxO4tAYTjy0G_Cb57gl1ZPRAe4kOuXED3--4LtierwI5w6FmR2R5TsnMA2bD7PEGKzjY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