langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java
changeset 40498 f54048be4a57
parent 40304 0318f4e75c6d
child 40516 9e0e107c39dd
equal deleted inserted replaced
40320:2e83d21d78cd 40498:f54048be4a57
   921         public List<Suggestion> completionSuggestions(String input, int cursor, int[] anchor) {
   921         public List<Suggestion> completionSuggestions(String input, int cursor, int[] anchor) {
   922             List<Suggestion> result = new ArrayList<>();
   922             List<Suggestion> result = new ArrayList<>();
   923 
   923 
   924             for (String alternative : alternatives) {
   924             for (String alternative : alternatives) {
   925                 if (alternative.startsWith(input)) {
   925                 if (alternative.startsWith(input)) {
   926                     result.add(new Suggestion(alternative, false));
   926                     result.add(new ArgSuggestion(alternative));
   927                 }
   927                 }
   928             }
   928             }
   929 
   929 
   930             anchor[0] = 0;
   930             anchor[0] = 0;
   931 
   931 
   949             String prefix = lastSlash != (-1) ? code.substring(lastSlash + 1) : code;
   949             String prefix = lastSlash != (-1) ? code.substring(lastSlash + 1) : code;
   950             Path current = toPathResolvingUserHome(path);
   950             Path current = toPathResolvingUserHome(path);
   951             List<Suggestion> result = new ArrayList<>();
   951             List<Suggestion> result = new ArrayList<>();
   952             try (Stream<Path> dir = Files.list(current)) {
   952             try (Stream<Path> dir = Files.list(current)) {
   953                 dir.filter(f -> accept.test(f) && f.getFileName().toString().startsWith(prefix))
   953                 dir.filter(f -> accept.test(f) && f.getFileName().toString().startsWith(prefix))
   954                    .map(f -> new Suggestion(f.getFileName() + (Files.isDirectory(f) ? "/" : ""), false))
   954                    .map(f -> new ArgSuggestion(f.getFileName() + (Files.isDirectory(f) ? "/" : "")))
   955                    .forEach(result::add);
   955                    .forEach(result::add);
   956             } catch (IOException ex) {
   956             } catch (IOException ex) {
   957                 //ignore...
   957                 //ignore...
   958             }
   958             }
   959             if (path.isEmpty()) {
   959             if (path.isEmpty()) {
   960                 StreamSupport.stream(FileSystems.getDefault().getRootDirectories().spliterator(), false)
   960                 StreamSupport.stream(FileSystems.getDefault().getRootDirectories().spliterator(), false)
   961                              .filter(root -> accept.test(root) && root.toString().startsWith(prefix))
   961                              .filter(root -> accept.test(root) && root.toString().startsWith(prefix))
   962                              .map(root -> new Suggestion(root.toString(), false))
   962                              .map(root -> new ArgSuggestion(root.toString()))
   963                              .forEach(result::add);
   963                              .forEach(result::add);
   964             }
   964             }
   965             anchor[0] = path.length();
   965             anchor[0] = path.length();
   966             return result;
   966             return result;
   967         };
   967         };
   979             return snippetsSupplier.get()
   979             return snippetsSupplier.get()
   980                         .flatMap(k -> (k instanceof DeclarationSnippet)
   980                         .flatMap(k -> (k instanceof DeclarationSnippet)
   981                                 ? Stream.of(String.valueOf(k.id()), ((DeclarationSnippet) k).name())
   981                                 ? Stream.of(String.valueOf(k.id()), ((DeclarationSnippet) k).name())
   982                                 : Stream.of(String.valueOf(k.id())))
   982                                 : Stream.of(String.valueOf(k.id())))
   983                         .filter(k -> k.startsWith(prefix))
   983                         .filter(k -> k.startsWith(prefix))
   984                         .map(k -> new Suggestion(k, false))
   984                         .map(k -> new ArgSuggestion(k))
   985                         .collect(Collectors.toList());
   985                         .collect(Collectors.toList());
   986         };
   986         };
   987     }
   987     }
   988 
   988 
   989     private CompletionProvider snippetKeywordCompletion(Supplier<Stream<? extends Snippet>> snippetsSupplier) {
   989     private CompletionProvider snippetKeywordCompletion(Supplier<Stream<? extends Snippet>> snippetsSupplier) {
  1144                              .stream()
  1144                              .stream()
  1145                              .distinct()
  1145                              .distinct()
  1146                              .filter(cmd -> cmd.kind.shouldSuggestCompletions)
  1146                              .filter(cmd -> cmd.kind.shouldSuggestCompletions)
  1147                              .map(cmd -> cmd.command)
  1147                              .map(cmd -> cmd.command)
  1148                              .filter(key -> key.startsWith(prefix))
  1148                              .filter(key -> key.startsWith(prefix))
  1149                              .map(key -> new Suggestion(key + " ", false));
  1149                              .map(key -> new ArgSuggestion(key + " "));
  1150             anchor[0] = 0;
  1150             anchor[0] = 0;
  1151         } else {
  1151         } else {
  1152             String arg = prefix.substring(space + 1);
  1152             String arg = prefix.substring(space + 1);
  1153             String cmd = prefix.substring(0, space);
  1153             String cmd = prefix.substring(0, space);
  1154             Command[] candidates = findCommand(cmd, c -> true);
  1154             Command[] candidates = findCommand(cmd, c -> true);
  2455             this.snippet = snippet;
  2455             this.snippet = snippet;
  2456             this.space = space;
  2456             this.space = space;
  2457             this.tid = tid;
  2457             this.tid = tid;
  2458         }
  2458         }
  2459     }
  2459     }
       
  2460 
       
  2461     private static class ArgSuggestion implements Suggestion {
       
  2462 
       
  2463         private final String continuation;
       
  2464 
       
  2465         /**
       
  2466          * Create a {@code Suggestion} instance.
       
  2467          *
       
  2468          * @param continuation a candidate continuation of the user's input
       
  2469          */
       
  2470         public ArgSuggestion(String continuation) {
       
  2471             this.continuation = continuation;
       
  2472         }
       
  2473 
       
  2474         /**
       
  2475          * The candidate continuation of the given user's input.
       
  2476          *
       
  2477          * @return the continuation string
       
  2478          */
       
  2479         @Override
       
  2480         public String continuation() {
       
  2481             return continuation;
       
  2482         }
       
  2483 
       
  2484         /**
       
  2485          * Indicates whether input continuation matches the target type and is thus
       
  2486          * more likely to be the desired continuation. A matching continuation is
       
  2487          * preferred.
       
  2488          *
       
  2489          * @return {@code false}, non-types analysis
       
  2490          */
       
  2491         @Override
       
  2492         public boolean matchesType() {
       
  2493             return false;
       
  2494         }
       
  2495     }
  2460 }
  2496 }
  2461 
  2497 
  2462 abstract class NonInteractiveIOContext extends IOContext {
  2498 abstract class NonInteractiveIOContext extends IOContext {
  2463 
  2499 
  2464     @Override
  2500     @Override