unformat
unformat copied to clipboard
Make initial guesses to reduce the number of iterations needed
There are some factors you can look at to reduce the number of guesses needed to make a suitable .clang-format. These tests can be done with string manipulation and regexes; the results from these tests would guide the initial generation of .clang-format files. Here are some ideas:
- In the given files, look at the indentation. If it's consistently done with tabs, then you can initialize the relevant config options to use tabs and not mutate it. Likewise with spaces. If it's not consistent, make a guess based on the fractions.
- If indentation is done with spaces, count the number of spaces. Are they mostly in multiples of 2, 4, or something else? That's your clue.
- Do braces
{}appear alone (plus whitespace) on lines? If so, you're likely not going to use Egyptian-style braces. - Does the keyword
namespaceappear starting at any column other than zero? If so,namespaceindentation is probably the standard.
Thanks for those suggestions. Yes, they could potentially make the search run a lot quicker.
A few comments:
- Firstly, as a workaround to get the speed benefits you mention, provide an initial .clang-format file to unformat and it will use it as the starting point for subsequent search. You can populate it with the desired settings, e.g. 4 spaces for an indent. You can even kill unformat, edit the config file that it generated and re-run. Then, unformat will pick up those changes and take things from there.
- While the counting of spaces is relatively straight-forward, it's just one of many settings which need to be tuned. Typically, it's one of the ones that is found the quickest. Other things such as
{on new lines can vary in different circumstances (e.g. control block vs function body vs class body vs initializer list) and that's where things start to get a little more complicated. - This project is still in an early stage and I'm not as far as optimizing these kinds of things. For a more mature library which does search in a more straight-forward manner, see whatstyle.