edit: a comment at the start of a model group's line stays when the group is taken out (#1566, ggbdpq)

What changed: delItem read the "is the element alone on its line" check
from the comment-blanked copy, so a JSONC comment written before the
element on the same line (VS Code's chatLanguageModels.json carries the
user's notes beside their groups) counted as blank space, the delete took
the line from its start, and the comment went with the element. It reads
raw now, as delJSONBytes does for the same check on an object member, so
only the element (and its comma, or its line) goes.

Test: TestDelJSONItemKeepsACommentBeforeTheElement (edit.DelJSONItem,
three shapes) fails on the old code with "the comment before the element
went with it" and passes now; the edit package's other tests, among them
the CRLF and among-others deletes, pass unchanged.

Run: go test -tags nogui -count=1 ./internal/edit ./internal/agent; the
full go test -tags nogui ./... in a sandbox HOME (TestOffline red as on
the baseline; gateway's TestSearchFollowsTheSearchersProxy red once under
the full run, passes alone, no path from this diff); gofmt -l; go vet and
go build on darwin, GOOS=linux -tags nogui and GOOS=windows.

Review: the test fails on main (3f5702421) with "the comment before the
element went with it". Added a fourth shape, a block comment that ends on
the element's line: on the old code DelJSONItem cut from the line start
into the comment and refused the result as "invalid JSON/JSONC", so the
group could not be taken out at all; now it goes and the comment stays.
Re-run: go test ./internal/edit, green with the fix and red with
stripped[ls:a] put back by hand.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
ggbdpq
2026-10-11 12:00:41 +08:00
committed by yetone
co-authored by factory-droid[bot]
parent 8a881dc203
commit dca04af14f
2 changed files with 51 additions and 1 deletions
+4 -1
View File
@@ -186,7 +186,10 @@ func findItem(stripped []byte, where map[string]string) (gjson.Result, bool) {
func delItem(raw, stripped []byte, r gjson.Result) []byte {
a, b := r.Index, r.Index+len(r.Raw)
ls := bytes.LastIndexByte(raw[:a], '\n') + 1
alone := len(bytes.TrimSpace(stripped[ls:a])) == 0
// a comment before the element on its line is not alone: read from raw,
// where the comment is still there (stripped has it blanked), as
// delJSONBytes does, so the comment stays when the element goes.
alone := len(bytes.TrimSpace(raw[ls:a])) == 0
j := b
for j < len(stripped) && isSpace(stripped[j]) {
j++