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
Labels
Milestone

Comments

@dprutean
Copy link

@dprutean 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

This comment has been minimized.

Copy link
Contributor

@snuyanzin snuyanzin commented Jul 29, 2019

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

@mattirn mattirn added the question label Sep 3, 2019
@mattirn mattirn added this to the 3.13.3 milestone Jan 4, 2020
@mattirn mattirn closed this in ae74cf7 Jan 4, 2020
@mattirn mattirn added the bug label Jan 4, 2020
mattirn added a commit that referenced this issue Jan 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.