【问题标题】:How can i configure clang format without .clang-format file in every workspace?如何在每个工作区中配置没有 .clang-format 文件的 clang 格式?
【发布时间】:2017-03-15 18:38:21
【问题描述】:

我想配置 clang-format 而不必将我的 .clang-format 文件复制到每个新工作区。

在我的settings.json 我目前有

"C_Cpp.clang_format_style": "{BasedOnStyle: Google, IndentWidth: 4, IndentCaseLabels: false, TabWidth: 4, UseTab: ForIndentation, ColumnLimit: 0}",
"C_Cpp.clang_format_fallbackStyle": "Google"

C_Cpp.clang_format_style 的描述是

编码风格,目前支持:Visual Studio、LLVM、Google、Chromium、Mozilla、WebKit。使用“文件”从当前或父目录中的 .clang 格式文件加载样式。使用“{key: value, ...}”设置具体参数,例如:“{ BasedOnStyle: LLVM, IndentWidth: 8 }”

这让我认为我的方法会奏效,但事实并非如此。当我使用自动格式时,它总是使用 Google fallbackStyle。

这是不可能的还是我在这里做错了什么?

【问题讨论】:

  • 为了让 clang-format 运行,我大致做了以下事情(在我的脑海中,希望不会太错误):我安装并激活了 (!) xaver 的 Clang-Format 扩展,然后配置就像"clang-format.style": "{BasedOnStyle: LLVM, IndentWidth: 2, Standard: Cpp11}"
  • 格式化程序将在文件系统中上升,直到它在某个祖先目录中找到.clag-format_clang.format。您可以将该文件放在 $HOME 目录中,每个项目都会使用它。

标签: visual-studio-code vscode-settings clang-format


【解决方案1】:

我刚遇到同样的问题,发现你的方法使用(我只改变了格式)

"C_Cpp.clang_format_style": "{BasedOnStyle: Google, 
IndentWidth: 4, 
IndentCaseLabels: false, 
TabWidth: 4, UseTab: ForIndentation, 
ColumnLimit: 0}",

确实有效。

【讨论】:

【解决方案2】:

clang-format -style=file 的文档有些误导。

您需要在settings.json(工作区或全局)中定义以下内容

{
   "C_Cpp.clang_format_style": "file",
}

因为 file 不是您想要的文件的路径,而是告诉 clang-format 使用它可以在本地路径中找到的 .clang-format 文件 - 一直向上(cd .. 直到找到一个) .

如果是 VSCode C++ 扩展自己的clang-format,它位于 VSCode 扩展文件夹中。

这应该使clang-format 转到cd .. 直到它在您的代码文件夹中找到.clang-format`(如果存在的话)。

【讨论】:

    猜你喜欢
    • 2020-07-27
    • 2013-09-11
    • 2020-04-11
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 2017-10-30
    相关资源
    最近更新 更多