【问题标题】:How to align function declaration using uncrustify?如何使用 uncrustify 对齐函数声明?
【发布时间】:2020-07-16 11:39:07
【问题描述】:

我正在尝试使用 uncrustify 复制 GNU 编码标准。我的程序有以下函数声明,

static void connect_to_server_cb1 (GObject      *source_object,
                                   GAsyncResult *result,
                                   gpointer      user_data);

static gboolean connect_to_server_cb2 (GObject     *source_object,
                                   GAsyncResult *result,
                                   gpointer      user_data);

static void connect_to_server_cb3 (GObject      *source_object,
                                   GAsyncResult *result,
                                   gpointer      user_data);

我期望输出如下,

static void     connect_to_server_cb1 (GObject      *source_object,
                                       GAsyncResult *result,
                                       gpointer      user_data);

static gboolean connect_to_server_cb2 (GObject      *source_object,
                                       GAsyncResult *result,
                                       gpointer      user_data);

static void     connect_to_server_cb3 (GObject      *source_object,
                                       GAsyncResult *result,
                                       gpointer      user_data);

我应该尝试哪个配置选项来实现这一点?

【问题讨论】:

    标签: uncrustify


    【解决方案1】:

    我不确定 uncrustify 能否完全满足您的需求。您可以尝试使用来自GNOME C Utilsgcu-lineup-parameters 程序对输出进行后处理,例如,Nautilus 已经使用了该程序。 Nautilus 源代码树包含一个run-uncrustify.sh script,它为每个文件执行此操作:

    # Aligning prototypes is not working yet, so avoid headers
    "$UNCRUSTIFY" -c "$DATA/uncrustify.cfg" --no-backup "$FILE"
    "$DATA/lineup-parameters" "$FILE" > "$FILE.temp" && mv "$FILE.temp" "$FILE"
    

    如果您只想处理 .c 文件,这应该可以工作,但评论表明在标头中对齐原型更加困难。

    【讨论】:

      【解决方案2】:
          # Whether to align variable definitions in prototypes and functions.
          align_func_params               = true     # true/false
      
          # How to consider (or treat) the '*' in the alignment of variable definitions.
          #
          # 0: Part of the type     'void *   foo;' (default)
          # 1: Part of the variable 'void     *foo;'
          # 2: Dangling             'void    *foo;'
          # Dangling: the '*' will not be taken into account when aligning.
          align_var_def_star_style        = 2        # unsigned number
      
          # How to consider (or treat) the '&' in the alignment of variable definitions.
          #
          # 0: Part of the type     'long &   foo;' (default)
          # 1: Part of the variable 'long     &foo;'
          # 2: Dangling             'long    &foo;'
          # Dangling: the '&' will not be taken into account when aligning.
          align_var_def_amp_style         = 2        # unsigned number
      
          # The span for aligning function prototypes.
          #
          # 0: Don't align (default).
          align_func_proto_span           = 4        # unsigned number
      

      【讨论】:

      • 嗨,我尝试了上述组合,它部分工作。函数声明中的指针悬在中间,例如 static void * connect_to_server_cb3 (GObject *source_object, GAsyncResult *result, gpointer user_data);
      • 如果您找不到打开工单的方法,请尝试 align_var_def_star_style 的不同选项。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-05
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多