| 149 | } |
| 150 | |
| 151 | class TreeSitterParsedCommand implements IParsedCommand { |
| 152 | readonly originalCommand: string |
| 153 | // Tree-sitter's startIndex/endIndex are UTF-8 byte offsets, but JS |
| 154 | // String.slice() uses UTF-16 code-unit indices. For ASCII they coincide; |
| 155 | // for multi-byte code points (e.g. `—` U+2014: 3 UTF-8 bytes, 1 code unit) |
| 156 | // they diverge and slicing the string directly lands mid-token. Slicing |
| 157 | // the UTF-8 Buffer with tree-sitter's byte offsets and decoding back to |
| 158 | // string is correct regardless of code-point width. |
| 159 | private readonly commandBytes: Buffer |
| 160 | private readonly pipePositions: number[] |
| 161 | private readonly redirectionNodes: RedirectionNode[] |
| 162 | private readonly treeSitterAnalysis: TreeSitterAnalysis |
| 163 | |
| 164 | constructor( |
| 165 | command: string, |
| 166 | pipePositions: number[], |
| 167 | redirectionNodes: RedirectionNode[], |
| 168 | treeSitterAnalysis: TreeSitterAnalysis, |
| 169 | ) { |
| 170 | this.originalCommand = command |
| 171 | this.commandBytes = Buffer.from(command, 'utf8') |
| 172 | this.pipePositions = pipePositions |
| 173 | this.redirectionNodes = redirectionNodes |
| 174 | this.treeSitterAnalysis = treeSitterAnalysis |
| 175 | } |
| 176 | |
| 177 | toString(): string { |
| 178 | return this.originalCommand |
| 179 | } |
| 180 | |
| 181 | getPipeSegments(): string[] { |
| 182 | if (this.pipePositions.length === 0) { |
| 183 | return [this.originalCommand] |
| 184 | } |
| 185 | |
| 186 | const segments: string[] = [] |
| 187 | let currentStart = 0 |
| 188 | |
| 189 | for (const pipePos of this.pipePositions) { |
| 190 | const segment = this.commandBytes |
| 191 | .subarray(currentStart, pipePos) |
| 192 | .toString('utf8') |
| 193 | .trim() |
| 194 | if (segment) { |
| 195 | segments.push(segment) |
| 196 | } |
| 197 | currentStart = pipePos + 1 |
| 198 | } |
| 199 | |
| 200 | const lastSegment = this.commandBytes |
| 201 | .subarray(currentStart) |
| 202 | .toString('utf8') |
| 203 | .trim() |
| 204 | if (lastSegment) { |
| 205 | segments.push(lastSegment) |
| 206 | } |
| 207 | |
| 208 | return segments |
nothing calls this directly
no outgoing calls
no test coverage detected