From 5c464bce1f9566aaf325cb246712fab121e66635 Mon Sep 17 00:00:00 2001 From: mattirn Date: Fri, 11 Oct 2019 10:01:26 +0200 Subject: [PATCH] TailTipWidgets: bug fix & small improvements --- .../main/java/org/jline/builtins/Widgets.java | 20 ++++-- .../test/java/org/jline/example/Example.java | 38 ++++++----- .../org/jline/reader/impl/LineReaderImpl.java | 65 ++++++++++--------- 3 files changed, 70 insertions(+), 53 deletions(-) diff --git a/builtins/src/main/java/org/jline/builtins/Widgets.java b/builtins/src/main/java/org/jline/builtins/Widgets.java index 7674af3c4..954f5e4ba 100644 --- a/builtins/src/main/java/org/jline/builtins/Widgets.java +++ b/builtins/src/main/java/org/jline/builtins/Widgets.java @@ -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 { @@ -110,10 +112,10 @@ public void addDescription(List desc) { } public void clearDescription() { - clearDescription(0); + initDescription(0); } - public void clearDescription(int size) { + public void initDescription(int size) { if (size > 0) { List as = new ArrayList<>(); for (int i = 0; i < size; i++) { @@ -507,10 +509,10 @@ public TailTipWidgets(LineReader reader, Map> tailTips, int public TailTipWidgets(LineReader reader, Map> 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); @@ -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() { @@ -645,7 +647,11 @@ private void doDescription(List 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 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("")); @@ -729,7 +735,7 @@ public ArgDesc(String name) { public ArgDesc(String name, List description) { this.name = name; - this.description.addAll(description); + this.description = new ArrayList<>(description); } public String getName() { diff --git a/builtins/src/test/java/org/jline/example/Example.java b/builtins/src/test/java/org/jline/example/Example.java index f347e3092..126b3d6c9 100644 --- a/builtins/src/test/java/org/jline/example/Example.java +++ b/builtins/src/test/java/org/jline/example/Example.java @@ -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" @@ -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 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 @@ -310,16 +317,17 @@ public void complete(LineReader reader, ParsedLine line, List candida AutopairWidgets autopairWidgets = new AutopairWidgets(reader); AutosuggestionWidgets autosuggestionWidgets = new AutosuggestionWidgets(reader); Map> 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<>()) @@ -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); diff --git a/reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java b/reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java index d52ac7fdf..17f0324cf 100644 --- a/reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java +++ b/reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java @@ -187,6 +187,7 @@ protected enum BellType { protected boolean searchFailing; protected boolean searchBackward; protected int searchIndex = -1; + protected boolean inCompleterMenu; // Reading buffers @@ -3942,49 +3943,50 @@ public AttributedString getDisplayedBufferWithPrompts(List 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(); } @@ -4897,6 +4899,7 @@ && getLastBinding().charAt(0) != ' ' return true; } } + inCompleterMenu = true; redisplay(); } return false;