Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mark a region so clang-format won't touch it?

For example, in the MySQL++ library there are macros that can be used to define simple structs based on sql table definition, like this:

sql_create_6(stock, 1, 6,
    mysqlpp::sql_char, item,
    mysqlpp::sql_bigint, num,
    mysqlpp::sql_double, weight,
    mysqlpp::sql_decimal, price,
    mysqlpp::sql_date, sdate,
    mysqlpp::Null<mysqlpp::sql_mediumtext>, description)

The problem is that clang-format will reformat this in a way that is much more difficult to read (every param on a new line). Most code formatters can recognize special format-off / format-on comments, but I haven't found anything like that in the clang-format manual.

like image 421
Alex Avatar asked Jul 26 '14 19:07

Alex


People also ask

How do I turn off clang format?

Disabling Formatting on a Piece of Code The code between a comment // clang-format off or /* clang-format off */ up to a comment // clang-format on or /* clang-format on */ will not be formatted.

How do you customize your clang format?

clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the . clang-format or _clang-format file in the project directory.

Can clang format break code?

Short answer: YES. The clang-format tool has a -sort-includes option. Changing the order of #include directives can definitely change the behavior of existing code, and may break existing code.

How do I disable clang formatting in Visual Studio?

Tools > Options configuration By default, Visual Studio will use any existing ClangFormat file in your source tree for formatting operations. To turn this off, you can uncheck Enable ClangFormat support.

How to use Clang-format to extend a line or region?

With this integration you can press the bound key and clang-format will format the current line in NORMAL and INSERT mode or the selected region in VISUAL mode. The line or region is extended to the next bigger syntactic entity. It operates on the current, potentially unsaved buffer and does not create or save any files.

How to override the default Clang format in Linux?

To do so, you can override the defaults by writing another .clang-format file in a subfolder. The tool itself has already been included in the repositories of popular Linux distributions for a long time. Search for clang-format in your repositories.

What is Clang format in C++?

clang-format. clang-format is a tool to format C/C++/… code according to a set of rules and heuristics. Like most tools, it is not perfect nor covers every single case, but it is good enough to be helpful. clang-format can be used for several purposes: Quickly reformat a block of code to the kernel style.

How to use Clang-format in Visual Studio Code?

Change “C-K” to another binding if you need clang-format on a different key (C-K stands for Ctrl+k). With this integration you can press the bound key and clang-format will format the current line in NORMAL and INSERT mode or the selected region in VISUAL mode. The line or region is extended to the next bigger syntactic entity.


2 Answers

In newer version, you can surround a section of code with:

// clang-format off ... // clang-format on 
like image 55
djasper Avatar answered Sep 20 '22 05:09

djasper


Try adding a // comment marker after each line, this may do it. I had the same issue in Eclipse and learned this trick.

sql_create_6(stock, 1, 6, //     mysqlpp::sql_char, item, //     mysqlpp::sql_bigint, num, //     mysqlpp::sql_double, weight, //     mysqlpp::sql_decimal, price, //     mysqlpp::sql_date, sdate, //     mysqlpp::Null<mysqlpp::sql_mediumtext>, description) 
like image 41
vsoftco Avatar answered Sep 20 '22 05:09

vsoftco