microsoft / microsoft/vscode-cpptools

[vcFormat] Extra spaces around `=` are removed if `space.aroundAssignmentOperator` is set to `insert`

Open
#14,413 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Feature Request 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 C_Cpp.vcFormat.space.aroundAssignmentOperator is set to insert, vcFormat will remove all extra spaces around an assignment operator leaving exactly one space on each side. However, there many times where extra spaces are warranted for the sake of columnar alignment to improve readability.

Therefore, I believe there are two possible solutions to this problem:

  1. Change the behavior of insert so that it only modifies the file by adding a space if no space is present on a side of the assignment operator
  2. Remove the insert and remove options and replace them with the following options (retaining the ignore option):
    • none: Formatter will remove spaces from both sides of the assignment operator
    • one: Formatter will add or remove spaces from both sides of the assignment operator so that the result is one space on each side of the assignment operator
    • oneOrMore: Formatter will only add one space to a side of the assignment operator if zero spaces exist on that side. If one or more spaces exist, the formatter will not make any changes.

Steps to reproduce:

  1. Ensure C_Cpp.vcFormat.space.aroundAssignmentOperator is set to insert
  2. Add the following lines to a .c file:
enum {
    short_name          = 0x00,
    longer_name         = 0x01,
    even_longer_name    = 0x02,
    another_longer_name = 0x04,
    shorter_name        = 0x08,
};
  1. Format the file using vcFormat

Expected behavior:

  • The file should not change

Actual output:

enum {
    short_name = 0x00,
    longer_name = 0x01,
    even_longer_name = 0x02,
    another_longer_name = 0x04,
    shorter_name = 0x08,
};
Configuration and Logs
N/A
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 locating the vcFormat implementation and tests for C_Cpp.vcFormat.space.aroundAssignmentOperator. Reproduce the enum example in a .c file with the setting set to insert, then verify the formatter preserves existing alignment spaces while still handling sides with no space.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.