【问题标题】:QtCreator: unify QtCreatorCodeStyle with clang-formatQtCreator:将 QtCreatorCodeStyle 与 clang-format 统一起来
【发布时间】:2017-03-09 11:46:56
【问题描述】:

我想知道是否有一种简单的方法可以将qt creator codestyle(xml文件)转换为clang-format文件?

我的 c++ 代码有以下配置,从 qtcreator 生成:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorCodeStyle>
<!-- Written by QtCreator 4.2.1, 2017-03-09T12:29:05. -->
<qtcreator>
 <data>
  <variable>CodeStyleData</variable>
  <valuemap type="QVariantMap">
   <value type="bool" key="AlignAssignments">false</value>
   <value type="bool" key="AutoSpacesForTabs">false</value>
   <value type="bool" key="BindStarToIdentifier">false</value>
   <value type="bool" key="BindStarToLeftSpecifier">true</value>
   <value type="bool" key="BindStarToRightSpecifier">false</value>
   <value type="bool" key="BindStarToTypeName">true</value>
   <value type="bool" key="ExtraPaddingForConditionsIfConfusingAlign">true</value>
   <value type="bool" key="IndentAccessSpecifiers">false</value>
   <value type="bool" key="IndentBlockBody">true</value>
   <value type="bool" key="IndentBlockBraces">false</value>
   <value type="bool" key="IndentBlocksRelativeToSwitchLabels">true</value>
   <value type="bool" key="IndentClassBraces">false</value>
   <value type="bool" key="IndentControlFlowRelativeToSwitchLabels">true</value>
   <value type="bool" key="IndentDeclarationsRelativeToAccessSpecifiers">true</value>
   <value type="bool" key="IndentEnumBraces">false</value>
   <value type="bool" key="IndentFunctionBody">true</value>
   <value type="bool" key="IndentFunctionBraces">false</value>
   <value type="bool" key="IndentNamespaceBody">false</value>
   <value type="bool" key="IndentNamespaceBraces">false</value>
   <value type="int" key="IndentSize">2</value>
   <value type="bool" key="IndentStatementsRelativeToSwitchLabels">true</value>
   <value type="bool" key="IndentSwitchLabels">true</value>
   <value type="int" key="PaddingMode">2</value>
   <value type="bool" key="ShortGetterName">true</value>
   <value type="bool" key="SpacesForTabs">false</value>
   <value type="int" key="TabSize">2</value>
  </valuemap>
 </data>
 <data>
  <variable>DisplayName</variable>
  <value type="QString">Google</value>
 </data>
</qtcreator>

但我找不到大多数这些变量的 clang 格式等效项。 我在美化器插件中的 clang 格式文件相似但不完全相同,这导致我的 git diff 每次都很混乱。

【问题讨论】:

    标签: c++ qt-creator clang-format


    【解决方案1】:

    我浏览了 QtCreator 设置并提出了以下一般映射。这些设置按照它们在Qt Creator 4.10.2代码样式对话框中出现的顺序列出,这有助于识别有时神秘的名称。

    标记为 N/A 的设置是 clang-format 似乎不支持的东西。 (根据文档clang-format 有意仅支持一小组固定样式,而不是通用格式化程序。)

    我没有尝试匹配特定的设置值或更复杂的组合,但这个列表应该作为找出它的起点。

    QtCreator                                     Clang-format
    ---------                                     ------------
    
    [GENERAL]
    SpacesForTabs                                 UseTab
    TabSize                                       TabWidth
    IndentSize                                    IndentWidth
    PaddingMode                                   UseTab
    
    [CONTENT]
    IndentAccessSpecifiers                        AccessModifierOffset
    IndentDeclarationsRelativeToAccessSpecifiers  N/A
    IndentFunctionBody                            N/A
    IndentBlockBody                               N/A
    IndentNamespaceBody                           NamespaceIndentation
    
    [BRACES]
    # Qt's implied style matches Clang BreakBeforeBraces=Allman
    # Qt's IndentX options match Clang's BreakBeforeBraces=GNU
    # to customize GNU style, see:
    IndentClassBraces       BraceWrapping:AfterClass:IndentBraces
    IndentNamespaceBraces   BraceWrapping:AfterNamespace:IndentBraces
    IndentEnumBraces        BraceWrapping:AfterEnum:IndentBraces
    IndentFunctionBraces    BraceWrapping:AfterFunction:IndentBraces
    IndentBlockBraces       N/A
    
    [SWITCH]
    IndentSwitchLabels                            IndentCaseLabels
    IndentStatementsRelativeToSwitchLabels        N/A
    IndentBlocksRelativeToSwitchLabels            N/A
    IndentControlFlowRelativeToSwitchLabels       N/A
    
    [ALIGNMENT]
    AlignAssignments                              AlignOperands
    ExtraPaddingForConditionsIfConfusingAlign     N/A
    
    [POINTERS AND REFERENCES]
    BindStarToIdentifier                          PointerAlignment
    BindStarToTypeName                            PointerAlignment
    BindStarToLeftSpecifier                       PointerAlignment
    BindStarToRightSpecifier                      PointerAlignment
    
    [?]
    AutoSpacesForTabs                             N/A (editor setting)
    

    【讨论】:

      【解决方案2】:

      您可以从已安装的格式中获取示例配置,例如llvm:

      clang-format -style=llvm -dump-config > .clang-format
      

      所以你需要“value”元素的文本和key属性。 格式主要是。 :

      使用 python 和 lxml:

      with open(xml_path) as xml_file: 
      xml_content = xml_file.read()
      tree = etree.parse(xml_path)
      elements = tree.findall("value")
      fileBuffer =""
      for e in elements:
          filebuffer += e.attr["key"] + ": " + e.text + "\n"
      

      未测试

      【讨论】:

        猜你喜欢
        • 2020-03-18
        • 2020-09-24
        • 2021-07-08
        • 1970-01-01
        • 1970-01-01
        • 2021-01-19
        • 1970-01-01
        • 2022-11-14
        相关资源
        最近更新 更多