MCPcopy Create free account
hub / github.com/c-bata/go-prompt / TranslateRowColToIndex

Method TranslateRowColToIndex

document.go:396–425  ·  view source on GitHub ↗

TranslateRowColToIndex given a (row, col), return the corresponding index. (Row and col params are 0-based.)

(row int, column int)

Source from the content-addressed store, hash-verified

394// TranslateRowColToIndex given a (row, col), return the corresponding index.
395// (Row and col params are 0-based.)
396func (d *Document) TranslateRowColToIndex(row int, column int) (index int) {
397 indexes := d.lineStartIndexes()
398 if row < 0 {
399 row = 0
400 } else if row > len(indexes) {
401 row = len(indexes) - 1
402 }
403 index = indexes[row]
404 line := d.Lines()[row]
405
406 // python) result += max(0, min(col, len(line)))
407 if column > 0 || len(line) > 0 {
408 if column > len(line) {
409 index += len(line)
410 } else {
411 index += column
412 }
413 }
414
415 // Keep in range. (len(self.text) is included, because the cursor can be
416 // right after the end of the text as well.)
417 // python) result = max(0, min(result, len(self.text)))
418 if index > len(d.Text) {
419 index = len(d.Text)
420 }
421 if index < 0 {
422 index = 0
423 }
424 return index
425}
426
427// OnLastLine returns true when we are at the last line.
428func (d *Document) OnLastLine() bool {

Callers 3

GetCursorUpPositionMethod · 0.95
GetCursorDownPositionMethod · 0.95

Calls 2

lineStartIndexesMethod · 0.95
LinesMethod · 0.95

Tested by 1