diff --git a/builtins/src/main/java/org/jline/builtins/Widgets.java b/builtins/src/main/java/org/jline/builtins/Widgets.java index 20dcecbc7..b8f73b01c 100644 --- a/builtins/src/main/java/org/jline/builtins/Widgets.java +++ b/builtins/src/main/java/org/jline/builtins/Widgets.java @@ -801,12 +801,21 @@ private boolean doTailTip(String widget) { private void doCommandTailTip(String widget, CmdDesc cmdDesc, List args) { int argnum = 0; + String prevArg = ""; for (String a : args) { if (!a.startsWith("-")) { - argnum++; + if (!prevArg.matches("-[a-zA-Z]{1}") || !cmdDesc.optionWithValue(prevArg)) { + argnum++; + } } + prevArg = a; + } + String lastArg = ""; + prevArg = args.get(args.size() - 1); + if (!prevChar().equals(" ")) { + lastArg = args.get(args.size() - 1); + prevArg = args.get(args.size() - 2); } - String lastArg = !prevChar().equals(" ") ? args.get(args.size() - 1) : ""; int bpsize = argnum; boolean doTailTip = true; boolean noCompleters = false; @@ -814,7 +823,9 @@ private void doCommandTailTip(String widget, CmdDesc cmdDesc, List args) setSuggestionType(SuggestionType.TAIL_TIP); noCompleters = true; if (!lastArg.startsWith("-")) { - bpsize--; + if (!prevArg.matches("-[a-zA-Z]{1}") || !cmdDesc.optionWithValue(prevArg)) { + bpsize--; + } } if (prevChar().equals(" ")) { bpsize++; @@ -824,12 +835,23 @@ private void doCommandTailTip(String widget, CmdDesc cmdDesc, List args) } if (cmdDesc != null) { if (lastArg.startsWith("-")) { - doDescription(cmdDesc.getOptionDescription(lastArg, descriptionSize)); - if (!lastArg.contains("=")) { - setSuggestionType(SuggestionType.TAIL_TIP); - noCompleters = true; + if (lastArg.matches("-[a-zA-Z]{1}[a-zA-Z0-9]+")) { + if (cmdDesc.optionWithValue(lastArg.substring(0,2))) { + doDescription(cmdDesc.getOptionDescription(lastArg.substring(0,2), descriptionSize)); + setTipType(tipType); + } else { + doDescription(cmdDesc.getOptionDescription("-" + lastArg.substring(lastArg.length() - 1), descriptionSize)); + setSuggestionType(SuggestionType.TAIL_TIP); + noCompleters = true; + } } else { - setTipType(tipType); + doDescription(cmdDesc.getOptionDescription(lastArg, descriptionSize)); + if (!lastArg.contains("=")) { + setSuggestionType(SuggestionType.TAIL_TIP); + noCompleters = true; + } else { + setTipType(tipType); + } } } else if (!widget.endsWith(LineReader.BACKWARD_DELETE_CHAR)){ setTipType(tipType); @@ -841,9 +863,13 @@ private void doCommandTailTip(String widget, CmdDesc cmdDesc, List args) } if (bpsize - 1 < params.size()) { if (!lastArg.startsWith("-")) { - List d = params.get(bpsize - 1) - .getDescription(); - if (d.isEmpty()) { + List d = null; + if (!prevArg.matches("-[a-zA-Z]{1}") || !cmdDesc.optionWithValue(prevArg)) { + d = params.get(bpsize - 1).getDescription(); + } else { + d = cmdDesc.getOptionDescription(prevArg, descriptionSize); + } + if (d == null || d.isEmpty()) { d = cmdDesc.getMainDescription(descriptionSize); } doDescription(d); @@ -1202,11 +1228,11 @@ public CmdDesc(List mainDesc, List argsDesc, Map getArgsDesc() { + protected List getArgsDesc() { return argsDesc; } - public List getMainDescription(int descriptionSize) { + protected List getMainDescription(int descriptionSize) { List out = new ArrayList<>(); if (mainDesc == null) { // do nothing @@ -1276,11 +1302,11 @@ public List getMainDescription(int descriptionSize) { return out; } - public List getOptionDescription(String opt, int descriptionSize) { + protected List getOptionDescription(String opt, int descriptionSize) { List out = new ArrayList<>(); if (!opt.startsWith("-")) { return out; - } else if (opt.startsWith("--")) { + } else { int ind = opt.indexOf("="); if (ind > 0) { opt = opt.substring(0, ind); @@ -1374,7 +1400,20 @@ public List getOptionDescription(String opt, int descriptionSi } return out; } - + + protected boolean optionWithValue(String option) { + for (String key: optsDesc.keySet()) { + if (key.matches("(^|.*\\s)" + option + "($|=.*|\\s.*)")) { + if (key.contains("=")) { + return true; + } else { + return false; + } + } + } + return false; + } + private AttributedString optionDescription(String key) { return optsDesc.get(key).size() > 0 ? optsDesc.get(key).get(0) : new AttributedString(""); } diff --git a/builtins/src/test/java/org/jline/example/Example.java b/builtins/src/test/java/org/jline/example/Example.java index 7ef377996..464c93336 100644 --- a/builtins/src/test/java/org/jline/example/Example.java +++ b/builtins/src/test/java/org/jline/example/Example.java @@ -150,7 +150,7 @@ private static Map compileTailTips() { tailTips.put("foo11", new CmdDesc(Arrays.asList( new ArgDesc("param1",Arrays.asList(new AttributedString("Param1 description...") , new AttributedString("line 2: This is a very long line that does exceed the terminal width." - +" The line will be truncated automatically (by Status class) be before printing out.") + +" The line will be truncated automatically (by Status class) before printing out.") , new AttributedString("line 3") , new AttributedString("line 4") , new AttributedString("line 5")