@@ -16,6 +16,19 @@ import (
1616 "github.com/xonecas/symb/internal/treesitter"
1717 )
1818
19+const (
20+ // windowThreshold is the minimum line count to trigger windowed output.
21+ windowThreshold = 50
22+ // windowContext is the number of lines shown above/below the edit region.
23+ windowContext = 20
24+)
25+
26+// editRegion describes which lines (1-indexed, inclusive) were affected in the new file.
27+type editRegion struct {
28+ start int // first affected line
29+ end int // last affected line
30+}
31+
1932 // EditArgs represents arguments for the Edit tool.
2033 // Exactly one of the operation fields (Replace, Insert, Delete) must be set.
2134 type EditArgs struct {
@@ -128,7 +141,7 @@ func (h *EditHandler) SetTSIndex(idx *treesitter.Index) { h.tsIndex = idx }
128141 func (h *EditHandler) Handle(ctx context.Context, arguments json.RawMessage) (*mcp.ToolResult, error) {
129142 var args EditArgs
130143 if err := json.Unmarshal(arguments, &args); err != nil {
131- return toolError("Invalid arguments: %v", err), nil
144+ return toolError("%s", editUnmarshalHint(arguments, err)), nil
132145 }
133146 if args.File == "" {
134147 return toolError("File path cannot be empty"), nil
@@ -174,6 +187,28 @@ func validateEditOps(args EditArgs) error {
174187 return nil
175188 }
176189
190+// editUnmarshalHint returns an actionable error when JSON unmarshalling fails.
191+// It detects common mistakes like passing a string for create instead of an object.
192+func editUnmarshalHint(raw json.RawMessage, original error) string {
193+ var probe map[string]json.RawMessage
194+ if json.Unmarshal(raw, &probe) == nil {
195+ for _, key := range []string{"create", "replace", "insert", "delete"} {
196+ v, ok := probe[key]
197+ if !ok || len(v) == 0 {
198+ continue
199+ }
200+ // If the value starts with '"' it's a string, but the schema expects an object.
201+ if v[0] == '"' {
202+ return fmt.Sprintf(
203+ `invalid %s: expected an object, got a string. Use {"file":"path", "%s":{"content":"..."}}`,
204+ key, key,
205+ )
206+ }
207+ }
208+ }
209+ return fmt.Sprintf("invalid arguments: %v", original)
210+}
211+
177212 // applyEdit reads the file, applies the edit operation, writes it back, and returns fresh hashes.
178213 func (h *EditHandler) applyEdit(ctx context.Context, absPath string, args EditArgs) (*mcp.ToolResult, error) {
179214 content, err := os.ReadFile(absPath)
@@ -183,13 +218,14 @@ func (h *EditHandler) applyEdit(ctx context.Context, absPath string, args EditAr
183218 lines := strings.Split(string(content), "\n")
184219
185220 var result string
221+ var region editRegion
186222 switch {
187223 case args.Replace != nil:
188- result, err = applyReplace(lines, args.Replace)
224+ result, region, err = applyReplace(lines, args.Replace)
189225 case args.Insert != nil:
190- result, err = applyInsert(lines, args.Insert)
226+ result, region, err = applyInsert(lines, args.Insert)
191227 case args.Delete != nil:
192- result, err = applyDelete(lines, args.Delete)
228+ result, region, err = applyDelete(lines, args.Delete)
193229 }
194230 if err != nil {
195231 return toolError("%v", err), nil
@@ -204,7 +240,7 @@ func (h *EditHandler) applyEdit(ctx context.Context, absPath string, args EditAr
204240 }
205241
206242 tagged := hashline.TagLines(result, 1)
207- text := fmt.Sprintf("Edited %s (%d lines):\n\n%s", args.File, len(tagged), hashline.FormatTagged(tagged))
243+ text := formatEditResponse(args.File, tagged, region)
208244
209245 if h.lspManager != nil {
210246 diags := h.lspManager.NotifyAndWait(ctx, absPath, 5*time.Second)
@@ -258,40 +294,81 @@ func (h *EditHandler) handleCreate(ctx context.Context, absPath, displayPath str
258294 }, nil
259295 }
260296
261-func applyReplace(lines []string, op *ReplaceOp) (string, error) {
297+// formatEditResponse builds the response text, using windowed output for large files.
298+func formatEditResponse(displayPath string, tagged []hashline.TaggedLine, region editRegion) string {
299+ total := len(tagged)
300+ if total <= windowThreshold {
301+ return fmt.Sprintf("Edited %s (%d lines):\n\n%s", displayPath, total, hashline.FormatTagged(tagged))
302+ }
303+
304+ // Clamp window bounds.
305+ winStart := region.start - windowContext
306+ if winStart < 1 {
307+ winStart = 1
308+ }
309+ winEnd := region.end + windowContext
310+ if winEnd > total {
311+ winEnd = total
312+ }
313+
314+ window := tagged[winStart-1 : winEnd] // tagged is 0-indexed, line nums are 1-indexed
315+ return fmt.Sprintf("Edited %s (%d lines, showing %d–%d):\n\n%s",
316+ displayPath, total, winStart, winEnd, hashline.FormatTagged(window))
317+}
318+
319+func applyReplace(lines []string, op *ReplaceOp) (string, editRegion, error) {
262320 if err := hashline.ValidateRange(op.Start, op.End, lines); err != nil {
263- return "", fmt.Errorf("replace: %w", err)
321+ return "", editRegion{}, fmt.Errorf("replace: %w", err)
264322 }
265323
324+ inserted := strings.Split(op.Content, "\n")
266325 newLines := make([]string, 0, len(lines))
267326 newLines = append(newLines, lines[:op.Start.Num-1]...)
268- newLines = append(newLines, strings.Split(op.Content, "\n")...)
327+ newLines = append(newLines, inserted...)
269328 newLines = append(newLines, lines[op.End.Num:]...)
270329
271- return strings.Join(newLines, "\n"), nil
330+ region := editRegion{
331+ start: op.Start.Num,
332+ end: op.Start.Num + len(inserted) - 1,
333+ }
334+ return strings.Join(newLines, "\n"), region, nil
272335 }
273336
274-func applyInsert(lines []string, op *InsertOp) (string, error) {
337+func applyInsert(lines []string, op *InsertOp) (string, editRegion, error) {
275338 if err := op.After.Validate(lines); err != nil {
276- return "", fmt.Errorf("insert: after anchor: %w", err)
339+ return "", editRegion{}, fmt.Errorf("insert: after anchor: %w", err)
277340 }
278341
279- newLines := make([]string, 0, len(lines)+1)
342+ inserted := strings.Split(op.Content, "\n")
343+ newLines := make([]string, 0, len(lines)+len(inserted))
280344 newLines = append(newLines, lines[:op.After.Num]...)
281- newLines = append(newLines, strings.Split(op.Content, "\n")...)
345+ newLines = append(newLines, inserted...)
282346 newLines = append(newLines, lines[op.After.Num:]...)
283347
284- return strings.Join(newLines, "\n"), nil
348+ region := editRegion{
349+ start: op.After.Num + 1,
350+ end: op.After.Num + len(inserted),
351+ }
352+ return strings.Join(newLines, "\n"), region, nil
285353 }
286354
287-func applyDelete(lines []string, op *DeleteOp) (string, error) {
355+func applyDelete(lines []string, op *DeleteOp) (string, editRegion, error) {
288356 if err := hashline.ValidateRange(op.Start, op.End, lines); err != nil {
289- return "", fmt.Errorf("delete: %w", err)
357+ return "", editRegion{}, fmt.Errorf("delete: %w", err)
290358 }
291359
292360 newLines := make([]string, 0, len(lines))
293361 newLines = append(newLines, lines[:op.Start.Num-1]...)
294362 newLines = append(newLines, lines[op.End.Num:]...)
295363
296- return strings.Join(newLines, "\n"), nil
364+ // The "region" is where the deletion happened — show surrounding context.
365+ regionLine := op.Start.Num - 1
366+ if regionLine < 1 {
367+ regionLine = 1
368+ }
369+ region := editRegion{
370+ start: regionLine,
371+ end: regionLine,
372+ }
373+ return strings.Join(newLines, "\n"), region, nil
297374 }