2020
2121import java .util .Collections ;
2222import java .util .List ;
23- import jline .console .completer .Completer ;
23+ import org .jline .reader .Candidate ;
24+ import org .jline .reader .Completer ;
25+ import org .jline .reader .LineReader ;
26+ import org .jline .reader .ParsedLine ;
27+ import org .jline .utils .AttributedString ;
2428
2529class JLineZNodeCompleter implements Completer {
2630
27- private ZooKeeper zk ;
31+ private final ZooKeeper zk ;
2832
2933 public JLineZNodeCompleter (ZooKeeper zk ) {
3034 this .zk = zk ;
3135 }
3236
33- @ SuppressWarnings ({ "unchecked" , "rawtypes" })
34- public int complete (String buffer , int cursor , List candidates ) {
37+ @ Override
38+ public void complete (LineReader lineReader , ParsedLine commandLine , List < Candidate > candidates ) {
3539 // Guarantee that the final token is the one we're expanding
36- buffer = buffer .substring (0 , cursor );
37- String token = "" ;
38- if (!buffer .endsWith (" " )) {
39- String [] tokens = buffer .split (" " );
40- if (tokens .length != 0 ) {
41- token = tokens [tokens .length - 1 ];
42- }
43- }
40+ String token = commandLine .words ().get (commandLine .words ().size () - 1 );
4441
4542 if (token .startsWith ("/" )) {
46- return completeZNode (buffer , token , candidates );
43+ completeZNode (token , candidates );
44+ } else {
45+ completeCommand (token , candidates );
4746 }
48- return completeCommand (buffer , token , candidates );
4947 }
5048
51- private int completeCommand (String buffer , String token , List <String > candidates ) {
49+ private void completeCommand (String token , List <Candidate > candidates ) {
5250 for (String cmd : ZooKeeperMain .getCommands ()) {
5351 if (cmd .startsWith (token )) {
54- candidates .add (cmd );
52+ candidates .add (createCandidate ( cmd ) );
5553 }
5654 }
57- return buffer .lastIndexOf (" " ) + 1 ;
5855 }
5956
60- private int completeZNode (String buffer , String token , List <String > candidates ) {
57+ private void completeZNode (String token , List <Candidate > candidates ) {
6158 String path = token ;
6259 int idx = path .lastIndexOf ("/" ) + 1 ;
6360 String prefix = path .substring (idx );
@@ -67,16 +64,17 @@ private int completeZNode(String buffer, String token, List<String> candidates)
6764 List <String > children = zk .getChildren (dir , false );
6865 for (String child : children ) {
6966 if (child .startsWith (prefix )) {
70- candidates .add (child );
67+ String zNode = dir + (idx == 1 ? "" : "/" ) + child ;
68+ candidates .add (createCandidate (zNode ));
7169 }
7270 }
73- } catch (InterruptedException e ) {
74- return 0 ;
75- } catch (KeeperException e ) {
76- return 0 ;
71+ } catch (InterruptedException | KeeperException e ) {
72+ return ;
7773 }
7874 Collections .sort (candidates );
79- return candidates .size () == 0 ? buffer .length () : buffer .lastIndexOf ("/" ) + 1 ;
8075 }
8176
77+ private static Candidate createCandidate (String cmd ) {
78+ return new Candidate (AttributedString .stripAnsi (cmd ), cmd , null , null , null , null , true );
79+ }
8280}
0 commit comments