MCPcopy Index your code
hub / github.com/claude-code-best/claude-code / executeLineOp

Function executeLineOp

src/vim/operators.ts:102–166  ·  view source on GitHub ↗
(
  op: Operator,
  count: number,
  ctx: OperatorContext,
)

Source from the content-addressed store, hash-verified

100 * Execute a line operation (dd, cc, yy).
101 */
102export function executeLineOp(
103 op: Operator,
104 count: number,
105 ctx: OperatorContext,
106): void {
107 const text = ctx.text
108 const lines = text.split('\n')
109 // Calculate logical line by counting newlines before cursor offset
110 // (cursor.getPosition() returns wrapped line which is wrong for this)
111 const currentLine = countCharInString(text.slice(0, ctx.cursor.offset), '\n')
112 const linesToAffect = Math.min(count, lines.length - currentLine)
113 const lineStart = ctx.cursor.startOfLogicalLine().offset
114 let lineEnd = lineStart
115 for (let i = 0; i < linesToAffect; i++) {
116 const nextNewline = text.indexOf('\n', lineEnd)
117 lineEnd = nextNewline === -1 ? text.length : nextNewline + 1
118 }
119
120 let content = text.slice(lineStart, lineEnd)
121 // Ensure linewise content ends with newline for paste detection
122 if (!content.endsWith('\n')) {
123 content = content + '\n'
124 }
125 ctx.setRegister(content, true)
126
127 if (op === 'yank') {
128 ctx.setOffset(lineStart)
129 } else if (op === 'delete') {
130 let deleteStart = lineStart
131 const deleteEnd = lineEnd
132
133 // If deleting to end of file and there's a preceding newline, include it
134 // This ensures deleting the last line doesn't leave a trailing newline
135 if (
136 deleteEnd === text.length &&
137 deleteStart > 0 &&
138 text[deleteStart - 1] === '\n'
139 ) {
140 deleteStart -= 1
141 }
142
143 const newText = text.slice(0, deleteStart) + text.slice(deleteEnd)
144 ctx.setText(newText || '')
145 const maxOff = Math.max(
146 0,
147 newText.length - (lastGrapheme(newText).length || 1),
148 )
149 ctx.setOffset(Math.min(deleteStart, maxOff))
150 } else if (op === 'change') {
151 // For single line, just clear it
152 if (lines.length === 1) {
153 ctx.setText('')
154 ctx.enterInsert(0)
155 } else {
156 // Delete all affected lines, replace with single empty line, enter insert
157 const beforeLines = lines.slice(0, currentLine)
158 const afterLines = lines.slice(currentLine + linesToAffect)
159 const newText = [...beforeLines, '', ...afterLines].join('\n')

Callers 2

handleNormalInputFunction · 0.85
fromOperatorFunction · 0.85

Calls 4

countCharInStringFunction · 0.85
startOfLogicalLineMethod · 0.80
maxMethod · 0.80
lastGraphemeFunction · 0.50

Tested by

no test coverage detected