Skip to content

Commit

Permalink
Allow easy custom candidate sorting (#678)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw429s committed Oct 14, 2021
1 parent b9ca72d commit 8843bbe
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions reader/src/main/java/org/jline/reader/Candidate.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ public class Candidate implements Comparable<Candidate> {
private final String suffix;
private final String key;
private final boolean complete;
private final int sort;

/**
* Simple constructor with only a single String as an argument.
*
* @param value the candidate
*/
public Candidate(String value) {
this(value, value, null, null, null, null, true);
this(value, value, null, null, null, null, true, 0);
}

/**
Expand All @@ -44,15 +45,32 @@ public Candidate(String value) {
* @param suffix the suffix
* @param key the key
* @param complete the complete flag
* @param sort the sort flag
*/
public Candidate(String value, String displ, String group, String descr, String suffix, String key, boolean complete) {
public Candidate(String value, String displ, String group, String descr, String suffix, String key, boolean complete, int sort) {
this.value = Objects.requireNonNull(value);
this.displ = Objects.requireNonNull(displ);
this.group = group;
this.descr = descr;
this.suffix = suffix;
this.key = key;
this.complete = complete;
this.sort = sort;
}

/**
* Constructs a new Candidate.
*
* @param value the value
* @param displ the display string
* @param group the group
* @param descr the description
* @param suffix the suffix
* @param key the key
* @param complete the complete flag
*/
public Candidate(String value, String displ, String group, String descr, String suffix, String key, boolean complete) {
this(value, displ, group, descr, suffix, key, complete, 0);
}

/**
Expand Down Expand Up @@ -133,9 +151,23 @@ public boolean complete() {
return complete;
}

/**
* Integer used to override default sort logic.
* @return the sort int
*/
public int sort() {
return sort;
}


@Override
public int compareTo(Candidate o) {
return value.compareTo(o.value);
// If both candidates have same sort, use default behavior
if( sort == o.sort() ) {
return value.compareTo(o.value);
} else {
return sort - o.sort();
}
}

@Override
Expand Down

0 comments on commit 8843bbe

Please sign in to comment.