Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export type PromptProps = {
jump: (action: "top" | "bottom" | "high" | "middle" | "low") => void
wordNext: (big: boolean) => boolean
wordPrev: (big: boolean) => boolean
wordEnd: (big: boolean) => boolean
nextParagraph: () => boolean
previousParagraph: () => boolean
text: () => string
Expand Down Expand Up @@ -567,6 +568,9 @@ export function Prompt(props: PromptProps) {
copyWordPrev(big) {
return props.copy?.wordPrev(big) ?? false
},
copyWordEnd(big) {
return props.copy?.wordEnd(big) ?? false
},
copyNextParagraph() {
return props.copy?.nextParagraph() ?? false
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export function createVimHandler(input: {
copyJump?: (action: VimJump) => void
copyWordNext?: (big: boolean) => boolean
copyWordPrev?: (big: boolean) => boolean
copyWordEnd?: (big: boolean) => boolean
copyNextParagraph?: () => boolean
copyPreviousParagraph?: () => boolean
copyText?: () => string
Expand Down Expand Up @@ -1157,6 +1158,10 @@ export function createVimHandler(input: {
}

if (key === "e" && !event.shift) {
if (input.copyWordEnd?.(false)) {
event.preventDefault()
return true
}
const text = input.copyText?.() ?? ""
copyMotion(wordEnd(text, pos, false))
event.preventDefault()
Expand Down Expand Up @@ -1187,6 +1192,10 @@ export function createVimHandler(input: {
}

if (isShifted(event, "e")) {
if (input.copyWordEnd?.(true)) {
event.preventDefault()
return true
}
const text = input.copyText?.() ?? ""
copyMotion(wordEnd(text, pos, true))
event.preventDefault()
Expand Down
30 changes: 27 additions & 3 deletions packages/opencode/src/cli/cmd/tui/component/vim/vim-motions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ function wordClass(char: string, big: boolean): "blank" | "word" | "punct" {
return "punct"
}

function wordRunEnd(text: string, offset: number, big: boolean) {
const target = wordClass(text[offset], big)
let pos = offset
while (pos + 1 < text.length && wordClass(text[pos + 1], big) === target) pos++
return pos
}

export function wordEnd(text: string, offset: number, big: boolean) {
if (text.length === 0) return 0
let pos = offset
Expand All @@ -331,9 +338,7 @@ export function wordEnd(text: string, offset: number, big: boolean) {
if (pos >= text.length) return text.length - 1
}

const target = wordClass(text[pos], big)
while (pos + 1 < text.length && wordClass(text[pos + 1], big) === target) pos++
return pos
return wordRunEnd(text, pos, big)
}

function deleteOffsets(textarea: TextareaRenderable, startOffset: number, endOffset: number) {
Expand Down Expand Up @@ -453,6 +458,25 @@ export function copyWordPrev(rows: VimCopyRow[], get: (idx: number) => string, i
return { idx, col: min }
}

export function copyWordEnd(rows: VimCopyRow[], get: (idx: number) => string, idx: number, col: number, big: boolean) {
const row = rows[idx]
if (!row) return { idx, col }
const min = row.col
const text = get(idx)
const pos = Math.max(0, col - min)
const end = wordEnd(text, pos, big)
if (end > pos && wordClass(text[end], big) !== "blank") return { idx, col: min + end }
for (let i = idx + 1; i < rows.length; i++) {
const nextRow = rows[i]
if (!nextRow) continue
const nextText = get(i)
const start = nextText.split("").findIndex((char) => wordClass(char, big) !== "blank")
if (start === -1) continue
return { idx: i, col: nextRow.col + wordRunEnd(nextText, start, big) }
}
return { idx, col: min + Math.max(0, text.length - 1) }
}

export type CopyParagraphResult = { index: number; atEnd: boolean }

// `atEnd` is true only when content runs to EOF without a trailing blank line,
Expand Down
14 changes: 14 additions & 0 deletions packages/opencode/src/cli/cmd/tui/routes/session/copy-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Part } from "@opencode-ai/sdk/v2"
import {
copyNextParagraph,
copyPreviousParagraph,
copyWordEnd,
copyWordNext,
copyWordPrev,
firstNonWhitespace,
Expand Down Expand Up @@ -390,6 +391,18 @@ export function createCopyMode(input: {
return true
}

function wordEnd(big: boolean) {
const s = state()
if (!s.active) return false
const list = rows()
if (!list.length) return false
const next = copyWordEnd(list, (idx) => rowText(list[idx]!), s.idx, s.col, big)
if (next.idx === s.idx && next.col === s.col) return false
if (next.idx !== s.idx) sync(next.idx)
setCol(next.col)
return true
}

function paragraphColumn(
row: CopyRow,
atEnd: boolean,
Expand Down Expand Up @@ -669,6 +682,7 @@ export function createCopyMode(input: {
jump,
wordNext,
wordPrev,
wordEnd,
nextParagraph,
previousParagraph,
text: copyText,
Expand Down
127 changes: 127 additions & 0 deletions packages/opencode/test/cli/tui/vim-motions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { VimJump } from "../../../src/cli/cmd/tui/component/vim/vim-motion-
import {
copyNextParagraph,
copyPreviousParagraph,
copyWordEnd,
copyWordNext,
copyWordPrev,
deleteSelection,
Expand Down Expand Up @@ -331,6 +332,14 @@ function createHandler(
setCopyCol(prev.col)
return moved
},
copyWordEnd(big) {
if (!copyRows) return false
const next = copyWordEnd(copyRows, (idx) => options?.copy?.texts?.[idx] ?? "", copyIdx(), copyCol(), big)
const moved = next.idx !== copyIdx() || next.col !== copyCol()
setCopyIdx(next.idx)
setCopyCol(next.col)
return moved
},
copyNextParagraph() {
if (!copyRows) return false
const next = copyNextParagraph(copyRows, (idx) => options?.copy?.texts?.[idx] ?? "", copyIdx())
Expand Down Expand Up @@ -4280,6 +4289,124 @@ describe("copy mode", () => {
expect(ctx.copyCol()).toBe(6)
})

test("e advances to next copy row like vim", () => {
const ctx = createHandler("abc", {
mode: "copy",
copy: {
idx: 0,
col: 4,
rows: [{ col: 0 }, { col: 0 }],
texts: ["alpha", "beta gamma"],
},
})

const evt = createEvent("e")
expect(ctx.handler.handleKey(evt.event)).toBe(true)
expect(evt.prevented()).toBe(true)
expect(ctx.copyIdx()).toBe(1)
expect(ctx.copyCol()).toBe(3)
})

test("e lands on single-char word on next copy row", () => {
const ctx = createHandler("abc", {
mode: "copy",
copy: {
idx: 0,
col: 4,
rows: [{ col: 0 }, { col: 0 }],
texts: ["alpha", "a beta"],
},
})

ctx.handler.handleKey(createEvent("e").event)
expect(ctx.copyIdx()).toBe(1)
expect(ctx.copyCol()).toBe(0)
})

test("E advances to next copy row with big word", () => {
const ctx = createHandler("abc", {
mode: "copy",
copy: {
idx: 0,
col: 4,
rows: [{ col: 0 }, { col: 0 }],
texts: ["alpha", "foo,bar baz"],
},
})

const evt = createEvent("E")
expect(ctx.handler.handleKey(evt.event)).toBe(true)
expect(evt.prevented()).toBe(true)
expect(ctx.copyIdx()).toBe(1)
expect(ctx.copyCol()).toBe(6)
})

test("e skips whitespace-only current copy row", () => {
const ctx = createHandler("abc", {
mode: "copy",
copy: {
idx: 0,
col: 0,
rows: [{ col: 0 }, { col: 0 }],
texts: [" ", "beta"],
},
})

ctx.handler.handleKey(createEvent("e").event)
expect(ctx.copyIdx()).toBe(1)
expect(ctx.copyCol()).toBe(3)
})

test("e skips blank copy rows", () => {
const ctx = createHandler("abc", {
mode: "copy",
copy: {
idx: 0,
col: 4,
rows: [{ col: 0 }, { col: 0 }, { col: 0 }],
texts: ["alpha", " ", " beta"],
},
})

ctx.handler.handleKey(createEvent("e").event)
expect(ctx.copyIdx()).toBe(2)
expect(ctx.copyCol()).toBe(5)
})

test("e respects copy row column offsets", () => {
const ctx = createHandler("abc", {
mode: "copy",
copy: {
idx: 0,
col: 14,
rows: [{ col: 10 }, { col: 20 }],
texts: ["alpha", " beta"],
},
})

ctx.handler.handleKey(createEvent("e").event)
expect(ctx.copyIdx()).toBe(1)
expect(ctx.copyCol()).toBe(24)
})

test("e at final copy word end stays put", () => {
const ctx = createHandler("abc", {
mode: "copy",
copy: {
idx: 1,
col: 3,
rows: [{ col: 0 }, { col: 0 }],
texts: ["alpha", "beta"],
},
})

const evt = createEvent("e")
expect(ctx.handler.handleKey(evt.event)).toBe(true)
expect(evt.prevented()).toBe(true)
expect(ctx.copyIdx()).toBe(1)
expect(ctx.copyCol()).toBe(3)
})

test("B retreats to previous copy row with big word", () => {
const ctx = createHandler("abc", {
mode: "copy",
Expand Down
Loading