microsoft / microsoft/vscode-cpptools

[vcFormat] `space.withinInitializerListBraces`/`space.withinSquareBrackets` are not heeded inside array initialization

Open
#14,412 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Feature: Code Formatting Language Service Visual Studio
Dominant language
TypeScript
Stars
6.2k
Forks
1.7k
Avg merge
14h 46m
Merged PRs (30d)
61

Description

Environment
  • OS and Version: Various (assuming all)
  • VS Code Version: 1.117.0
  • C/C++ Extension Version: 1.31.5
Bug Summary and Steps to Reproduce

Bug Summary:

When the setting C_Cpp.vcFormat.space.withinInitializerListBraces is set to true, vcFormat fails to add (and will actually remove) a space from between the opening brace and the first designated initializer of all struct members when part of the initialization of an array of structs.

Steps to reproduce:

  1. Ensure C_Cpp.vcFormat.space.withinInitializerListBraces is set to true
  2. Add the following lines to a .c file:
typedef struct {
    int member_a;
    int member_b;
} MyStruct_t;

MyStruct_t array_of_structs[] = {
    { .member_a = 0, .member_b = 1 },
    { .member_a = 2, .member_b = 3 },
    { .member_a = 4, .member_b = 5 },
};
  1. Format the file using vcFormat

Expected behavior:

  • The file should not change

Actual result:

typedef struct {
    int member_a;
    int member_b;
} MyStruct_t;

MyStruct_t array_of_structs[] = {
    {.member_a = 0, .member_b = 1 },
    {.member_a = 2, .member_b = 3 },
    {.member_a = 4, .member_b = 5 },
};

The same problem also appears to afflict the first designated initializer of an array with spaces enabled inside square brackets. Here are steps to reproduce:

  1. Ensure C_Cpp.vcFormat.space.withinSquareBrackets is set to true
  2. Add the following lines to a .c file:
int my_array[3] = {
    [ 0 ] = 100,
    [ 1 ] = 101,
    [ 2 ] = 102,
};
  1. Format the file using vcFormat

Expected behavior:

  • The file should not change

Actual result:

int my_array[3] = {
    [0] = 100,
    [ 1 ] = 101,
    [ 2 ] = 102,
};
Configuration and Logs
Configurations vary; the pertinent configuration options are mentioned above.

Logs will be provided if deemed necessary. I believe this should be very easily reproduced.
Other Extensions

No response

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue in a .c file with vcFormat and the relevant space.withinInitializerListBraces or space.withinSquareBrackets setting enabled. Trace how vcFormat handles the first designated initializer inside array initialization, then verify that formatting preserves the spaces shown in both expected examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
56/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.