8255273: jshell crashes with UnsupportedOperationException: Should not get here.

Reviewed-by: vromero
This commit is contained in:
Jan Lahoda 2021-04-15 16:17:35 +00:00
parent e43aee58e1
commit 325eecbccb
2 changed files with 74 additions and 41 deletions

View File

@ -364,11 +364,49 @@ class ConsoleIOContext extends IOContext {
.distinct()
.count() == 2;
boolean tooManyItems = suggestions.size() > /*in.getAutoprintThreshold()*/AUTOPRINT_THRESHOLD;
CompletionTask ordinaryCompletion =
new OrdinaryCompletionTask(suggestions,
anchor[0],
!command && !doc.isEmpty(),
hasBoth);
CompletionTask ordinaryCompletion;
List<? extends CharSequence> ordinaryCompletionToShow;
if (hasBoth) {
ordinaryCompletionToShow =
suggestions.stream()
.filter(Suggestion::matchesType)
.map(Suggestion::continuation)
.distinct()
.toList();
} else {
ordinaryCompletionToShow =
suggestions.stream()
.map(Suggestion::continuation)
.distinct()
.toList();
}
if (ordinaryCompletionToShow.isEmpty()) {
ordinaryCompletion = new ContinueCompletionTask();
} else {
Optional<String> prefixOpt =
suggestions.stream()
.map(Suggestion::continuation)
.reduce(ConsoleIOContext::commonPrefix);
String prefix =
prefixOpt.orElse("").substring(cursor - anchor[0]);
if (!prefix.isEmpty() && !command) {
//the completion will fill in the prefix, which will invalidate
//the documentation, avoid adding documentation tasks into the
//todo list:
doc = List.of();
}
ordinaryCompletion =
new OrdinaryCompletionTask(ordinaryCompletionToShow,
prefix,
!command && !doc.isEmpty(),
hasBoth);
}
CompletionTask allCompletion = new AllSuggestionsCompletionTask(suggestions, anchor[0]);
todo = new ArrayList<>();
@ -567,17 +605,17 @@ class ConsoleIOContext extends IOContext {
}
private final class OrdinaryCompletionTask implements CompletionTask {
private final List<Suggestion> suggestions;
private final int anchor;
private final List<? extends CharSequence> toShow;
private final String prefix;
private final boolean cont;
private final boolean showSmart;
public OrdinaryCompletionTask(List<Suggestion> suggestions,
int anchor,
public OrdinaryCompletionTask(List<? extends CharSequence> toShow,
String prefix,
boolean cont,
boolean showSmart) {
this.suggestions = suggestions;
this.anchor = anchor;
this.toShow = toShow;
this.prefix = prefix;
this.cont = cont;
this.showSmart = showSmart;
}
@ -589,34 +627,7 @@ class ConsoleIOContext extends IOContext {
@Override
public Result perform(String text, int cursor) throws IOException {
List<? extends CharSequence> toShow;
if (showSmart) {
toShow =
suggestions.stream()
.filter(Suggestion::matchesType)
.map(Suggestion::continuation)
.distinct()
.toList();
} else {
toShow =
suggestions.stream()
.map(Suggestion::continuation)
.distinct()
.toList();
}
if (toShow.isEmpty()) {
return Result.CONTINUE;
}
Optional<String> prefix =
suggestions.stream()
.map(Suggestion::continuation)
.reduce(ConsoleIOContext::commonPrefix);
String prefixStr = prefix.orElse("").substring(cursor - anchor);
in.putString(prefixStr);
in.putString(prefix);
boolean showItems = toShow.size() > 1 || showSmart;
@ -625,7 +636,7 @@ class ConsoleIOContext extends IOContext {
printColumns(toShow);
}
if (!prefixStr.isEmpty())
if (!prefix.isEmpty())
return showItems ? Result.FINISH : Result.SKIP_NOREPAINT;
return cont ? Result.CONTINUE : Result.FINISH;
@ -798,6 +809,19 @@ class ConsoleIOContext extends IOContext {
}
private static class ContinueCompletionTask implements ConsoleIOContext.CompletionTask {
@Override
public String description() {
throw new UnsupportedOperationException("Should not get here.");
}
@Override
public CompletionTask.Result perform(String text, int cursor) throws IOException {
return CompletionTask.Result.CONTINUE;
}
}
@Override
public boolean terminalEditorRunning() {
Terminal terminal = in.getTerminal();

View File

@ -23,7 +23,7 @@
/**
* @test
* @bug 8177076 8185426 8189595 8188072 8221759
* @bug 8177076 8185426 8189595 8188072 8221759 8255273
* @modules
* jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
@ -331,4 +331,13 @@ public class ToolTabSnippetTest extends UITesting {
//where:
private final Compiler compiler = new Compiler();
public void testDocumentationAfterInsert() throws Exception {
doRunTest((inputSink, out) -> {
inputSink.write("import java.time.*\n");
waitOutput(out, PROMPT);
inputSink.write("new Instant" + TAB);
waitOutput(out, PROMPT + "new InstantiationE");
});
}
}