Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor NavigationInjector #416

Merged
merged 2 commits into from
Apr 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions src/main/java/seedu/address/logic/injector/NavigationInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down