diff --git a/src/main/java/seedu/address/logic/injector/NavigationInjector.java b/src/main/java/seedu/address/logic/injector/NavigationInjector.java index 3335f3097ed..7303c9c2853 100644 --- a/src/main/java/seedu/address/logic/injector/NavigationInjector.java +++ b/src/main/java/seedu/address/logic/injector/NavigationInjector.java @@ -27,33 +27,39 @@ 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"); - // If command is whitelisted, return unmodified user input. - if (isCommandWhitelisted(commandWord)) { - return commandText; + commandText = removeRootPrefix(commandText); + return injectMissingArgs(commandText, arguments, model).trim(); + } + + private boolean shouldIgnoreCommand(Matcher matcher) { + if (!matcher.matches()) { + return true; } - final String arguments = matcher.group("arguments"); + final String commandWord = matcher.group("commandWord"); + if (isCommandWhitelisted(commandWord) || !isValidCommandWord(commandWord)) { + return true; + } + + 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(); - commandText = removeRootPrefix(commandText); - // Don't inject context prefixes if only root prefix present. if (hasRootPrefix && !hasLecturePrefix) { return commandText; @@ -82,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)) {