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

Completer sortoing order #419

Closed
dprutean opened this issue Jul 19, 2019 · 1 comment
Closed

Completer sortoing order #419

dprutean opened this issue Jul 19, 2019 · 1 comment

Comments

@dprutean
Copy link

dprutean commented Jul 19, 2019

I am using JLine3 with an SQL command line app. The completion is sorting the candidates in the lenght order. Is it possible to simple sort them alphabetical ? Maybe I am doing something wrong ? Below a sample output and the Class I am using for completion.

image

`public class SQLCompleter implements Completer {

private static final Logger LOGGER = Logger.getLogger( Processor.class );
private boolean enabled = true;

public void setEnabled( boolean enabled ){
    this.enabled = enabled;
}

@Override
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
    if ( enabled ) {
        String buf = line.line();
        String[] keywords = buf.split(" ");
        String lk = keywords.length > 0 ? keywords[keywords.length - 1] : "";
        String keyword = buf.endsWith(" ") ? "" : lk;

        final Connector connector = ConnectionFactory.getFirstConnector();
        if (connector != null) {
            try {
                if (!connector.isLearned()) {
                    connector.learnSchema( connector.getSchemaName(), null, false);
                }
            } catch (Throwable ex) {
                LOGGER.error("Error in Processor", ex );
            }
            for (Schema schema : connector.getSchemes()) {
                validateCandidate(keyword, schema.name, candidates);
                for (Table table : schema.tables) {
                    validateCandidate(keyword, schema + "." + table.name, candidates);
                    validateCandidate(keyword, table.name, candidates);
                }
            }
        }
    }
}


private void validateCandidate( String typedString, String possibleMatch, final List<Candidate> candidates ) {
    if ( typedString == null || possibleMatch.toLowerCase().startsWith(typedString.toLowerCase())) {
       candidates.add ( new Candidate(AttributedString.stripAnsi(possibleMatch), possibleMatch, null, null, null, null, true) );
    }
}

}`

@snuyanzin
Copy link
Contributor

Candidate implements java.lang.Comparable and the default way of sorting is defined in org.jline.reader.Candidate#compareTo.
You need to extends from Candidate and override this method in a way you like and use your extension instead of Candidate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants