Clang-format use formatted file name in include regex

28 Views Asked by At

Consider the following includes in the file Main.c

#include "Main.h"
#include "Main_something.h"
#include "Main_anotherthing.h"
#include "Main_randomString.h"
#include "Something_FromAnotherLib.h"

I want to make clang-format consider everything that starts with Main (the formatted file name) as a main header and assign it a category of 0.

clang-format offers the opposite of what I need. It allows you to specify a bunch of source file suffixes which also count as main files. I.e. Main_myThing.c is considered to be named Main.c for the purpose of includes sorting. I need to be able to specify a bunch of header name suffixes which are to be considered "main". I.e. Main_mything.h == Main.h for the purposes of the include sorting.

Is it possible to achieve this?

1

There are 1 best solutions below

0
mr NAE On

Unfortunately, you can't use any "predefined magic values" in this regexp Take a look at https://clang.llvm.org/doxygen/HeaderIncludes_8cpp_source.html

CategoryRegexs.emplace_back(Category.Regex
...
if (CategoryRegexs[i].match(IncludeName))

But, if you want, you can do something like this. Create template .clang-format.tmpl file,

IncludeCategories
  - Regex: '${placeholder}.*'

and propagate it by script in each subfolder .clang-format with replacing ${placeholder} with Main_ or $0/ or any other.