gersemi icon indicating copy to clipboard operation
gersemi copied to clipboard

Formatting nested groups

Open Jacobfaib opened this issue 1 month ago • 1 comments

What is the best way to tell gersemi how to format nested argument groups?

We have several forwarding cmake functions which, after a particular keyword, forward all remaining arguments to another cmake function. We would like to have those remaining arguments also be nicely indented. Something like so:

forwarding_function(
  # regularly defined args...
  FORWARD_REMAINING # special keyword to signify remaining args are forwarded
    AN_OPTION_ARG
    A_SINGLE_VALUE_ARG ON
    A_MULTI_VALUE_ARG
      MV_ONE
      MV_TWO
      ...     
)

Currently we approximate this by providing a stub like so:

function(forwarding_function)
  set(options ...)
  set(one_value_args ...)
  set(multi_value_args ... FORWARD_REMAINING)

  cmake_parse_arguments(...)
endfunction()

To at least have gersemi understand that FORWARD_REMAINING is "list-like". But this results in the forwarded args just being formatted like any other list:

forwarding_function(
  # regularly defined args...
  FORWARD_REMAINING
    AN_OPTION_ARG
    A_SINGLE_VALUE_ARG
    ON
    A_MULTI_VALUE_ARG
    MV_ONE
    MV_TWO
    ...     
)

Jacobfaib avatar Dec 05 '25 15:12 Jacobfaib

This is as far as you can go with stubs. You'd have to implement extension such as this one:

# acme_corporation.py

command_definitions = {
    "forwarding_function": {
        # other keywords if you need
        "multi_value_keywords": ["FORWARD_REMAINING"],
        "sections": {
            "FORWARD_REMAINING": {
                "options": ["AN_OPTION"],
                "one_value_keywords": ["A_SINGLE_VALUE_ARG"],
                "multi_value_keywords": ["A_MULTI_VALUE_ARG"],
            }
        }
    }
}

You'd use it with either --extensions ./acme_corporation.py or equivalent entry in .gersemirc. If this forwarding function happen to share keywords with a builtin you might be able to clone that definition and tweak it. See extension-example in this repository for more details.

BlankSpruce avatar Dec 05 '25 15:12 BlankSpruce