From dca04af14fba73128bf5b4925e8fae9dc1dc37b7 Mon Sep 17 00:00:00 2001 From: ggbdpq Date: Sun, 11 Oct 2026 06:52:27 +0800 Subject: [PATCH] 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> --- internal/edit/jsonarray.go | 5 ++- internal/edit/jsonarray_comment_test.go | 47 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 internal/edit/jsonarray_comment_test.go diff --git a/internal/edit/jsonarray.go b/internal/edit/jsonarray.go index 2d1ee8ba3..45e54f43e 100644 --- a/internal/edit/jsonarray.go +++ b/internal/edit/jsonarray.go @@ -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++ diff --git a/internal/edit/jsonarray_comment_test.go b/internal/edit/jsonarray_comment_test.go new file mode 100644 index 000000000..6a30aca6a --- /dev/null +++ b/internal/edit/jsonarray_comment_test.go @@ -0,0 +1,47 @@ +package edit + +import ( + "encoding/json" + "strings" + "testing" + + "github.com/tidwall/jsonc" +) + +// A comment at the start of the element's line stays when the element is +// taken out, as a member's delete keeps the comment before it: VS Code's +// chatLanguageModels.json carries the user's notes beside their groups. + +func TestDelJSONItemKeepsACommentBeforeTheElement(t *testing.T) { + where := map[string]string{"vendor": "customendpoint", "name": "magpie"} + magpie := `{"name": "magpie", "vendor": "customendpoint"}` + for _, in := range []string{ + // last element, its comment on the line with it + "[\n {\"name\": \"a\", \"vendor\": \"openai\"},\n /* magpie's group */ " + magpie + "\n]\n", + // not the last + "[\n /* magpie's group */ " + magpie + ",\n {\"name\": \"b\", \"vendor\": \"ollama\"}\n]\n", + // on one line, the [ on it too + "[ {\"name\": \"a\", \"vendor\": \"openai\"}, /* magpie's group */ " + magpie + " ]\n", + // a block comment that ends on the element's line + "[\n {\"name\": \"a\", \"vendor\": \"openai\"},\n /* magpie's group\n */ " + magpie + "\n]\n", + } { + p := tmpFile(t, "chatLanguageModels.json", in) + if err := DelJSONItem(p, where); err != nil { + t.Fatal(err) + } + b := read(t, p) + var got []map[string]any + if err := json.Unmarshal(jsonc.ToJSON([]byte(b)), &got); err != nil { + t.Fatalf("%q: not valid JSONC after: %v\n%s", in, err, b) + } + if len(got) != strings.Count(in, "\"vendor\"")-1 { + t.Fatalf("%q: %d elements left:\n%s", in, len(got), b) + } + if _, ok := GetJSONItem(p, where); ok { + t.Fatalf("%q: the element is still there:\n%s", in, b) + } + if !strings.Contains(b, "/* magpie's group") || !strings.Contains(b, "*/") { + t.Fatalf("%q: the comment before the element went with it:\n%s", in, b) + } + } +}