MCPcopy Create free account
hub / github.com/codehamr/codehamr / insertChip

Method insertChip

internal/tui/prompt.go:256–283  ·  view source on GitHub ↗

insertChip splices a chip label in at the cursor, recording the new span at the right ORDER position so the following reconcile() walks the labels left-to-right correctly. reconcile re-derives every span's start/end from the updated value, so the offsets on the inserted literal are placeholders it o

(content string)

Source from the content-addressed store, hash-verified

254// updated value, so the offsets on the inserted literal are placeholders it
255// overwrites: only the insertion index matters here.
256func (p *promptInput) insertChip(content string) {
257 lines := countLines(content)
258 id := p.nextID
259 p.nextID++
260 p.store[id] = chipContent{content: content, lines: lines}
261
262 label := chipLabel(lines)
263 labelLen := utf8.RuneCountInString(label)
264 // Snap out of any chip the cursor is parked inside before choosing the
265 // insertion point, so a paste can't splice a new label into the interior of
266 // an existing one: reconcile would then fail to re-find the broken label
267 // and silently drop a chip, sending the wrong (or no) paste to the LLM.
268 insertAt := p.snapCursorOutOfChip()
269
270 insertIdx := 0
271 for i, s := range p.spans {
272 if s.start < insertAt {
273 insertIdx = i + 1
274 } else {
275 break
276 }
277 }
278 p.spans = slices.Insert(p.spans, insertIdx,
279 chipSpan{id: id, start: insertAt, end: insertAt + labelLen})
280
281 p.ta.InsertString(label)
282 p.reconcile()
283}
284
285// deleteSpan removes the chip's label from the value and drops it from spans
286// and store. Cursor lands at the vacated start; reconcile re-validates later

Callers 1

UpdateMethod · 0.95

Calls 4

snapCursorOutOfChipMethod · 0.95
reconcileMethod · 0.95
countLinesFunction · 0.85
chipLabelFunction · 0.85

Tested by

no test coverage detected