Skip to content

Commit

Permalink
Merge 784a341 into cc554fd
Browse files Browse the repository at this point in the history
  • Loading branch information
jonchan51 committed Nov 9, 2019
2 parents cc554fd + 784a341 commit 5e37fc0
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package seedu.weme.logic.parser.contextparser;

import static seedu.weme.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.weme.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND;

import java.util.regex.Matcher;

import seedu.weme.logic.commands.Command;
import seedu.weme.logic.commands.generalcommand.HelpCommand;
import seedu.weme.logic.commands.generalcommand.RedoCommand;
import seedu.weme.logic.commands.generalcommand.UndoCommand;
import seedu.weme.logic.parser.exceptions.ParseException;

/**
* Parses user input in the view context.
*/
public class ViewParser extends WemeParser {

/**
* Parses user input into command for execution.
*
* @param userInput full user input string
* @return the command based on the user input
* @throws ParseException if the user input does not conform the expected format
*/
@Override
public Command parseCommand(String userInput) throws ParseException {
final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim());
if (!matcher.matches()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE));
}

final String commandWord = matcher.group(COMMAND_WORD);
final String arguments = matcher.group(ARGUMENTS);
switch (commandWord) {
case UndoCommand.COMMAND_WORD:
case RedoCommand.COMMAND_WORD:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
// override Undo and Redo command to prevent user from undoing in view context.

default:
return super.parseCommand(userInput);
}
}
}
5 changes: 4 additions & 1 deletion src/main/java/seedu/weme/logic/parser/util/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import seedu.weme.logic.parser.contextparser.ImportParser;
import seedu.weme.logic.parser.contextparser.MemeParser;
import seedu.weme.logic.parser.contextparser.TemplateParser;
import seedu.weme.logic.parser.contextparser.ViewParser;
import seedu.weme.logic.parser.contextparser.WemeParser;
import seedu.weme.logic.parser.exceptions.ParseException;
import seedu.weme.model.ModelContext;
Expand Down Expand Up @@ -50,6 +51,7 @@ public class ParserUtil {
public static final ImportParser IMPORT_PARSER = new ImportParser();
public static final ExportParser EXPORT_PARSER = new ExportParser();
public static final CreateParser CREATE_PARSER = new CreateParser();
public static final ViewParser VIEW_PARSER = new ViewParser();

/**
* Returns a Parser depending on the given ModelContext.
Expand All @@ -69,8 +71,9 @@ public static WemeParser forContext(ModelContext modelContext) {
return TEMPLATE_PARSER;
case CONTEXT_CREATE:
return CREATE_PARSER;
case CONTEXT_STATISTICS:
case CONTEXT_VIEW:
return VIEW_PARSER;
case CONTEXT_STATISTICS:
case CONTEXT_PREFERENCES:
// TODO: This is a temporary placeholder until all tabs have been implemented
return new WemeParser() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package seedu.weme.logic.prompter.contextprompter;

import static seedu.weme.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND;
import static seedu.weme.logic.parser.contextparser.WemeParser.ARGUMENTS;
import static seedu.weme.logic.parser.contextparser.WemeParser.BASIC_COMMAND_FORMAT;
import static seedu.weme.logic.parser.contextparser.WemeParser.COMMAND_WORD;
import static seedu.weme.logic.prompter.util.PrompterUtil.GENERAL_COMMANDS;
import static seedu.weme.logic.prompter.util.PrompterUtil.VIEW_COMMANDS;
import static seedu.weme.logic.prompter.util.PrompterUtil.VIEW_COMMANDS_DESCRIPTION_MAP;
import static seedu.weme.logic.prompter.util.PrompterUtil.promptCommandWord;

import java.util.regex.Matcher;

import seedu.weme.logic.commands.generalcommand.RedoCommand;
import seedu.weme.logic.commands.generalcommand.UndoCommand;
import seedu.weme.logic.prompter.exceptions.PromptException;
import seedu.weme.logic.prompter.prompt.CommandPrompt;
import seedu.weme.model.Model;

/**
* Prompt command arguments in the view context.
*/
public class ViewPrompter extends WemePrompter {

@Override
public CommandPrompt promptCommand(Model model, String userInput) throws PromptException {
final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim());
if (!matcher.matches()) {
return new CommandPrompt(VIEW_COMMANDS
.stream()
.sorted()
.map(command -> VIEW_COMMANDS_DESCRIPTION_MAP.get(command))
.reduce((x, y) -> x + '\n' + y)
.orElse(""),
VIEW_COMMANDS.stream().sorted().findFirst().orElse(""));
}

final String commandWord = matcher.group(COMMAND_WORD);
final String arguments = matcher.group(ARGUMENTS);

// Ignore if trying to do undo or redo
if (GENERAL_COMMANDS.contains(commandWord) && !commandWord.equals(UndoCommand.COMMAND_WORD)
&& !commandWord.equals(RedoCommand.COMMAND_WORD)) {
return super.promptCommand(model, userInput);
}

if (arguments.isBlank()) {
return promptCommandWord(VIEW_COMMANDS, commandWord, VIEW_COMMANDS_DESCRIPTION_MAP);
} else {
throw new PromptException(MESSAGE_UNKNOWN_COMMAND);
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/seedu/weme/logic/prompter/util/PrompterUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import seedu.weme.logic.prompter.contextprompter.PreferencePrompter;
import seedu.weme.logic.prompter.contextprompter.StatisticsPrompter;
import seedu.weme.logic.prompter.contextprompter.TemplatePrompter;
import seedu.weme.logic.prompter.contextprompter.ViewPrompter;
import seedu.weme.logic.prompter.contextprompter.WemePrompter;
import seedu.weme.logic.prompter.exceptions.PromptException;
import seedu.weme.logic.prompter.prompt.CommandPrompt;
Expand Down Expand Up @@ -237,6 +238,18 @@ public class PrompterUtil {
public static final Map<String, String> PREFERENCES_COMMANDS_DESCRIPTION_MAP = new HashMap<>(
GENERAL_COMMANDS_DESCRIPTION_MAP);

public static final Set<String> VIEW_COMMANDS = new HashSet<>() {{
addAll(GENERAL_COMMANDS);
remove(UndoCommand.COMMAND_WORD);
remove(RedoCommand.COMMAND_WORD);
}};

public static final Map<String, String> VIEW_COMMANDS_DESCRIPTION_MAP = new HashMap<>() {{
putAll(GENERAL_COMMANDS_DESCRIPTION_MAP);
remove(UndoCommand.COMMAND_WORD);
remove(RedoCommand.COMMAND_WORD);
}};

public static final String X_COORDINATE_PROMPT = "0.2\n0.4\n0.6\n0.8";
public static final String X_COORDINATE_AUTO_COMPLETION = "0.2";
public static final String Y_COORDINATE_PROMPT = "0.2\n0.4\n0.6\n0.8";
Expand All @@ -253,6 +266,7 @@ public class PrompterUtil {
public static final WemePrompter EXPORT_PROMPTER = new ExportPrompter();
public static final WemePrompter IMPORT_PROMPTER = new ImportPrompter();
public static final WemePrompter PREFERENCE_PROMPTER = new PreferencePrompter();
public static final WemePrompter VIEW_PROMPTER = new ViewPrompter();

/**
* Returns a Prompter depending on the given ModelContext.
Expand All @@ -275,6 +289,8 @@ public static WemePrompter forContext(ModelContext modelContext) {
return STATISTICS_PROMPTER;
case CONTEXT_PREFERENCES:
return PREFERENCE_PROMPTER;
case CONTEXT_VIEW:
return VIEW_PROMPTER;
default:
throw new IllegalArgumentException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MemeViewCommandParserTest {
private MemeViewCommandParser parser = new MemeViewCommandParser();

@Test
public void parse_validArgs_returnsStageCommand() {
public void parse_validArgs_returnsViewCommand() {
assertParseSuccess(parser, "1", new MemeViewCommand(INDEX_FIRST));
}

Expand Down

0 comments on commit 5e37fc0

Please sign in to comment.