Menu

#38 Class name tab completion adding an space after package name

open
nobody
5
2007-10-01
2007-10-01
No

I'm using Ubuntu linux and JLine 0.9.91 with the Class name tab completer and when trying to tab complete a class within the JDK everything works fine. However, when attempting to tab complete a class within the JAR file that my test class is running from, each time I hit the tab key I get an extra space after the package name.

it looks like this:

Enter bean: com.texturemedia.<sp>

I then have to hit backspace and tab again to continue the completion.

I took a quick look into the class name completion class and couldn't see anything obvious. I'll keep looking, but is this a known issue or something specific to my environment?

Also, here is my simple test class:

package com.texturemedia;

public class Interrogator {
public static void main(String[] args) throws IOException {
ConsoleReader reader = new ConsoleReader();
reader.addCompletor(new ClassNameCompletor());
String bean = reader.readLine("Enter bean: ");
System.out.println("Bean is " + bean);
}
}

I was actually just trying to tab complete this class when I found the issue. This class is packaged into a JAR file and is the only class in that JAR file.

Discussion

  • Brian Pontarelli

    Logged In: YES
    user_id=1541332
    Originator: YES

    It looks like this only happens when there is a single class within a JAR file. Still not sure if it is the ClassNameCompletor or the ConsoleReader. My initial guess is the ClassNameCompletor.

     
  • Brian Pontarelli

    Logged In: YES
    user_id=1541332
    Originator: YES

    I figured this out. It looks like the SimpleCompletor has two bugs in it. I fixed these locally and it works great. The first issue is that if multiple matches exist, but the prefix of all of them up to the delimiter is the same, the candidates still contains multiple values, each of them are identical. This causes the CompletionHandler to print out candidates, even though there really is only one. The second part is that if there is only one candidate, but you haven't gotten to it yet (i.e. because it stops on delimiters, you might need to hit tab a few times to get all the way to the completion), it incorrectly appends a space to the end.

    Here is my fixed method:

    public int complete(final String buffer, final int cursor, final List clist) {
    String start = (buffer == null) ? "" : buffer;

    SortedSet matches = candidates.tailSet(start);

    Set results = new HashSet();
    boolean trimmed = false;
    for (Iterator i = matches.iterator(); i.hasNext();) {
    String can = (String) i.next();

    if (!(can.startsWith(start))) {
    break;
    }

    if (delimiter != null) {
    int index = can.indexOf(delimiter, cursor);

    if (index != -1) {
    can = can.substring(0, index + 1);
    trimmed = true;
    }
    }

    results.add(can);
    }

    clist.addAll(results);
    if (clist.size() == 1 && !trimmed) {
    clist.set(0, (clist.get(0)) + " ");
    }

    // the index of the completion is always from the beginning of
    // the buffer.
    return (clist.size() == 0) ? (-1) : 0;
    }

     
  • Charles Oliver Nutter

    Can't reproduce this with JRuby's jirb completion logic, which seems to complete fine. I suspect the problem is not in jline for this case...

     

Log in to post a comment.