Skip to content

Commit

Permalink
Parser: added static method getCommand()
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Nov 18, 2019
1 parent 06eb15f commit e4efe36
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
21 changes: 18 additions & 3 deletions builtins/src/main/java/org/jline/builtins/Completers.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 @@ -1005,7 +1006,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
17 changes: 16 additions & 1 deletion reader/src/main/java/org/jline/reader/Parser.java
Original file line number Diff line number Diff line change
@@ -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

1 comment on commit e4efe36

@mattirn
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes #474

Please sign in to comment.