Skip to content

Commit

Permalink
Refactor: added a new package org.jline.console
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Apr 18, 2020
1 parent d53681a commit da71da6
Show file tree
Hide file tree
Showing 18 changed files with 549 additions and 480 deletions.
7 changes: 4 additions & 3 deletions builtins/src/main/java/org/jline/builtins/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
import org.jline.builtins.Completers.FilesCompleter;
import org.jline.builtins.Completers.OptDesc;
import org.jline.builtins.Completers.OptionCompleter;
import org.jline.builtins.Completers.SystemCompleter;
import org.jline.builtins.Options.HelpException;
import org.jline.builtins.Widgets.ArgDesc;
import org.jline.builtins.Widgets.CmdDesc;
import org.jline.console.ArgDesc;
import org.jline.console.CmdDesc;
import org.jline.console.ConfigurationPath;
import org.jline.reader.*;
import org.jline.reader.LineReader.Option;
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.reader.impl.completer.SystemCompleter;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedString;

Expand Down
20 changes: 11 additions & 9 deletions builtins/src/main/java/org/jline/builtins/CommandRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
*/
package org.jline.builtins;

import org.jline.builtins.Completers;
import org.jline.builtins.Widgets;
import org.jline.builtins.Options.HelpException;
import org.jline.console.ArgDesc;
import org.jline.console.CmdDesc;
import org.jline.reader.impl.completer.SystemCompleter;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedString;

Expand All @@ -30,8 +32,8 @@ public interface CommandRegistry {
* @param commandRegistries command registeries which completers is to be aggregated
* @return uncompiled SystemCompleter
*/
static Completers.SystemCompleter aggregateCompleters(CommandRegistry ... commandRegistries) {
Completers.SystemCompleter out = new Completers.SystemCompleter();
static SystemCompleter aggregateCompleters(CommandRegistry ... commandRegistries) {
SystemCompleter out = new SystemCompleter();
for (CommandRegistry r: commandRegistries) {
out.add(r.compileCompleters());
}
Expand All @@ -43,8 +45,8 @@ static Completers.SystemCompleter aggregateCompleters(CommandRegistry ... comman
* @param commandRegistries command registeries which completers is to be aggregated and compile
* @return compiled SystemCompleter
*/
static Completers.SystemCompleter compileCompleters(CommandRegistry ... commandRegistries) {
Completers.SystemCompleter out = aggregateCompleters(commandRegistries);
static SystemCompleter compileCompleters(CommandRegistry ... commandRegistries) {
SystemCompleter out = aggregateCompleters(commandRegistries);
out.compile();
return out;
}
Expand Down Expand Up @@ -99,7 +101,7 @@ default List<String> commandInfo(String command) {
* information for all registered commands.
* @return a SystemCompleter that can provide command completion for all registered commands
*/
Completers.SystemCompleter compileCompleters();
SystemCompleter compileCompleters();

/**
* Returns a command description for use in the JLine Widgets framework.
Expand All @@ -108,7 +110,7 @@ default List<String> commandInfo(String command) {
* @return command description for JLine TailTipWidgets to be displayed
* in the terminal status bar.
*/
default Widgets.CmdDesc commandDescription(List<String> args) {
default CmdDesc commandDescription(List<String> args) {
return !args.isEmpty() ? commandDescription(args.get(0)) : commandDescription("");
}

Expand All @@ -118,7 +120,7 @@ default Widgets.CmdDesc commandDescription(List<String> args) {
* @return command description for JLine TailTipWidgets to be displayed
* in the terminal status bar.
*/
default Widgets.CmdDesc commandDescription(String command) {
default CmdDesc commandDescription(String command) {
try {
if (command != null && !command.isEmpty()) {
invoke(new CommandSession(), command, new Object[] {"--help"});
Expand All @@ -131,7 +133,7 @@ default Widgets.CmdDesc commandDescription(String command) {
break;
}
}
return new Widgets.CmdDesc(main, Widgets.ArgDesc.doArgNames(Arrays.asList("")), options);
return new CmdDesc(main, ArgDesc.doArgNames(Arrays.asList("")), options);
}
} catch (HelpException e) {
return Builtins.compileCommandDescription(e.getMessage());
Expand Down
4 changes: 2 additions & 2 deletions builtins/src/main/java/org/jline/builtins/Commands.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2019, the original author or authors.
* Copyright (c) 2002-2020, 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.
Expand Down Expand Up @@ -44,9 +44,9 @@
import org.jline.builtins.Options.HelpException;
import org.jline.builtins.Source.StdInSource;
import org.jline.builtins.Source.URLSource;
import org.jline.console.ConfigurationPath;
import org.jline.keymap.KeyMap;
import org.jline.reader.Binding;
import org.jline.reader.ConfigurationPath;
import org.jline.reader.Highlighter;
import org.jline.reader.History;
import org.jline.reader.LineReader;
Expand Down
127 changes: 0 additions & 127 deletions builtins/src/main/java/org/jline/builtins/Completers.java
Original file line number Diff line number Diff line change
Expand Up @@ -567,133 +567,6 @@ public int cursor() {
}
}

public static class SystemCompleter implements org.jline.reader.Completer {
private Map<String,List<org.jline.reader.Completer>> completers = new HashMap<>();
private Map<String,String> aliasCommand = new HashMap<>();
private StringsCompleter commands;
private boolean compiled = false;

public SystemCompleter() {}

@Override
public void complete(LineReader reader, ParsedLine commandLine, List<Candidate> candidates) {
if (!compiled) {
throw new IllegalStateException();
}
assert commandLine != null;
assert candidates != null;
if (commandLine.words().size() > 0) {
if (commandLine.words().size() == 1) {
String buffer = commandLine.words().get(0);
int eq = buffer.indexOf('=');
if (eq < 0) {
commands.complete(reader, commandLine, candidates);
} else if (reader.getParser().validVariableName(buffer.substring(0, eq))) {
String curBuf = buffer.substring(0, eq + 1);
for (String c: completers.keySet()) {
candidates.add(new Candidate(AttributedString.stripAnsi(curBuf+c)
, c, null, null, null, null, true));
}
}
} else {
String cmd = reader.getParser().getCommand(commandLine.words().get(0));
if (command(cmd) != null) {
completers.get(command(cmd)).get(0).complete(reader, commandLine, candidates);
}
}
}
}

public boolean isCompiled() {
return compiled;
}

private String command(String cmd) {
String out = null;
if (cmd != null) {
if (completers.containsKey(cmd)) {
out = cmd;
} else if (aliasCommand.containsKey(cmd)) {
out = aliasCommand.get(cmd);
}
}
return out;
}

public void add(String command, List<org.jline.reader.Completer> completers) {
for (org.jline.reader.Completer c : completers) {
add(command, c);
}
}

public void add(List<String> commands, org.jline.reader.Completer completer) {
for (String c: commands) {
add(c, completer);
}
}

public void add(String command, org.jline.reader.Completer completer) {
Objects.requireNonNull(command);
if (compiled) {
throw new IllegalStateException();
}
if (!completers.containsKey(command)) {
completers.put(command, new ArrayList<org.jline.reader.Completer>());
}
if (completer instanceof ArgumentCompleter) {
((ArgumentCompleter) completer).setStrictCommand(false);
}
completers.get(command).add(completer);
}

public void add(SystemCompleter other) {
if (other.isCompiled()) {
throw new IllegalStateException();
}
for (Map.Entry<String, List<org.jline.reader.Completer>> entry: other.getCompleters().entrySet()) {
for (org.jline.reader.Completer c: entry.getValue()) {
add(entry.getKey(), c);
}
}
addAliases(other.getAliases());
}

public void addAliases(Map<String,String> aliasCommand) {
if (compiled) {
throw new IllegalStateException();
}
this.aliasCommand.putAll(aliasCommand);
}

public Map<String,String> getAliases() {
return aliasCommand;
}

public void compile() {
if (compiled) {
return;
}
Map<String, List<org.jline.reader.Completer>> compiledCompleters = new HashMap<>();
for (Map.Entry<String, List<org.jline.reader.Completer>> entry: completers.entrySet()) {
if (entry.getValue().size() == 1) {
compiledCompleters.put(entry.getKey(), entry.getValue());
} else {
compiledCompleters.put(entry.getKey(), new ArrayList<org.jline.reader.Completer>());
compiledCompleters.get(entry.getKey()).add(new AggregateCompleter(entry.getValue()));
}
}
completers = compiledCompleters;
Set<String> cmds = new HashSet<>(completers.keySet());
cmds.addAll(aliasCommand.keySet());
commands = new StringsCompleter(cmds);
compiled = true;
}

public Map<String,List<org.jline.reader.Completer>> getCompleters() {
return completers;
}
}

public static class OptDesc {
private String shortOption;
private String longOption;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@
import org.jline.builtins.Completers.FilesCompleter;
import org.jline.builtins.Completers.OptDesc;
import org.jline.builtins.Completers.OptionCompleter;
import org.jline.builtins.Completers.SystemCompleter;
import org.jline.builtins.Nano.SyntaxHighlighter;
import org.jline.builtins.Options.HelpException;
import org.jline.console.ConfigurationPath;
import org.jline.console.ScriptEngine;
import org.jline.reader.*;
import org.jline.reader.Parser.ParseContext;
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.reader.impl.completer.SystemCompleter;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
Expand Down Expand Up @@ -254,7 +256,7 @@ public void alias(String alias, String command) {
}

@Override
public Completers.SystemCompleter compileCompleters() {
public SystemCompleter compileCompleters() {
SystemCompleter out = new SystemCompleter();
for (Map.Entry<Command, String> entry: commandName.entrySet()) {
out.add(entry.getValue(), commandExecute.get(entry.getKey()).compileCompleter().apply(entry.getValue()));
Expand Down
4 changes: 2 additions & 2 deletions builtins/src/main/java/org/jline/builtins/Less.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2019, the original author or authors.
* Copyright (c) 2002-2020, 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.
Expand Down Expand Up @@ -36,9 +36,9 @@
import org.jline.builtins.Nano.SyntaxHighlighter;
import org.jline.builtins.Source.ResourceSource;
import org.jline.builtins.Source.URLSource;
import org.jline.console.ConfigurationPath;
import org.jline.keymap.BindingReader;
import org.jline.keymap.KeyMap;
import org.jline.reader.ConfigurationPath;
import org.jline.terminal.Attributes;
import org.jline.terminal.Size;
import org.jline.terminal.Terminal;
Expand Down
4 changes: 2 additions & 2 deletions builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2019, the original author or authors.
* Copyright (c) 2002-2020, 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.
Expand Down Expand Up @@ -42,9 +42,9 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jline.console.ConfigurationPath;
import org.jline.keymap.BindingReader;
import org.jline.keymap.KeyMap;
import org.jline.reader.ConfigurationPath;
import org.jline.reader.Editor;
import org.jline.terminal.Attributes;
import org.jline.terminal.Attributes.ControlChar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.HashMap;
import java.util.Map;

import org.jline.console.CmdDesc;
import org.jline.reader.Completer;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedStringBuilder;
Expand Down Expand Up @@ -62,7 +63,7 @@ public interface SystemRegistry extends CommandRegistry {
* @return command description for JLine TailTipWidgets to be displayed
* in the terminal status bar.
*/
Widgets.CmdDesc commandDescription(Widgets.CmdLine line);
CmdDesc commandDescription(Widgets.CmdLine line);

/**
* Execute a command, script or evaluate scriptEngine statement
Expand Down
19 changes: 11 additions & 8 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@
import org.jline.builtins.Widgets;
import org.jline.builtins.Builtins.CommandMethods;
import org.jline.builtins.Options.HelpException;
import org.jline.console.CmdDesc;
import org.jline.console.ConfigurationPath;
import org.jline.reader.*;
import org.jline.reader.Parser.ParseContext;
import org.jline.reader.impl.completer.AggregateCompleter;
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.reader.impl.completer.SystemCompleter;
import org.jline.terminal.Attributes;
import org.jline.terminal.Attributes.InputFlag;
import org.jline.utils.*;
Expand Down Expand Up @@ -211,9 +214,9 @@ private boolean isCommandOrScript(String command) {
}

@Override
public Completers.SystemCompleter compileCompleters() {
Completers.SystemCompleter out = CommandRegistry.aggregateCompleters(commandRegistries);
Completers.SystemCompleter local = new Completers.SystemCompleter();
public SystemCompleter compileCompleters() {
SystemCompleter out = CommandRegistry.aggregateCompleters(commandRegistries);
SystemCompleter local = new SystemCompleter();
for (String command : commandExecute.keySet()) {
if (subcommands.containsKey(command)) {
for(Map.Entry<String,List<Completer>> entry : subcommands.get(command).compileCompleters().getCompleters().entrySet()) {
Expand Down Expand Up @@ -252,7 +255,7 @@ public Completer completer() {
return new AggregateCompleter(completers);
}

private Widgets.CmdDesc localCommandDescription(String command) {
private CmdDesc localCommandDescription(String command) {
if (!isLocalCommand(command)) {
throw new IllegalArgumentException();
}
Expand All @@ -268,8 +271,8 @@ private Widgets.CmdDesc localCommandDescription(String command) {
}

@Override
public Widgets.CmdDesc commandDescription(List<String> args) {
Widgets.CmdDesc out = new Widgets.CmdDesc(false);
public CmdDesc commandDescription(List<String> args) {
CmdDesc out = new CmdDesc(false);
String command = args.get(0);
int id = registryId(command);
if (id > -1) {
Expand All @@ -283,8 +286,8 @@ public Widgets.CmdDesc commandDescription(List<String> args) {
}

@Override
public Widgets.CmdDesc commandDescription(Widgets.CmdLine line) {
Widgets.CmdDesc out = null;
public CmdDesc commandDescription(Widgets.CmdLine line) {
CmdDesc out = null;
switch (line.getDescriptionType()) {
case COMMAND:
String cmd = parser.getCommand(line.getArgs().get(0));
Expand Down
Loading

0 comments on commit da71da6

Please sign in to comment.