Skip to content

jsonEdit: preserve comments above a property when it is deleted#309171

Open
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/settings-reset-preserves-comments
Open

jsonEdit: preserve comments above a property when it is deleted#309171
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/settings-reset-preserves-comments

Conversation

@yogeshwaran-c
Copy link
Copy Markdown
Contributor

When removeProperty (called e.g. when a setting is reset to its default value via the Settings UI or keyboard shortcuts) removed a property from settings.json, it also silently deleted any comment placed on the line immediately above the property.

Root cause

The deletion range was anchored to surrounding structure rather than to the property's own line:

Case removeBegin (before fix) Problem
First / only property parent.offset + 1 (just after {) Includes all comment lines between { and the property
Non-first property previous.offset + previous.length (trailing comma of the previous sibling) Includes all comment lines between the previous property and the current one

Fix

Anchor the removal to the property's own line (using the newline that precedes the line as the boundary):

  • First/only property (propertyIndex === 0): removeBegin is set to the start of the property's line (including its preceding newline) instead of parent.offset + 1.
  • Non-first property (propertyIndex > 0) with a comment between the previous property and this one: two separate edits are returned — one that removes only the trailing comma from the previous sibling, and one that removes the property's line (including its own trailing comma for middle properties). The intermediate content (comments) is left intact.
  • When no content separates the comma from the property (same-line or comment-free formatting), the original single-range approach is preserved — no behavioural change for the common case.

Tests

Three new test cases in jsonEdit.test.ts:

  1. Only property with a preceding comment.
  2. Last property (non-first) with a preceding comment.
  3. Middle property with a preceding comment — surrounding properties remain valid JSON.

Fixes #275792

When removeProperty (used e.g. to reset a setting to its default value)
removed the first/only property or a property that was not the first in
the object, it calculated a removal range that started before the property
itself — often including comment lines that appeared immediately above it.

The root causes:
- propertyIndex === 0: removeBegin was set to parent.offset + 1 (just after
  the opening brace), which includes any comments between '{' and the
  property.
- propertyIndex > 0: removeBegin was set to previous.offset + previous.length
  (the trailing comma of the previous property), and removeEnd to the end of
  the current property. Any comment between the previous property and the
  current one fell inside this range.

Fix: anchor the removal to the property's own line rather than to the
surrounding structure. When content (comments) is detected between the
previous anchor point and the start of the property's line, use two
separate edits — one to remove the trailing comma and one to remove the
property line — so the intermediate content is preserved. Update the
test suite with cases that cover both the first-property and non-first-
property scenarios.

Fixes microsoft#275792
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resetting various settings can delete comments from settings.json file

2 participants