PakhomovAlexander commented on code in PR #1363:
URL: https://github.com/apache/ignite-3/pull/1363#discussion_r1032338569


##########
modules/cli/src/main/java/org/apache/ignite/internal/cli/core/repl/completer/DynamicCompleterRegistry.java:
##########
@@ -38,60 +40,97 @@ public class DynamicCompleterRegistry {
     public List<DynamicCompleter> findCompleters(String[] words) {
         return completionStrategiesList.stream()
                 .filter(strategy -> strategy.canBeApplied(words))
-                .map(CompletionStrategy::completer)
+                .map(strategy -> strategy.completer(words))
                 .collect(Collectors.toList());
     }
 
-    public void register(DynamicCompleter completer) {
-        completionStrategiesList.add(new CompletionStrategy(ignored -> true, 
completer));
-    }
-
     /** Registers dynamic completer that can be found by given predicate. */
-    public void register(Predicate<String[]> predicate, DynamicCompleter 
completer) {
-        completionStrategiesList.add(new CompletionStrategy(predicate, 
completer));
-    }
+    public void register(CompleterConf conf, DynamicCompleterFactory factory) {
+        if (conf.isExclusiveEnableOptions()) {
+            // add disable option for all strategies because current 
configuration has exclusive enable option
+            completionStrategiesList.forEach(strategy -> 
strategy.exclusiveDisableOptions.addAll(conf.enableOptions()));
+        }
 
-    /** Registers dynamic completer that can be found by given prefix. */
-    public void register(String[] prefixWords, DynamicCompleter completer) {
-        register((String[] words) -> samePrefix(words, prefixWords), 
completer);
-    }
+        Set<String> exclusiveDisableOptions = completionStrategiesList.stream()
+                .filter(strategy -> strategy.conf.isExclusiveEnableOptions())
+                .flatMap(strategy -> strategy.conf.enableOptions().stream())
+                .collect(Collectors.toSet());
 
-    /** Registers dynamic completer that can be found by given prefix. */
-    public void register(String[] prefixWords, String[] stopPostfixWords, 
DynamicCompleter completer) {
-        register((String[] words) -> samePrefix(words, prefixWords) && 
notSamePostfix(words, stopPostfixWords), completer);
+        CompletionStrategy strategy = new CompletionStrategy(conf, factory);
+        strategy.exclusiveDisableOptions.addAll(exclusiveDisableOptions);
+        completionStrategiesList.add(strategy);
     }
 
-    private boolean samePrefix(String[] words, String[] prefixWords) {
-        if (words.length < prefixWords.length) {
-            return false;
+    private static class CompletionStrategy {
+        private final CompleterConf conf;
+
+        private final DynamicCompleterFactory factory;
+
+        private final Set<String> exclusiveDisableOptions = new HashSet<>();
+
+        private CompletionStrategy(CompleterConf conf, DynamicCompleterFactory 
factory) {
+            this.conf = conf;
+            this.factory = factory;
         }
-        for (int i = 0; i < prefixWords.length; i++) {
-            if (!words[i].equals(prefixWords[i])) {
+
+        private static boolean samePrefix(String[] words, String[] 
prefixWords) {
+            if (words.length < prefixWords.length) {
                 return false;
             }
+            for (int i = 0; i < prefixWords.length; i++) {
+                if (!words[i].equals(prefixWords[i])) {
+                    return false;
+                }
+            }
+            return true;
         }
-        return true;
-    }
-
-    private boolean notSamePostfix(String[] words, String[] stopPostfixWords) {
-        return words.length > 0 && 
!Set.of(stopPostfixWords).contains(findLastNotEmptyWord(words));
-    }
 
-    private static class CompletionStrategy {
-        private final Predicate<String[]> predicate;
-        private final DynamicCompleter completer;
+        boolean canBeApplied(String[] words) {
+            // empty command means can be applied to all words
+            if (!conf.commandSpecific()) {
+                return canBeAppliedCommandMatch(words);
+            }
 
-        private CompletionStrategy(Predicate<String[]> predicate, 
DynamicCompleter completer) {
-            this.predicate = predicate;
-            this.completer = completer;
+            Optional<String[]> commandsMatch = 
conf.commands().stream().filter(command -> samePrefix(words, 
command)).findFirst();
+            return commandsMatch.isPresent() && 
canBeAppliedCommandMatch(words);
         }
 
-        boolean canBeApplied(String[] words) {
-            return predicate.test(words);
+        private boolean canBeAppliedCommandMatch(String[] words) {
+            String cursorWord = words[words.length - 1];
+            String lastNotEmptyWord = findLastNotEmptyWord(words);
+            String preLastNotEmptyWord = 
findLastNotEmptyWordBeforeWordFromEnd(words, lastNotEmptyWord);
+
+            if (cursorWord.equals(lastNotEmptyWord)) {
+                if (exclusiveDisableOptions.contains(lastNotEmptyWord) || 
exclusiveDisableOptions.contains(preLastNotEmptyWord)) {
+                    return false;
+                }
+            } else if (exclusiveDisableOptions.contains(lastNotEmptyWord)) {
+                return false;
+            }
+
+            if (conf.hasEnableOptions()) {
+                if (cursorWord.equals(lastNotEmptyWord)) {
+                    return conf.enableOptions().contains(lastNotEmptyWord) // 
command subcommand --enable-option
+                            || 
conf.enableOptions().contains(preLastNotEmptyWord); // command subcommand 
--enable-option lastWord
+                } else {
+                    return conf.enableOptions().contains(lastNotEmptyWord); // 
command subcommand --enable-option <space>
+                }
+            }
+
+            return !conf.hasDisableOptions()
+
+                    || (conf.hasDisableOptions()
+                    && !cursorWord.equals(lastNotEmptyWord)
+                    && !conf.disableOptions().contains(lastNotEmptyWord))
+
+                    || (conf.hasDisableOptions()
+                    && cursorWord.equals(lastNotEmptyWord)
+                    && !conf.disableOptions().contains(lastNotEmptyWord)
+                    && !conf.disableOptions().contains(preLastNotEmptyWord));

Review Comment:
   This expression is about `disable options` and the above is about `enable 
options` and the logic statement is the opposite, actually. I will rewrite this 
expression with if-else chain to make them look similar and it will be easier 
to catch the difference.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to