Skip to content

Commit

Permalink
TailTipWidgets: bug fix & small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Oct 11, 2019
1 parent 3462231 commit 5c464bc
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 53 deletions.
20 changes: 13 additions & 7 deletions builtins/src/main/java/org/jline/builtins/Widgets.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.jline.reader.Widget;
import org.jline.reader.impl.BufferImpl;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import org.jline.utils.Status;

public abstract class Widgets {
Expand Down Expand Up @@ -110,10 +112,10 @@ public void addDescription(List<AttributedString> desc) {
}

public void clearDescription() {
clearDescription(0);
initDescription(0);
}

public void clearDescription(int size) {
public void initDescription(int size) {
if (size > 0) {
List<AttributedString> as = new ArrayList<>();
for (int i = 0; i < size; i++) {
Expand Down Expand Up @@ -507,10 +509,10 @@ public TailTipWidgets(LineReader reader, Map<String,List<ArgDesc>> tailTips, int

public TailTipWidgets(LineReader reader, Map<String,List<ArgDesc>> tailTips, int descriptionSize, TipType tipType) {
super(reader);
this.tailTips.putAll(tailTips);
this.tailTips = new HashMap<>(tailTips);
this.descriptionSize = descriptionSize;
this.tipType = tipType;
clearDescription(descriptionSize);
initDescription(descriptionSize);
addWidget("_tailtip-accept-line", this::tailtipAcceptLine);
addWidget("_tailtip-insert", this::tailtipInsert);
addWidget("_tailtip-backward-delete-char", this::tailtipBackwardDelete);
Expand Down Expand Up @@ -543,7 +545,7 @@ private void addKeySequence(Reference widget, String keySequence) {

public void setDescriptionSize(int descriptionSize) {
this.descriptionSize = descriptionSize;
clearDescription(descriptionSize);
initDescription(descriptionSize);
}

public int getDescriptionSize() {
Expand Down Expand Up @@ -645,7 +647,11 @@ private void doDescription(List<AttributedString> desc) {
} else if (desc.size() == descriptionSize) {
addDescription(desc);
} else if (desc.size() > descriptionSize) {
addDescription(desc.subList(0, descriptionSize));
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(desc.get(descriptionSize - 1)).append("...", new AttributedStyle(AttributedStyle.INVERSE));
List<AttributedString> mod = new ArrayList<>(desc.subList(0, descriptionSize-1));
mod.add(asb.toAttributedString());
addDescription(mod);
} else if (desc.size() < descriptionSize) {
while (desc.size() != descriptionSize) {
desc.add(new AttributedString(""));
Expand Down Expand Up @@ -729,7 +735,7 @@ public ArgDesc(String name) {

public ArgDesc(String name, List<AttributedString> description) {
this.name = name;
this.description.addAll(description);
this.description = new ArrayList<>(description);
}

public String getName() {
Expand Down
38 changes: 23 additions & 15 deletions builtins/src/test/java/org/jline/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void usage() {
, " -system terminalBuilder.system(false)"
, " +system terminalBuilder.system(true)"
, " Completors:"
, " argumet an argument completor"
, " argumet an argument completor & autosuggestion"
, " files a completor that completes file names"
, " none no completors"
, " param a paramenter completer using Java functional interface"
Expand Down Expand Up @@ -205,7 +205,14 @@ public static void main(String[] args) throws IOException {
break;
case "argument":
completer = new ArgumentCompleter(
new StringsCompleter("foo11", "foo12", "foo13", "tail"),
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("foo13", "foo13", null, "-", null, null, true));
}
},
new StringsCompleter("foo21", "foo22", "foo23"),
new Completer() {
@Override
Expand Down Expand Up @@ -310,16 +317,17 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
AutopairWidgets autopairWidgets = new AutopairWidgets(reader);
AutosuggestionWidgets autosuggestionWidgets = new AutosuggestionWidgets(reader);
Map<String, List<ArgDesc>> tailTips = new HashMap<>();
tailTips.put("tail", ArgDesc.doArgNames(Arrays.asList("param1", "param2", "[paramN...]")));
tailTips.put("foo12", ArgDesc.doArgNames(Arrays.asList("param1", "param2", "[paramN...]")));
tailTips.put("foo11", Arrays.asList(
new ArgDesc("param1",Arrays.asList(new AttributedString("line 1")
, new AttributedString("line 2")
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.")
, new AttributedString("line 3")
, new AttributedString("line 4")
, new AttributedString("line 5")
, new AttributedString("line 6")
))
, new ArgDesc("param2",Arrays.asList(new AttributedString("line 1")
, new ArgDesc("param2",Arrays.asList(new AttributedString("Param2 description...")
, new AttributedString("line 2")
))
, new ArgDesc("param3", new ArrayList<>())
Expand Down Expand Up @@ -477,29 +485,29 @@ else if ("autopair".equals(pl.word())) {
}
else if ("autosuggestion".equals(pl.word())) {
if (pl.words().size() > 1) {
String type = pl.words().get(1);
if (type.toLowerCase().startsWith("his")) {
String type = pl.words().get(1).toLowerCase();
if (type.startsWith("his")) {
tailtipWidgets.defaultBindings();
autosuggestionWidgets.autosuggestionBindings();
} else if (type.toLowerCase().startsWith("tai")) {
} else if (type.startsWith("tai")) {
autosuggestionWidgets.defaultBindings();
tailtipWidgets.autosuggestionBindings();
tailtipWidgets.setDescriptionSize(5);
if (pl.words().size() > 2) {
String mode = pl.words().get(2);
if (mode.toLowerCase().startsWith("tai")) {
String mode = pl.words().get(2).toLowerCase();
if (mode.startsWith("tai")) {
tailtipWidgets.setTipType(TipType.TAIL_TIP);
} else if (mode.toLowerCase().startsWith("comp")) {
} else if (mode.startsWith("comp")) {
tailtipWidgets.setTipType(TipType.COMPLETER);
} else if (mode.toLowerCase().startsWith("comb")) {
} else if (mode.startsWith("comb")) {
tailtipWidgets.setTipType(TipType.COMBINED);
}
}
} else if (type.toLowerCase().startsWith("com")) {
} else if (type.startsWith("com")) {
autosuggestionWidgets.defaultBindings();
tailtipWidgets.defaultBindings();
reader.setAutosuggestion(SuggestionType.COMPLETER);
} else if (type.toLowerCase().startsWith("non")) {
} else if (type.startsWith("non")) {
autosuggestionWidgets.defaultBindings();
tailtipWidgets.defaultBindings();
reader.setAutosuggestion(SuggestionType.NONE);
Expand Down
65 changes: 34 additions & 31 deletions reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ protected enum BellType {
protected boolean searchFailing;
protected boolean searchBackward;
protected int searchIndex = -1;
protected boolean inCompleterMenu;


// Reading buffers
Expand Down Expand Up @@ -3942,49 +3943,50 @@ public AttributedString getDisplayedBufferWithPrompts(List<AttributedString> sec
AttributedStringBuilder full = new AttributedStringBuilder().tabs(TAB_WIDTH);
full.append(prompt);
full.append(tNewBuf);
String lastBinding = getLastBinding() != null ? getLastBinding() : "";
if (autosuggestion == SuggestionType.HISTORY) {
AttributedStringBuilder sb = new AttributedStringBuilder();
tailTip = matchPreviousCommand(buf.toString());
sb.styled(AttributedStyle::faint, tailTip);
full.append(sb.toAttributedString());
} else if (autosuggestion == SuggestionType.COMPLETER) {
if (buf.length() > 0 && buf.length() == buf.cursor()
&& (!lastBinding.equals("\t") || buf.prevChar() == ' ')) {
if (buf.prevChar() == ' ') {
if (!inCompleterMenu) {
String lastBinding = getLastBinding() != null ? getLastBinding() : "";
if (autosuggestion == SuggestionType.HISTORY) {
AttributedStringBuilder sb = new AttributedStringBuilder();
tailTip = matchPreviousCommand(buf.toString());
sb.styled(AttributedStyle::faint, tailTip);
full.append(sb.toAttributedString());
} else if (autosuggestion == SuggestionType.COMPLETER) {
if (buf.length() > 0 && buf.length() == buf.cursor()
&& (!lastBinding.equals("\t") || buf.prevChar() == ' ')) {
clearChoices();
}
listChoices(true);
} else if (!lastBinding.equals("\t")){
clearChoices();
clearStatus();
}
} else if (autosuggestion == SuggestionType.TAIL_TIP) {
if (buf.length() == buf.cursor()) {
if (!lastBinding.equals("\t")){
listChoices(true);
} else if (!lastBinding.equals("\t")){
clearChoices();
clearStatus();
}
AttributedStringBuilder sb = new AttributedStringBuilder();
if (buf.prevChar() != ' ') {
if (!tailTip.startsWith("[")) {
int idx = tailTip.indexOf(' ');
if (idx > 0) {
tailTip = tailTip.substring(idx);
} else if (autosuggestion == SuggestionType.TAIL_TIP) {
if (buf.length() == buf.cursor()) {
if (!lastBinding.equals("\t")){
clearChoices();
}
AttributedStringBuilder sb = new AttributedStringBuilder();
if (buf.prevChar() != ' ') {
if (!tailTip.startsWith("[")) {
int idx = tailTip.indexOf(' ');
if (idx > 0) {
tailTip = tailTip.substring(idx);
}
} else {
sb.append(" ");
}
} else {
sb.append(" ");
}
sb.styled(AttributedStyle::faint, tailTip);
full.append(sb.toAttributedString());
} else {
clearStatus();
}
sb.styled(AttributedStyle::faint, tailTip);
full.append(sb.toAttributedString());
} else {
clearStatus();
}
}
if (post != null) {
full.append("\n");
full.append(post.get());
}
inCompleterMenu = false;
return full.toAttributedString();
}

Expand Down Expand Up @@ -4897,6 +4899,7 @@ && getLastBinding().charAt(0) != ' '
return true;
}
}
inCompleterMenu = true;
redisplay();
}
return false;
Expand Down

0 comments on commit 5c464bc

Please sign in to comment.