From a89066045f5320bca30971b25b7951c30c80f63c Mon Sep 17 00:00:00 2001 From: mattirn Date: Sun, 10 Nov 2019 18:32:03 +0100 Subject: [PATCH] Extend StringsCompleter to accept a lambda for the strings, fixes #464 --- .../test/java/org/jline/example/Example.java | 38 ++++++++++++++++++- .../impl/completer/StringsCompleter.java | 23 ++++++++--- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/builtins/src/test/java/org/jline/example/Example.java b/builtins/src/test/java/org/jline/example/Example.java index 72dd43346..477cf0f42 100644 --- a/builtins/src/test/java/org/jline/example/Example.java +++ b/builtins/src/test/java/org/jline/example/Example.java @@ -18,6 +18,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; +import java.util.function.Supplier; import org.jline.builtins.Commands; import org.jline.builtins.Completers; @@ -37,6 +38,7 @@ import org.jline.reader.impl.DefaultParser; import org.jline.reader.impl.DefaultParser.Bracket; import org.jline.reader.impl.LineReaderImpl; +import org.jline.reader.impl.completer.AggregateCompleter; import org.jline.reader.impl.completer.ArgumentCompleter; import org.jline.reader.impl.completer.StringsCompleter; import org.jline.terminal.Cursor; @@ -63,7 +65,8 @@ public static void usage() { , " -system terminalBuilder.system(false)" , " +system terminalBuilder.system(true)" , " Completors:" - , " argumet an argument completor & autosuggestion" + , " aggregate an aggregate completor with strings Supplier" + , " argument an argument completor & autosuggestion" , " files a completor that completes file names" , " none no completors" , " param a paramenter completer using Java functional interface" @@ -120,6 +123,27 @@ public static void help() { } + private static class ReaderOptions { + LineReader reader; + + public ReaderOptions() { + } + + public void setReader(LineReader reader) { + this.reader = reader; + } + + List unsetted(boolean set) { + List out = new ArrayList<>(); + for (Option option : Option.values()) { + if (set == (reader.isSet(option) == option.isDef())) { + out.add((option.isDef() ? "no-" : "") + option.toString().toLowerCase().replace('_', '-')); + } + } + return out; + } + } + public static void main(String[] args) throws IOException { try { String prompt = "prompt> "; @@ -141,6 +165,7 @@ public static void main(String[] args) throws IOException { Completer completer = null; Parser parser = null; List> callbacks = new ArrayList<>(); + ReaderOptions readerOptions = new ReaderOptions(); for (int index=0; index < args.length; index++) { switch (args[index]) { @@ -205,6 +230,16 @@ public static void main(String[] args) throws IOException { }).start(); }); break; + case "aggregate": + List ccc = new ArrayList<>(); + ccc.add(new ArgumentCompleter( + new StringsCompleter("setopt"), + new StringsCompleter(() -> readerOptions.unsetted(true)))); + ccc.add(new ArgumentCompleter( + new StringsCompleter("unsetopt"), + new StringsCompleter(() -> readerOptions.unsetted(false)))); + completer = new AggregateCompleter(ccc); + break; case "argument": completer = new ArgumentCompleter( new Completer() { @@ -319,6 +354,7 @@ public void complete(LineReader reader, ParsedLine line, List candida .variable(LineReader.INDENTATION, 2) .option(Option.INSERT_BRACKET, true) .build(); + readerOptions.setReader(reader); AutopairWidgets autopairWidgets = new AutopairWidgets(reader); AutosuggestionWidgets autosuggestionWidgets = new AutosuggestionWidgets(reader); Map tailTips = new HashMap<>(); diff --git a/reader/src/main/java/org/jline/reader/impl/completer/StringsCompleter.java b/reader/src/main/java/org/jline/reader/impl/completer/StringsCompleter.java index b9818fd8b..2d876282b 100644 --- a/reader/src/main/java/org/jline/reader/impl/completer/StringsCompleter.java +++ b/reader/src/main/java/org/jline/reader/impl/completer/StringsCompleter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2016, the original author or authors. + * Copyright (c) 2002-2019, the original author or authors. * * This software is distributable under the BSD license. See the terms of the * BSD license in the documentation provided with this software. @@ -12,6 +12,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; +import java.util.function.Supplier; import org.jline.reader.Candidate; import org.jline.reader.Completer; @@ -27,11 +28,18 @@ */ public class StringsCompleter implements Completer { - protected final Collection candidates = new ArrayList<>(); + protected Collection candidates = new ArrayList<>(); + protected Supplier> stringsSupplier; public StringsCompleter() { } + public StringsCompleter(Supplier> stringsSupplier) { + assert stringsSupplier != null; + candidates = null; + this.stringsSupplier = stringsSupplier; + } + public StringsCompleter(String... strings) { this(Arrays.asList(strings)); } @@ -44,8 +52,7 @@ public StringsCompleter(Iterable strings) { } public StringsCompleter(Candidate ... candidates) { - assert candidates != null; - this.candidates.addAll(Arrays.asList(candidates)); + this(Arrays.asList(candidates)); } public StringsCompleter(Collection candidates) { @@ -56,7 +63,13 @@ public StringsCompleter(Collection candidates) { public void complete(LineReader reader, final ParsedLine commandLine, final List candidates) { assert commandLine != null; assert candidates != null; - candidates.addAll(this.candidates); + if (this.candidates != null) { + candidates.addAll(this.candidates); + } else { + for (String string : stringsSupplier.get()) { + candidates.add(new Candidate(AttributedString.stripAnsi(string), string, null, null, null, null, true)); + } + } } }