Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow easy custom candidate sorting #678

Merged
merged 1 commit into from
Oct 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 35 additions & 3 deletions reader/src/main/java/org/jline/reader/Candidate.java
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