Array pointing to the start indexes of all the lines.
()
| 271 | |
| 272 | // Array pointing to the start indexes of all the lines. |
| 273 | func (d *Document) lineStartIndexes() []int { |
| 274 | // TODO: Cache, because this is often reused. |
| 275 | // (If it is used, it's often used many times. |
| 276 | // And this has to be fast for editing big documents!) |
| 277 | lc := d.LineCount() |
| 278 | lengths := make([]int, lc) |
| 279 | for i, l := range d.Lines() { |
| 280 | lengths[i] = len(l) |
| 281 | } |
| 282 | |
| 283 | // Calculate cumulative sums. |
| 284 | indexes := make([]int, lc+1) |
| 285 | indexes[0] = 0 // https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/prompt_toolkit/document.py#L189 |
| 286 | pos := 0 |
| 287 | for i, l := range lengths { |
| 288 | pos += l + 1 |
| 289 | indexes[i+1] = pos |
| 290 | } |
| 291 | if lc > 1 { |
| 292 | // Pop the last item. (This is not a new line.) |
| 293 | indexes = indexes[:lc] |
| 294 | } |
| 295 | return indexes |
| 296 | } |
| 297 | |
| 298 | // For the index of a character at a certain line, calculate the index of |
| 299 | // the first character on that line. |
no test coverage detected