Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cli auto completion #8529

Merged
merged 3 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions client/trino-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
52 changes: 26 additions & 26 deletions client/trino-cli/src/main/java/io/trino/cli/Completion.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,42 @@
*/
package io.trino.cli;

import com.google.common.collect.ImmutableSet;
import org.jline.reader.Completer;
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 java.util.Set;
import java.util.List;

import static java.util.Locale.ENGLISH;
import static java.util.stream.Collectors.toSet;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;

public final class Completion
{
private static final Set<String> COMMANDS = ImmutableSet.of(
"SELECT",
"SHOW CATALOGS",
"SHOW COLUMNS",
"SHOW FUNCTIONS",
"SHOW SCHEMAS",
"SHOW SESSION",
"SHOW TABLES",
"CREATE TABLE",
"DROP TABLE",
"EXPLAIN",
"DESCRIBE",
"USE",
"HELP",
"QUIT");

private Completion() {}

public static Completer commandCompleter()
{
return new StringsCompleter(ImmutableSet.<String>builder()
.addAll(COMMANDS)
.addAll(COMMANDS.stream()
.map(value -> value.toLowerCase(ENGLISH))
.collect(toSet()))
.build());
return new AggregateCompleter(buildArgumentCompleter("CREATE", singletonList("TABLE")),
Copy link
Member

Choose a reason for hiding this comment

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

Can we have also SCHEMA? Can we also have other statements too like ALTER TABLE/SCHEMA

Copy link
Member Author

Choose a reason for hiding this comment

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

I initially kept the list of commands as it was before, but sure. I added those you suggested.

buildArgumentCompleter("DESCRIBE"),
buildArgumentCompleter("DROP", singletonList("TABLE")),
buildArgumentCompleter("EXPLAIN"),
buildArgumentCompleter("HELP"),
buildArgumentCompleter("QUIT"),
buildArgumentCompleter("SELECT"),
buildArgumentCompleter("SHOW", asList("CATALOGS", "COLUMNS", "FUNCTIONS", "SCHEMAS", "SESSION", "TABLES")),
buildArgumentCompleter("USE"));
}

private static Completer buildArgumentCompleter(String command)
{
// NullCompleter is used to indicate the command is complete and the last word should not be repeated
return new ArgumentCompleter(new StringsCompleter(command), NullCompleter.INSTANCE);
}

private static Completer buildArgumentCompleter(String command, List<String> options)
{
return new ArgumentCompleter(new StringsCompleter(command), new StringsCompleter(options), NullCompleter.INSTANCE);
}
}
3 changes: 2 additions & 1 deletion client/trino-cli/src/main/java/io/trino/cli/InputParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static com.google.common.base.CharMatcher.whitespace;
import static io.trino.cli.Console.STATEMENT_DELIMITERS;
import static java.util.Locale.ENGLISH;
import static org.jline.reader.Parser.ParseContext.COMPLETE;

public class InputParser
implements Parser
Expand All @@ -37,7 +38,7 @@ public ParsedLine parse(String line, int cursor, ParseContext context)
throws SyntaxError
{
String command = whitespace().trimFrom(line);
if (command.isEmpty() || SPECIAL.contains(command.toLowerCase(ENGLISH))) {
if (command.isEmpty() || SPECIAL.contains(command.toLowerCase(ENGLISH)) || context == COMPLETE) {
return new DefaultParser().parse(line, cursor, context);
}

Expand Down
34 changes: 34 additions & 0 deletions client/trino-cli/src/test/java/io/trino/cli/TestInputParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.cli;

import org.jline.reader.EOFError;
import org.testng.annotations.Test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.jline.reader.Parser.ParseContext.ACCEPT_LINE;
import static org.jline.reader.Parser.ParseContext.COMPLETE;
import static org.testng.Assert.assertNotNull;

public class TestInputParser
{
@Test
public void testParseIncompleteStatements()
{
InputParser instance = new InputParser();
// assert an incomplete statement throws an error if the ParseContext is not COMPLETE
assertThatThrownBy(() -> instance.parse("show", 4, ACCEPT_LINE)).isInstanceOf(EOFError.class);
assertNotNull(instance.parse("show", 4, COMPLETE));
}
}