diff --git a/builtins/src/main/java/org/jline/builtins/Nano.java b/builtins/src/main/java/org/jline/builtins/Nano.java index 0bf5131ef..2e7cc59d8 100644 --- a/builtins/src/main/java/org/jline/builtins/Nano.java +++ b/builtins/src/main/java/org/jline/builtins/Nano.java @@ -290,19 +290,19 @@ void read(InputStream fis) throws IOException { moveToChar(0); } - private int charPosition(int displayPosition){ + private int charPosition(int displayPosition) { return charPosition(line, displayPosition, CursorMovement.STILL); } - private int charPosition(int displayPosition, CursorMovement move){ + private int charPosition(int displayPosition, CursorMovement move) { return charPosition(line, displayPosition, move); } - private int charPosition(int line, int displayPosition){ + private int charPosition(int line, int displayPosition) { return charPosition(line, displayPosition, CursorMovement.STILL); } - private int charPosition(int line, int displayPosition, CursorMovement move){ + private int charPosition(int line, int displayPosition, CursorMovement move) { int out = lines.get(line).length(); if (!lines.get(line).contains("\t") || displayPosition == 0) { out = displayPosition; @@ -806,7 +806,7 @@ List computeHeader() { } } - void highlightDisplayedLine(int curLine, int curOffset, int nextOffset, AttributedStringBuilder line){ + void highlightDisplayedLine(int curLine, int curOffset, int nextOffset, AttributedStringBuilder line) { AttributedString disp = highlight ? syntaxHighlighter.highlight(new AttributedStringBuilder().tabs(tabs).append(getLine(curLine))) : new AttributedStringBuilder().tabs(tabs).append(getLine(curLine)).toAttributedString(); int[] hls = highlightStart(); @@ -1606,7 +1606,7 @@ public Pattern getEnd() { return end; } - public static RuleType evalRuleType(List colorCfg){ + public static RuleType evalRuleType(List colorCfg) { RuleType out = null; if (colorCfg.get(0).equals("color") || colorCfg.get(0).equals("icolor")) { out = RuleType.PATTERN; @@ -1776,14 +1776,14 @@ private Pattern doPattern(String regex, boolean caseInsensitive) { } protected static class Parser { - protected static List split(String s){ + protected static List split(String s) { List out = new ArrayList(); if (s.length() == 0) { return out; } int depth = 0; StringBuilder sb = new StringBuilder(); - for(int i = 0; i < s.length(); i++) { + for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '"') { depth = depth == 0 ? 1 : 0; @@ -1792,7 +1792,7 @@ protected static List split(String s){ sb = new StringBuilder(); continue; } - if(sb.length() > 0 || (c!=' ' && c!='\t')) { + if (sb.length() > 0 || (c!=' ' && c!='\t')) { sb.append(c); } } @@ -1802,7 +1802,7 @@ protected static List split(String s){ return out; } - private static String stripQuotes(String s){ + private static String stripQuotes(String s) { String out = s.trim(); if (s.startsWith("\"") && s.endsWith("\"")) { out = s.substring(1, s.length() - 1); @@ -1891,7 +1891,7 @@ public void add(String pattern) { patternId = -1; } - public void persist(){ + public void persist() { if (historyFile == null) { return; } @@ -2739,7 +2739,7 @@ void gotoLine() throws IOException { String[] pos = editBuffer.toString().split(",", 2); int[] args = { 0, 0 }; try { - for(int i = 0; i < pos.length; i++) { + for (int i = 0; i < pos.length; i++) { if (pos[i].trim().length() > 0) { args[i] = Integer.parseInt(pos[i]) - 1; if (args[i] < 0) { @@ -2888,12 +2888,14 @@ void help(String help) { boolean oldPrintLineNumbers = this.printLineNumbers; boolean oldConstantCursor = this.constantCursor; boolean oldAtBlanks = this.atBlanks; + boolean oldHighlight = this.highlight; String oldEditMessage = this.editMessage; this.editMessage = ""; this.wrapping = true; this.atBlanks = true; this.printLineNumbers = false; this.constantCursor = false; + this.highlight = false; this.buffer = newBuf; if (!oldWrapping) { buffer.computeAllOffsets(); @@ -2943,6 +2945,7 @@ void help(String help) { this.constantCursor = oldConstantCursor; this.shortcuts = oldShortcuts; this.atBlanks = oldAtBlanks; + this.highlight = oldHighlight; this.editMessage = oldEditMessage; terminal.puts(Capability.cursor_visible); if (!oldWrapping) { @@ -3414,7 +3417,7 @@ void enableSuspension() { } } - void toggleSuspension(){ + void toggleSuspension() { if (restricted) { setMessage("This function is disabled in restricted mode"); } else if (vsusp < 0) { diff --git a/builtins/src/test/java/org/jline/example/Example.java b/builtins/src/test/java/org/jline/example/Example.java index e34f2fdc2..3392d9054 100644 --- a/builtins/src/test/java/org/jline/example/Example.java +++ b/builtins/src/test/java/org/jline/example/Example.java @@ -20,6 +20,7 @@ import java.util.concurrent.TimeUnit; import java.util.function.Consumer; import java.util.regex.Pattern; +import java.util.function.Supplier; import org.jline.builtins.Commands; import org.jline.builtins.Completers; @@ -40,6 +41,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; @@ -66,7 +68,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" @@ -315,6 +318,27 @@ private CmdDesc commandDescription(String command) { } } + 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> "; @@ -337,6 +361,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]) { @@ -401,6 +426,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": argument = true; completer = new ArgumentCompleter( @@ -517,7 +552,7 @@ public void complete(LineReader reader, ParsedLine line, List candida .build(); Executor executor = new Executor(reader); - + readerOptions.setReader(reader); AutopairWidgets autopairWidgets = new AutopairWidgets(reader); AutosuggestionWidgets autosuggestionWidgets = new AutosuggestionWidgets(reader); TailTipWidgets tailtipWidgets = null; 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)); + } + } } }