问题标签 [clang-format]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
5 回答
23734 浏览

c++ - Clang 格式的换行符

我正在寻找一种clang-format设置来防止该工具删除换行符。

例如,我将我的ColumnLimit设置为 120,这就是我重新格式化一些示例代码时发生的情况。

前:

后:

我想要的是该工具会中断超过 120 个字符的行,但不会仅仅因为它们少于 120 个字符而决定合并行。

有这样的选择吗?文档中的任何内容对我来说都没有什么突出的。

0 投票
1 回答
1211 浏览

c++ - 如何在 Clang-Format 之前插入换行符:类/结构中的运算符

我正在尝试使用 clang-format 3.7 格式化一些代码。

我有一个看起来有点像这样的结构:

这很好,但 clang-format 坚持将其格式化为:

我更愿意在下一行使用 and 并且更喜欢在同一行使用大括号,但我可以没有它Thing1Thing2

这是我当前的.clang-format文件,有什么办法可以改变吗?

0 投票
2 回答
3193 浏览

git - Git 'pre-receive' hook and 'git-clang-format' script to reliably reject pushes that violate code style conventions

Let's immediately start with a scrap of the pre-receive hook that I've already written:

NOTE:
Places with irrelevant code are stubbed with # ....

NOTE:
If you are not familiar with git-clang-format, take a look here.

That hook works as expected, and so far, I didn't notice any bugs, but if you spot any problem or have an improvement suggestion, I'd appreciate any report. Probably, I should give a comment on what's the intention behind this hook. Well, it does check every pushed revision for compliance with code style conventions using git-clang-format, and if any of them does not comply, it will output the relevant diff (the one telling developers what should be fixed) for each of them. Basically, I have two in-depth questions regarding this hook.

First, notice that I perform copy of the remote's (server) bare repository to some temporary directory and check out the code for analysis there. Let me explain the intention of this. Note that I do several git checkouts and git resets (due to for loop) in order to analyze all of the pushed revisions individually with git-clang-format. What I am trying to avoid here, is the (possible) concurrency issue on push access to the remote's (server) bare repository. That is, I'm under impression that if multiple developers will try to push at the same time to a remote with this pre-receive hook installed, that might cause problems if each of these push "sessions" does not do git checkouts and git resets with its private copy of the repository. So, to put it simple, does git-daemon have built-in lock management for concurrent push "sessions"? Will it execute the corresponding pre-receive hook instances strictly sequentially or there is a possibility of interleaving (which can potentially cause undefined behavior)? Something tells me that there should be a built-in solution for this problem with concrete guarantees, otherwise how would remotes work in general (even without complex hooks) being subjected to concurrent pushes? If there is such a built-in solution, then the copy is redundant and simply reusing the bare repository would actually speed up the processing. By the way, any reference to official documentation regarding this question is very welcome.

Second, git-clang-format processes only staged (but not committed) changes vs. specific commit (HEAD by default). Thus, you can easily see where a corner case lies. Yes, it's with the root commits (revisions). In fact, git reset --soft 'HEAD~1' cannot be applied to root commits as they have no parents to reset to. Hence, the following check with my second question is there:

I've tried git update-ref -d 'HEAD' but this breaks the repository in such a way that git-clang-format is not able to process it anymore. I believe this is related to the fact that all of these pushed revisions that are being analyzed (including this root one) do not really belong to any branch yet. That is, they are in detached HEAD state. It would be perfect to find a solution to this corner case as well, so that initial commits can also undergo the same check by git-clang-format for compliance with code style conventions.

Peace.

0 投票
1 回答
591 浏览

c++ - 有没有办法阻止clang-format在返回后放置空间?

如果我有这样的事情:

并通过 clang-format 传递它,它总是在返回和打开之间添加一个空格(像这样:

我已经将 SpaceBeforeParens 设置为 Never 但它仍然添加了这个空间。有人知道控制这种情况的方法吗?

0 投票
1 回答
1691 浏览

c++ - 为 isocpp 的核心指南配置 clang-format

我想尽可能地遵循CppCoreGuidelines,但我在配置 clang-format 时遇到了一些问题。

如何配置 clang-format 以符合 isocpp 核心准则?

例如,以下指南给我带来了一些麻烦。

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rl-ptr

clang-format 似乎总是选择T &operator[](size_t);,我无法找到正确的设置。

0 投票
1 回答
448 浏览

c++ - 使用 clang-format 3.6 的 cpp 模板<> 间距未按预期工作

我正在使用clang-format version 3.6 on Fedora 21内部vim使用clang-format.pyforcpp代码。除了模板定义之外,代码看起来与预期的一样。的代码template<typename T> 正在更改为template <typename T>看起来相对接近的唯一选项, ObjCSpaceBeforeProtocolList我将其设置为 false,但它仍然在单词模板和尖括号之间放置空格。有谁知道如何防止在单词模板和尖括号之间插入空格?

0 投票
1 回答
735 浏览

clang - 如何使用美化器以这种方式缩进宏?

我希望我的预处理器以这种方式缩进:

我已经尝试过clang-format,但是它在#指令之后删除了所有空格,并且我找不到任何控制该行为的选项。那么,clang-format 可以以这种方式执行预处理器缩进吗?

0 投票
4 回答
28229 浏览

c++ - 在 CLion 中使用 clang 格式

我想在 CLion 中使用 clang-format 格式化我的代码。我已经完成了设置,但没有找到任何允许我运行外部命令来转换代码的设置。在 CLion 中这些事情通常是如何完成的?

0 投票
0 回答
323 浏览

c - 使用 Clang 格式的连续参数之间没有空格

我必须使用的 C 风格指南强制在函数调用中函数参数之间没有空格(或逗号后没有空格),例如:

如何使用 clang-format 实现这一点?文档中描述的与间距相关的选项似乎都不适用于此。

0 投票
1 回答
1852 浏览

clang-format - clang-format 在 if() while() for() 之后在语句周围添加 {}

我想知道是否可以设置 clang-format 来减少compound_statement{ non-compound_statement; }in iteration_statement

例子

输入:

输出:

然后美化器会根据需要缩进。