From cd7748fe67b1bfb0da64d365deb267c2fa6c05b3 Mon Sep 17 00:00:00 2001 From: Jedidiah Cheng Date: Sun, 9 Apr 2023 10:14:17 +0800 Subject: [PATCH 1/2] Refactor NavigationInjector --- .../logic/injector/NavigationInjector.java | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/main/java/seedu/address/logic/injector/NavigationInjector.java b/src/main/java/seedu/address/logic/injector/NavigationInjector.java index 3335f3097ed..26b9ae3deaa 100644 --- a/src/main/java/seedu/address/logic/injector/NavigationInjector.java +++ b/src/main/java/seedu/address/logic/injector/NavigationInjector.java @@ -27,27 +27,34 @@ public class NavigationInjector extends Injector { public String inject(String commandText, Model model) { final Matcher matcher = TrackerParser.BASIC_COMMAND_FORMAT.matcher(commandText.trim()); - // If input does not match syntax, return unmodified user input. - if (!matcher.matches()) { + if (shouldIgnoreCommand(matcher)) { return commandText; } - final String commandWord = matcher.group("commandWord"); + final String arguments = matcher.group("arguments"); + + return injectMissingArgs(commandText, arguments, model).trim(); + } - // If command is whitelisted, return unmodified user input. + private boolean shouldIgnoreCommand(Matcher matcher) { + if (!matcher.matches()) { + return true; + } + + final String commandWord = matcher.group("commandWord"); if (isCommandWhitelisted(commandWord)) { - return commandText; + return true; } - final String arguments = matcher.group("arguments"); + return false; + } + + private String injectMissingArgs(String commandText, String arguments, Model model) { + NavigationContext navContext = model.getCurrentNavContext(); ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(arguments, PREFIX_ROOT, PREFIX_MODULE, PREFIX_LECTURE); - NavigationContext navContext = model.getCurrentNavContext(); - return injectMissingArgs(commandText, argMultimap, navContext).trim(); - } - private String injectMissingArgs(String commandText, ArgumentMultimap argMultimap, NavigationContext navContext) { boolean hasRootPrefix = argMultimap.getValue(PREFIX_ROOT).isPresent(); boolean hasLecturePrefix = argMultimap.getValue(PREFIX_LECTURE).isPresent(); boolean hasModulePrefix = argMultimap.getValue(PREFIX_MODULE).isPresent(); From f4fe5e9ad677622f25e241d2ed1fb708e910dcfb Mon Sep 17 00:00:00 2001 From: Jedidiah Cheng Date: Sun, 9 Apr 2023 10:35:15 +0800 Subject: [PATCH 2/2] Fix bug when only /r is input as a command --- .../seedu/address/logic/injector/NavigationInjector.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/address/logic/injector/NavigationInjector.java b/src/main/java/seedu/address/logic/injector/NavigationInjector.java index 26b9ae3deaa..7303c9c2853 100644 --- a/src/main/java/seedu/address/logic/injector/NavigationInjector.java +++ b/src/main/java/seedu/address/logic/injector/NavigationInjector.java @@ -33,6 +33,7 @@ public String inject(String commandText, Model model) { final String arguments = matcher.group("arguments"); + commandText = removeRootPrefix(commandText); return injectMissingArgs(commandText, arguments, model).trim(); } @@ -42,7 +43,7 @@ private boolean shouldIgnoreCommand(Matcher matcher) { } final String commandWord = matcher.group("commandWord"); - if (isCommandWhitelisted(commandWord)) { + if (isCommandWhitelisted(commandWord) || !isValidCommandWord(commandWord)) { return true; } @@ -59,8 +60,6 @@ private String injectMissingArgs(String commandText, String arguments, Model mod boolean hasLecturePrefix = argMultimap.getValue(PREFIX_LECTURE).isPresent(); boolean hasModulePrefix = argMultimap.getValue(PREFIX_MODULE).isPresent(); - commandText = removeRootPrefix(commandText); - // Don't inject context prefixes if only root prefix present. if (hasRootPrefix && !hasLecturePrefix) { return commandText; @@ -89,6 +88,10 @@ private String injectLecturePrefixArg(String commandText, NavigationContext navC return commandText + " " + navContext.getLecturePrefixArg(); } + private boolean isValidCommandWord(String commandWord) { + return !commandWord.matches(PREFIX_ROOT.toString()); + } + private boolean isCommandWhitelisted(String commandWord) { for (var w : WHITELIST) { if (commandWord.equals(w)) {