langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ContinuousCompletionProvider.java
author mcimadamore
Fri, 16 Dec 2016 15:27:34 +0000
changeset 42827 36468b5fa7f4
parent 40765 6f9556cf4404
permissions -rw-r--r--
8181370: Convert anonymous inner classes into lambdas/method references Reviewed-by: jjg, rfield, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
     1
/*
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
     4
 *
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    10
 *
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    15
 * accompanied this code).
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    16
 *
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    20
 *
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    23
 * questions.
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    24
 */
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    25
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    26
package jdk.internal.jshell.tool;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    27
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    28
import java.util.List;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    29
import static java.util.Comparator.comparing;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    30
import java.util.Map;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    31
import java.util.function.BiPredicate;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    32
import java.util.function.Supplier;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    33
import static java.util.stream.Collectors.toList;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    34
import java.util.stream.Stream;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    35
import jdk.internal.jshell.tool.JShellTool.CompletionProvider;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    36
import jdk.jshell.SourceCodeAnalysis;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    37
import jdk.jshell.SourceCodeAnalysis.Suggestion;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    38
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    39
class ContinuousCompletionProvider implements CompletionProvider {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    40
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 40765
diff changeset
    41
    static final BiPredicate<String, String> STARTSWITH_MATCHER = String::startsWith;
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 40765
diff changeset
    42
    static final BiPredicate<String, String> PERFECT_MATCHER = String::equals;
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    43
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    44
    private final Supplier<Map<String, CompletionProvider>> wordCompletionProviderSupplier;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    45
    private final BiPredicate<String, String> matcher;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    46
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    47
    ContinuousCompletionProvider(
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    48
            Map<String, CompletionProvider> wordCompletionProvider,
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    49
            BiPredicate<String, String> matcher) {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    50
        this(() -> wordCompletionProvider, matcher);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    51
    }
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    52
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    53
    ContinuousCompletionProvider(
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    54
            Supplier<Map<String, CompletionProvider>> wordCompletionProviderSupplier,
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    55
            BiPredicate<String, String> matcher) {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    56
        this.wordCompletionProviderSupplier = wordCompletionProviderSupplier;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    57
        this.matcher = matcher;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    58
    }
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    59
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    60
    @Override
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    61
    public List<Suggestion> completionSuggestions(String input, int cursor, int[] anchor) {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    62
        String prefix = input.substring(0, cursor);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    63
        int space = prefix.indexOf(' ');
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    64
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    65
        Stream<SourceCodeAnalysis.Suggestion> result;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    66
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    67
        Map<String, CompletionProvider> wordCompletionProvider = wordCompletionProviderSupplier.get();
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    68
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    69
        if (space == (-1)) {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    70
            result = wordCompletionProvider.keySet().stream()
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    71
                    .distinct()
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    72
                    .filter(key -> key.startsWith(prefix))
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    73
                    .map(key -> new JShellTool.ArgSuggestion(key + " "));
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    74
            anchor[0] = 0;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    75
        } else {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    76
            String rest = prefix.substring(space + 1);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    77
            String word = prefix.substring(0, space);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    78
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    79
            List<CompletionProvider> candidates = wordCompletionProvider.entrySet().stream()
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    80
                    .filter(e -> matcher.test(e.getKey(), word))
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    81
                    .map(Map.Entry::getValue)
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    82
                    .collect(toList());
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    83
            if (candidates.size() == 1) {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    84
                result = candidates.get(0).completionSuggestions(rest, cursor - space - 1, anchor).stream();
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    85
            } else {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    86
                result = Stream.empty();
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    87
            }
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    88
            anchor[0] += space + 1;
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    89
        }
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    90
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    91
        return result.sorted(comparing(Suggestion::continuation))
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    92
                     .collect(toList());
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    93
    }
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    94
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents:
diff changeset
    95
}