Skip to content

Commit

Permalink
TailTipWidgets: added setTailTips() method and field valid in CmdDesc
Browse files Browse the repository at this point in the history
class
  • Loading branch information
mattirn committed Nov 4, 2019
1 parent c926b8b commit 36bfbb6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
48 changes: 35 additions & 13 deletions builtins/src/main/java/org/jline/builtins/Widgets.java
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,10 @@ private TailTipWidgets(LineReader reader
addWidget(TT_TOGGLE, this::toggleKeyBindings);
}

public void setTailTips(Map<String,CmdDesc> tailTips) {
cmdDescs.setDescriptions(tailTips);
}

public void setDescriptionSize(int descriptionSize) {
this.descriptionSize = descriptionSize;
initDescription(descriptionSize);
Expand Down Expand Up @@ -771,22 +775,28 @@ private boolean doTailTip(String widget) {
Pair<String,Boolean> cmdkey = cmdDescs.evaluateCommandLine(buffer.toString(), args);
CmdDesc cmdDesc = cmdDescs.getDescription(cmdkey.getU());
if (cmdDesc == null) {
setErrorPattern(null);
setErrorIndex(-1);
clearDescription();
resetTailTip();
} else if (cmdkey.getV()) {
doCommandTailTip(widget, cmdDesc, args);
} else {
doDescription(cmdDesc.getMainDescription(descriptionSize));
setErrorPattern(cmdDesc.getErrorPattern());
setErrorIndex(cmdDesc.getErrorIndex());
} else if (cmdDesc.isValid()) {
if (cmdkey.getV()) {
doCommandTailTip(widget, cmdDesc, args);
} else {
doDescription(cmdDesc.getMainDescription(descriptionSize));
setErrorPattern(cmdDesc.getErrorPattern());
setErrorIndex(cmdDesc.getErrorIndex());
}
}
} else {
Pair<String,Boolean> cmdkey = cmdDescs.evaluateCommandLine(buffer.toString(), buffer.cursor());
CmdDesc cmdDesc = cmdDescs.getDescription(cmdkey.getU());
if (cmdDesc == null) {
setErrorPattern(null);
setErrorIndex(-1);
clearDescription();
resetTailTip();
} else if (!cmdkey.getV()) {
} else if (cmdDesc.isValid() && !cmdkey.getV()) {
doDescription(cmdDesc.getMainDescription(descriptionSize));
setErrorPattern(cmdDesc.getErrorPattern());
setErrorIndex(cmdDesc.getErrorIndex());
Expand Down Expand Up @@ -970,6 +980,10 @@ public CommandDescriptions(Function<CmdLine,CmdDesc> descFun) {
this.descFun = descFun;
}

public void setDescriptions(Map<String,CmdDesc> descriptions) {
this.descriptions = new HashMap<>(descriptions);
}

public Pair<String,Boolean> evaluateCommandLine(String line, int curPos) {
return evaluateCommandLine(line, args(), curPos);
}
Expand Down Expand Up @@ -1059,7 +1073,7 @@ public void clearTemporaryDescs() {
public static class CmdLine {
public enum DescriptionType {
/**
* Cursor is at the end of line. The args[0] is completed, the line does not have unclosed opening parenthesis
* Cursor is at the end of line. The args[0] is completed, the line does not have unclosed opening parenthesis
* and does not end to the closing parenthesis.
*/
COMMAND,
Expand Down Expand Up @@ -1150,9 +1164,13 @@ public static class CmdDesc {
private TreeMap<String,List<AttributedString>> optsDesc;
private Pattern errorPattern;
private int errorIndex = -1;
private boolean valid = true;

public CmdDesc() {
}

public CmdDesc(boolean valid) {
this.valid = valid;
}

public CmdDesc(List<ArgDesc> argsDesc) {
Expand All @@ -1174,11 +1192,15 @@ public CmdDesc(List<AttributedString> mainDesc, List<ArgDesc> argsDesc, Map<Stri
}
}

public boolean isValid() {
return valid;
}

public CmdDesc mainDesc(List<AttributedString> mainDesc) {
this.mainDesc = new ArrayList<>(mainDesc);
return this;
}

public void setMainDesc(List<AttributedString> mainDesc) {
this.mainDesc = new ArrayList<>(mainDesc);
}
Expand All @@ -1192,13 +1214,13 @@ public Pattern getErrorPattern() {
}

public void setErrorIndex(int errorIndex) {
this.errorIndex = errorIndex;
this.errorIndex = errorIndex;
}

public int getErrorIndex() {
return errorIndex;
return errorIndex;
}

public List<ArgDesc> getArgsDesc() {
return argsDesc;
}
Expand Down
1 change: 1 addition & 0 deletions builtins/src/test/java/org/jline/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ CmdDesc commandDescription(CmdLine line) {
out = methodDescription(line);
break;
case SYNTAX:
out = new CmdDesc(false);
break;
}
return out;
Expand Down

0 comments on commit 36bfbb6

Please sign in to comment.