Skip to content

Commit

Permalink
TailTipWidgets: improved option description
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Oct 15, 2019
1 parent 226e89e commit eeff0fb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 27 deletions.
78 changes: 56 additions & 22 deletions builtins/src/main/java/org/jline/builtins/Widgets.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
package org.jline.builtins;

import static org.jline.keymap.KeyMap.range;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -68,6 +70,7 @@ public void callWidget(String name) {
}

public void executeWidget(String name) {
// this should be executed inside readLine()!!!
widget(name).apply();
}

Expand Down Expand Up @@ -702,9 +705,7 @@ public boolean tailtipInsert() {
private boolean doTailTip(String widget) {
Buffer buffer = buffer();
callWidget(widget);
if (buffer.length() == buffer.cursor()
&& ((!widget.endsWith(LineReader.BACKWARD_DELETE_CHAR) && (prevChar().equals(" ") || prevChar().equals("=") || prevChar().equals("-"))) ||
(widget.endsWith(LineReader.BACKWARD_DELETE_CHAR) && !prevChar().equals(" ")))) {
if (buffer.length() == buffer.cursor()) {
List<String> bp = args(buffer.toString());
int argnum = 0;
for (String a: bp) {
Expand All @@ -713,28 +714,48 @@ private boolean doTailTip(String widget) {
}
}
String lastArg = !prevChar().equals(" ") ? bp.get(bp.size() - 1) : "";
int bpsize = argnum + (!lastArg.startsWith("-") && widget.endsWith(LineReader.BACKWARD_DELETE_CHAR) ? -1 : 0);
int bpsize = argnum;
boolean doTailTip = true;
if (widget.endsWith(LineReader.BACKWARD_DELETE_CHAR)) {
if (!lastArg.startsWith("-")) {
bpsize--;
}
if (prevChar().equals(" ")) {
bpsize++;
}
} else if (!prevChar().equals(" ")) {
doTailTip = false;
}
List<AttributedString> desc = new ArrayList<>();
if (bpsize > 0 && tailTips.containsKey(bp.get(0))) {
List<ArgDesc> params = tailTips.get(bp.get(0)).getArgsDesc();
setSuggestionType(tipType == TipType.COMPLETER ? SuggestionType.COMPLETER : SuggestionType.TAIL_TIP);
if (bpsize - 1 < params.size()) {
if (lastArg.startsWith("-")) {
desc = tailTips.get(bp.get(0)).getOptionDescription(lastArg);
} else {
desc = params.get(bpsize - 1).getDescription();
}
StringBuilder tip = new StringBuilder();
for (int i = bpsize - 1; i < params.size(); i++) {
tip.append(params.get(i).getName());
tip.append(" ");
boolean clearTip = false;
if (tailTips.containsKey(bp.get(0))) {
if (lastArg.startsWith("-")) {
desc = tailTips.get(bp.get(0)).getOptionDescription(lastArg);
}
if (bpsize > 0 && doTailTip) {
List<ArgDesc> params = tailTips.get(bp.get(0)).getArgsDesc();
setSuggestionType(tipType == TipType.COMPLETER ? SuggestionType.COMPLETER : SuggestionType.TAIL_TIP);
if (bpsize - 1 < params.size()) {
if (!lastArg.startsWith("-")) {
desc = params.get(bpsize - 1).getDescription();
}
StringBuilder tip = new StringBuilder();
for (int i = bpsize - 1; i < params.size(); i++) {
tip.append(params.get(i).getName());
tip.append(" ");
}
setTailTip(tip.toString());
} else if (params.get(params.size() - 1).getName().charAt(0) == '[') {
setTailTip(params.get(params.size() - 1).getName());
desc = params.get(params.size() - 1).getDescription();
}
setTailTip(tip.toString());
} else if (params.get(params.size() - 1).getName().charAt(0) == '[') {
setTailTip(params.get(params.size() - 1).getName());
desc = params.get(params.size() - 1).getDescription();
} else if (doTailTip) {
clearTip = true;
}
} else {
clearTip = true;
}
if (clearTip) {
setTailTip("");
if (tipType != TipType.TAIL_TIP) {
setSuggestionType(SuggestionType.COMPLETER);
Expand Down Expand Up @@ -800,6 +821,9 @@ private boolean defaultBindings() {
map.bind(new Reference(LineReader.SELF_INSERT), " ");
map.bind(new Reference(LineReader.SELF_INSERT), "=");
map.bind(new Reference(LineReader.SELF_INSERT), "-");
map.bind(new Reference(LineReader.SELF_INSERT), range("A-Z"));
map.bind(new Reference(LineReader.SELF_INSERT), range("a-z"));

setSuggestionType(SuggestionType.NONE);
if (autopairEnabled()) {
callWidget(AP_TOGGLE);
Expand All @@ -821,6 +845,8 @@ private void customBindings() {
map.bind(new Reference("_tailtip-insert"), " ");
map.bind(new Reference("_tailtip-insert"), "=");
map.bind(new Reference("_tailtip-insert"), "-");
map.bind(new Reference("_tailtip-insert"), range("A-Z"));
map.bind(new Reference("_tailtip-insert"), range("a-z"));
if (tipType != TipType.TAIL_TIP) {
setSuggestionType(SuggestionType.COMPLETER);
} else {
Expand Down Expand Up @@ -889,7 +915,15 @@ public List<AttributedString> getOptionDescription(String opt) {
}
}
if (optsDesc.containsKey(opt)) {
out = new ArrayList<>(optsDesc.get(opt));
out.add(new AttributedString(opt));
for (AttributedString as: optsDesc.get(opt)) {
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(" \t");
asb.append(as);
out.add(asb.toAttributedString());
}
} else if (optsDesc.containsKey("main")) {
out = new ArrayList<>(optsDesc.get("main"));
} else {
for (Map.Entry<String, List<AttributedString>> entry: optsDesc.entrySet()) {
if (entry.getKey().startsWith(opt)) {
Expand Down
28 changes: 23 additions & 5 deletions builtins/src/test/java/org/jline/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,10 @@ public static void main(String[] args) throws IOException {
new Completer() {
@Override
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
candidates.add(new Candidate("foo11", "foo11", null, "with complete argDesc", null, null, true));
candidates.add(new Candidate("foo12", "foo12", null, "with argDesc -names only", null, null, true));
candidates.add(new Candidate("foo11", "foo11", null, "complete cmdDesc", null, null, true));
candidates.add(new Candidate("foo12", "foo12", null, "cmdDesc -names only", null, null, true));
candidates.add(new Candidate("foo13", "foo13", null, "-", null, null, true));
candidates.add(new Candidate("widget", "widget", null, "cmdDesc with short options", null, null, true));
}
},
new StringsCompleter("foo21", "foo22", "foo23"),
Expand Down Expand Up @@ -322,10 +323,24 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
AutosuggestionWidgets autosuggestionWidgets = new AutosuggestionWidgets(reader);
Map<String, CmdDesc> tailTips = new HashMap<>();
Map<String, List<AttributedString>> optDesc = new HashMap<>();
optDesc.put("--option1", Arrays.asList(new AttributedString("option1 description...")));
optDesc.put("--option2", Arrays.asList(new AttributedString("option2 description...")));
optDesc.put("--option3", Arrays.asList(new AttributedString("option3 description...")
optDesc.put("--optionA", Arrays.asList(new AttributedString("optionA description...")));
optDesc.put("--noitpoB", Arrays.asList(new AttributedString("noitpoB description...")));
optDesc.put("--optionC", Arrays.asList(new AttributedString("optionC description...")
, new AttributedString("line2")));
Map<String, List<AttributedString>> widgetOpts = new HashMap<>();
widgetOpts.put("main", Arrays.asList(new AttributedString("widget -N new-widget [function-name]")
, new AttributedString("widget -D widget ...")
, new AttributedString("widget -A old-widget new-widget")
, new AttributedString("widget -U string ...")
, new AttributedString("widget -l [options]")
));
widgetOpts.put("-N", Arrays.asList(new AttributedString("Create new widget")));
widgetOpts.put("-D", Arrays.asList(new AttributedString("Delete widgets")));
widgetOpts.put("-A", Arrays.asList(new AttributedString("Create alias to widget")));
widgetOpts.put("-U", Arrays.asList(new AttributedString("Push characters to the stack")));
widgetOpts.put("-l", Arrays.asList(new AttributedString("List user-defined widgets")));

tailTips.put("widget", new CmdDesc(ArgDesc.doArgNames(Arrays.asList("[pN...]")), widgetOpts));
tailTips.put("foo12", new CmdDesc(ArgDesc.doArgNames(Arrays.asList("param1", "param2", "[paramN...]"))));
tailTips.put("foo11", new CmdDesc(Arrays.asList(
new ArgDesc("param1",Arrays.asList(new AttributedString("Param1 description...")
Expand Down Expand Up @@ -544,6 +559,9 @@ else if ("help".equals(pl.word()) || "?".equals(pl.word())) {
catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
catch (IllegalStateException e) {
e.printStackTrace();
}
catch (UserInterruptException e) {
// Ignore
}
Expand Down

0 comments on commit eeff0fb

Please sign in to comment.