Skip to content

Commit

Permalink
Prevent index errors moving through completion menu
Browse files Browse the repository at this point in the history
Also introduces slight change in behaviour:
Left/right and up/down will now only change column and row
respectively.

ie. Moving left from the first column moves to the last column
on the _same_ row, not the one above.
  • Loading branch information
tpoliaw committed Nov 27, 2017
1 parent af91e80 commit b49d954
Showing 1 changed file with 59 additions and 60 deletions.
119 changes: 59 additions & 60 deletions reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4071,80 +4071,79 @@ public void previous() {
update();
}

public void down() {
if (isSet(Option.LIST_ROWS_FIRST)) {
int r = selection / columns;
int c = selection % columns;
if ((r + 1) * columns + c < possible.size()) {
r++;
} else if (c + 1 < columns) {
c++;
r = 0;
} else {
r = 0;
c = 0;
/**
* Move 'step' options along the major axis of the menu.<p>
* ie. if the menu is listing rows first, change row (up/down);
* otherwise move column (left/right)
*
* @param step number of options to move by
*/
private void major(int step) {
int axis = isSet(Option.LIST_ROWS_FIRST) ? columns : lines;
int sel = selection + step * axis;
if (sel < 0) {
int pos = (sel + axis) % axis; // needs +axis as (-1)%x == -1
int remainders = possible.size() % axis;
sel = possible.size() - remainders + pos;
if (sel >= possible.size()) {
sel -= axis;
}
selection = r * columns + c;
update();
} else if (sel >= possible.size()) {
sel = sel % axis;
}
selection = sel;
update();
}

/**
* Move 'step' options along the minor axis of the menu.<p>
* ie. if the menu is listing rows first, move along the row (left/right);
* otherwise move along the column (up/down)
*
* @param step number of options to move by
*/
private void minor(int step) {
int axis = isSet(Option.LIST_ROWS_FIRST) ? columns : lines;
int row = selection % axis;
int options = possible.size();
if (selection - row + axis > options) {
// selection is the last row/column
// so there are fewer options than other rows
axis = options%axis;
}
selection = selection - row + ((axis + row + step) % axis);
update();
}

public void up() {
if (isSet(Option.LIST_ROWS_FIRST)) {
major(-1);
} else {
next();
minor(-1);
}
}
public void left() {

public void down() {
if (isSet(Option.LIST_ROWS_FIRST)) {
previous();
major(1);
} else {
int c = selection / lines;
int r = selection % lines;
if (c - 1 >= 0) {
c--;
} else {
c = columns - 1;
r--;
}
selection = c * lines + r;
if (selection < 0) {
selection = possible.size() - 1;
}
update();
minor(1);
}
}
public void right() {

public void left() {
if (isSet(Option.LIST_ROWS_FIRST)) {
next();
minor(-1);
} else {
int c = selection / lines;
int r = selection % lines;
if (c + 1 < columns) {
c++;
} else {
c = 0;
r++;
}
selection = c * lines + r;
if (selection >= possible.size()) {
selection = 0;
}
update();
major(-1);
}
}
public void up() {

public void right() {
if (isSet(Option.LIST_ROWS_FIRST)) {
int r = selection / columns;
int c = selection % columns;
if (r > 0) {
r--;
} else {
c = (c + columns - 1) % columns;
r = lines - 1;
if (r * columns + c >= possible.size()) {
r--;
}
}
selection = r * columns + c;
update();
minor(1);
} else {
previous();
major(1);
}
}

Expand Down

0 comments on commit b49d954

Please sign in to comment.