Skip to content

Commit

Permalink
Merge branch 'command-assignment'
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Nov 19, 2019
2 parents 4c73a52 + 0b99819 commit ed0d946
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 27 deletions.
21 changes: 18 additions & 3 deletions builtins/src/main/java/org/jline/builtins/Completers.java
Expand Up @@ -31,11 +31,13 @@
import org.jline.reader.Candidate;
import org.jline.reader.LineReader;
import org.jline.reader.LineReader.Option;
import org.jline.reader.Parser;
import org.jline.reader.impl.completer.AggregateCompleter;
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.reader.ParsedLine;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;

Expand Down Expand Up @@ -584,9 +586,22 @@ public void complete(LineReader reader, ParsedLine commandLine, List<Candidate>
assert candidates != null;
if (commandLine.words().size() > 0) {
if (commandLine.words().size() == 1) {
commands.complete(reader, commandLine, candidates);
} else if (command(commandLine.words().get(0)) != null) {
completers.get(command(commandLine.words().get(0))).get(0).complete(reader, commandLine, candidates);
String buffer = commandLine.words().get(0);
int eq = buffer.indexOf('=');
if (eq < 0) {
commands.complete(reader, commandLine, candidates);
} else {
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 = Parser.getCommand(commandLine.words().get(0));
if (command(cmd) != null) {
completers.get(command(cmd)).get(0).complete(reader, commandLine, candidates);
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion builtins/src/main/java/org/jline/builtins/Widgets.java
Expand Up @@ -25,6 +25,7 @@
import org.jline.reader.Binding;
import org.jline.reader.Buffer;
import org.jline.reader.LineReader;
import org.jline.reader.Parser;
import org.jline.reader.LineReader.SuggestionType;
import org.jline.reader.Parser.ParseContext;
import org.jline.reader.Reference;
Expand Down Expand Up @@ -1017,7 +1018,7 @@ private Pair<String,Boolean> evaluateCommandLine(String line, List<String> args,
} else {
if (line.length() == curPos) {
cmd = args != null && (args.size() > 1 || (args.size() == 1
&& line.endsWith(" "))) ? args.get(0) : null;
&& line.endsWith(" "))) ? Parser.getCommand(args.get(0)) : null;
descType = CmdLine.DescriptionType.COMMAND;
}
int brackets = 0;
Expand Down
40 changes: 18 additions & 22 deletions builtins/src/test/java/org/jline/example/Example.java
Expand Up @@ -20,16 +20,13 @@
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.regex.Pattern;
import java.util.function.Supplier;

import org.jline.builtins.Builtins;
import org.jline.builtins.Builtins.Command;
import org.jline.builtins.Commands;
import org.jline.builtins.Completers;
import org.jline.builtins.Completers.TreeCompleter;
import org.jline.builtins.Completers.SystemCompleter;
import org.jline.builtins.Options.HelpException;
import org.jline.builtins.TTop;
import org.jline.builtins.Widgets.AutopairWidgets;
import org.jline.builtins.Widgets.AutosuggestionWidgets;
import org.jline.builtins.Widgets.TailTipWidgets;
Expand Down Expand Up @@ -179,7 +176,12 @@ CmdDesc commandDescription(CmdLine line) {
CmdDesc out = null;
switch (line.getDescriptionType()) {
case COMMAND:
out = builtins.commandDescription(line.getArgs().get(0));
String cmd = Parser.getCommand(line.getArgs().get(0));
if (builtins.hasCommand(cmd)) {
out = builtins.commandDescription(cmd);
} else {
// other commands...
}
break;
case METHOD:
out = methodDescription(line);
Expand Down Expand Up @@ -465,15 +467,15 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
.build();

builtins.setLineReader(reader);
DescriptionGenerator descritionGenerator = new DescriptionGenerator(builtins);
DescriptionGenerator descriptionGenerator = new DescriptionGenerator(builtins);
readerOptions.setReader(reader);
AutopairWidgets autopairWidgets = new AutopairWidgets(reader);
AutosuggestionWidgets autosuggestionWidgets = new AutosuggestionWidgets(reader);
TailTipWidgets tailtipWidgets = null;
if (argument) {
tailtipWidgets = new TailTipWidgets(reader, compileTailTips(), 5, TipType.COMPLETER);
} else {
tailtipWidgets = new TailTipWidgets(reader, descritionGenerator::commandDescription, 5, TipType.COMPLETER);
tailtipWidgets = new TailTipWidgets(reader, descriptionGenerator::commandDescription, 5, TipType.COMPLETER);
}

if (timer) {
Expand Down Expand Up @@ -537,20 +539,14 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
}
ParsedLine pl = reader.getParser().parse(line, 0);
String[] argv = pl.words().subList(1, pl.words().size()).toArray(new String[0]);
if ("help".equals(pl.word()) || "?".equals(pl.word())) {
String cmd = Parser.getCommand(pl.word());
if ("help".equals(cmd) || "?".equals(cmd)) {
help();
}
else if (builtins.hasCommand(pl.word())) {
builtins.execute(pl.word(), argv, System.in, System.out, System.err);
}
else if ("tmux".equals(pl.word())) {
Commands.tmux(terminal, System.out, System.err,
null, //Supplier<Object> getter,
null, //Consumer<Object> setter,
null, //Consumer<Terminal> runner,
argv);
else if (builtins.hasCommand(cmd)) {
builtins.execute(cmd, argv, System.in, System.out, System.err);
}
else if ("tput".equals(pl.word())) {
else if ("tput".equals(cmd)) {
if (argv.length == 1) {
Capability vcap = Capability.byName(argv[0]);
if (vcap != null) {
Expand All @@ -562,7 +558,7 @@ else if ("tput".equals(pl.word())) {
terminal.writer().println("Usage: tput <capability>");
}
}
else if ("testkey".equals(pl.word())) {
else if ("testkey".equals(cmd)) {
terminal.writer().write("Input the key event(Enter to complete): ");
terminal.writer().flush();
StringBuilder sb = new StringBuilder();
Expand All @@ -574,22 +570,22 @@ else if ("testkey".equals(pl.word())) {
terminal.writer().println(KeyMap.display(sb.toString()));
terminal.writer().flush();
}
else if ("cls".equals(pl.word())) {
else if ("cls".equals(cmd)) {
terminal.puts(Capability.clear_screen);
terminal.flush();
}
else if ("sleep".equals(pl.word())) {
else if ("sleep".equals(cmd)) {
Thread.sleep(3000);
}
else if ("autopair".equals(pl.word())) {
else if ("autopair".equals(cmd)) {
terminal.writer().print("Autopair widgets are ");
if (autopairWidgets.toggle()) {
terminal.writer().println("enabled.");
} else {
terminal.writer().println("disabled.");
}
}
else if ("autosuggestion".equals(pl.word())) {
else if ("autosuggestion".equals(cmd)) {
if (argv.length > 0) {
String type = argv[0].toLowerCase();
if (type.startsWith("his")) {
Expand Down
17 changes: 16 additions & 1 deletion reader/src/main/java/org/jline/reader/Parser.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.
Expand All @@ -8,6 +8,9 @@
*/
package org.jline.reader;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public interface Parser {

ParsedLine parse(String line, int cursor, ParseContext context) throws SyntaxError;
Expand All @@ -20,6 +23,18 @@ default boolean isEscapeChar(char ch) {
return ch == '\\';
}

static String getCommand(final String line) {
String out = null;
Pattern patternCommand = Pattern.compile("^\\s*[a-zA-Z]{1,}[a-zA-Z0-9]*=([a-zA-Z]{1,}[a-zA-Z0-9]*)(\\s+|$)");
Matcher matcher = patternCommand.matcher(line);
if (matcher.find()) {
out = matcher.group(1);
} else {
out = line.trim().split("\\s+")[0];
}
return out;
}

enum ParseContext {
UNSPECIFIED,

Expand Down

0 comments on commit ed0d946

Please sign in to comment.