<commit_before>package seedu.taskman.logic.commands;

import seedu.taskman.commons.core.Messages;
import seedu.taskman.commons.core.UnmodifiableObservableList;
import seedu.taskman.commons.exceptions.IllegalValueException;
import seedu.taskman.logic.parser.CommandParser;
import seedu.taskman.model.event.Activity;
import seedu.taskman.model.event.Deadline;
import seedu.taskman.model.event.Event;
import seedu.taskman.model.event.Frequency;
import seedu.taskman.model.event.Schedule;
import seedu.taskman.model.event.Status;
import seedu.taskman.model.event.Task;
import seedu.taskman.model.event.Title;
import seedu.taskman.model.event.UniqueActivityList;
import seedu.taskman.model.tag.Tag;
import seedu.taskman.model.tag.UniqueTagList;

import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static seedu.taskman.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

/**
 * Edits an existing activity
 */
public class EditCommand extends Command {

    public static final String COMMAND_WORD = "edit";

    public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits an existing activity.\n"
            + "Parameters: INDEX [TITLE] [d/OPTIONAL_DEADLINE] [c/OPTIONAL_STATUS] [s/OPTIONAL_SCHEDULE] [t/OPTIONAL_TAGS]...\n"
            + "Example: " + COMMAND_WORD
            + " 1 CS2103T Tutorial d/fri 11.59pm c/complete s/mon 2200 to tue 0200 t/friends t/owesMoney";

    public static final String MESSAGE_EDIT_EVENT_SUCCESS = "Event updated: %1$s";
    public static final String MESSAGE_EDIT_TASK_SUCCESS = "Task updated: %1$s";
    public static final String MESSAGE_DUPLICATE_ACTIVITY = "An event or a task with the same name already exists";
    private static final Pattern TASK_EDIT_ARGS_FORMAT = // '/' forward slashes are reserved for delimiter prefixes
            Pattern.compile("" + CommandParser.ArgumentPattern.PANEL
                    + CommandParser.ArgumentPattern.TARGET_INDEX
                    + CommandParser.ArgumentPattern.TITLE + "?"
                    + CommandParser.ArgumentPattern.OPTIONAL_DEADLINE
                    + CommandParser.ArgumentPattern.OPTIONAL_STATUS
                    + CommandParser.ArgumentPattern.OPTIONAL_SCHEDULE
                    + CommandParser.ArgumentPattern.OPTIONAL_FREQUENCY
                    + CommandParser.ArgumentPattern.OPTIONAL_TAGS); // variable number of tags

    private final ArgumentContainer argsContainer;
    private Activity beforeEdit;
    private Activity afterEdit;
    private Activity.ActivityType activityType;

    /**
     * Convenience constructor using raw values.
     * Fields which are null are assumed not to be replaced
     *
     * @throws IllegalValueException if any of the raw values are invalid
     */
    private EditCommand(Activity.PanelType panelType, int targetIndex,
                        @Nullable String title, @Nullable String deadline, @Nullable String status,
                        @Nullable String schedule, @Nullable String frequency, @Nullable Set<String> tags) {
        super(true);
        argsContainer = new ArgumentContainer(panelType, targetIndex, title, deadline, status, schedule, frequency, tags);
    }

    public static Command prepareEdit(String args) {
        final Matcher matcher = TASK_EDIT_ARGS_FORMAT.matcher(args.trim());

        if (!matcher.matches()) {
            return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, MESSAGE_USAGE));
        }

        String panelTypeRaw = matcher.group("panel").trim();
        Activity.PanelType panelType = Activity.PanelType.fromString(panelTypeRaw);
        String indexString = matcher.group("targetIndex").trim();
        int index = Integer.parseInt(indexString);

            String tags = matcher.group("tagArguments");
            return new EditCommand(
                    panelType,
                    index,
                    matcher.group("title"),
                    matcher.group("deadline"),
                    matcher.group("status"),
                    matcher.group("schedule"),
                    matcher.group("frequency"),
                    tags.isEmpty() ? null : getTagsFromArgs(tags));
    }

    @Override
    public CommandResult execute() {
        assert model != null;

        try {
            initMembers(argsContainer);
        } catch (IllegalValueException e) {
            return new CommandResult(e.getMessage(), false);
        }

        try {
            model.deleteActivity(beforeEdit);
            model.addActivity(afterEdit);
            switch(activityType) {
                case EVENT: {<commit_msg>Fixes invalid Title bug when only tags are edited<commit_after>package seedu.taskman.logic.commands;

import seedu.taskman.commons.core.Messages;
import seedu.taskman.commons.core.UnmodifiableObservableList;
import seedu.taskman.commons.exceptions.IllegalValueException;
import seedu.taskman.logic.parser.CommandParser;
import seedu.taskman.model.event.Activity;
import seedu.taskman.model.event.Deadline;
import seedu.taskman.model.event.Event;
import seedu.taskman.model.event.Frequency;
import seedu.taskman.model.event.Schedule;
import seedu.taskman.model.event.Status;
import seedu.taskman.model.event.Task;
import seedu.taskman.model.event.Title;
import seedu.taskman.model.event.UniqueActivityList;
import seedu.taskman.model.tag.Tag;
import seedu.taskman.model.tag.UniqueTagList;

import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static seedu.taskman.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

/**
 * Edits an existing activity
 */
public class EditCommand extends Command {

    public static final String COMMAND_WORD = "edit";

    public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits an existing activity.\n"
            + "Parameters: INDEX [TITLE] [d/OPTIONAL_DEADLINE] [c/OPTIONAL_STATUS] [s/OPTIONAL_SCHEDULE] [t/OPTIONAL_TAGS]...\n"
            + "Example: " + COMMAND_WORD
            + " 1 CS2103T Tutorial d/fri 11.59pm c/complete s/mon 2200 to tue 0200 t/friends t/owesMoney";

    public static final String MESSAGE_EDIT_EVENT_SUCCESS = "Event updated: %1$s";
    public static final String MESSAGE_EDIT_TASK_SUCCESS = "Task updated: %1$s";
    public static final String MESSAGE_DUPLICATE_ACTIVITY = "An event or a task with the same name already exists";
    private static final Pattern TASK_EDIT_ARGS_FORMAT = // '/' forward slashes are reserved for delimiter prefixes
            Pattern.compile("" + CommandParser.ArgumentPattern.PANEL
                    + CommandParser.ArgumentPattern.TARGET_INDEX
                    + CommandParser.ArgumentPattern.TITLE + "?"
                    + CommandParser.ArgumentPattern.OPTIONAL_DEADLINE
                    + CommandParser.ArgumentPattern.OPTIONAL_STATUS
                    + CommandParser.ArgumentPattern.OPTIONAL_SCHEDULE
                    + CommandParser.ArgumentPattern.OPTIONAL_FREQUENCY
                    + CommandParser.ArgumentPattern.OPTIONAL_TAGS); // variable number of tags

    private final ArgumentContainer argsContainer;
    private Activity beforeEdit;
    private Activity afterEdit;
    private Activity.ActivityType activityType;

    /**
     * Convenience constructor using raw values.
     * Fields which are null are assumed not to be replaced
     *
     * @throws IllegalValueException if any of the raw values are invalid
     */
    private EditCommand(Activity.PanelType panelType, int targetIndex,
                        @Nullable String title, @Nullable String deadline, @Nullable String status,
                        @Nullable String schedule, @Nullable String frequency, @Nullable Set<String> tags) {
        super(true);
        argsContainer = new ArgumentContainer(panelType, targetIndex, title, deadline, status, schedule, frequency, tags);
    }

    public static Command prepareEdit(String args) {
        final Matcher matcher = TASK_EDIT_ARGS_FORMAT.matcher(args.trim());

        if (!matcher.matches()) {
            return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, MESSAGE_USAGE));
        }

        String panelTypeRaw = matcher.group("panel").trim();
        Activity.PanelType panelType = Activity.PanelType.fromString(panelTypeRaw);

        String indexString = matcher.group("targetIndex").trim();
        int index = Integer.parseInt(indexString);

        // filter out empty string
        // an alternative would be to use non-greedy regex
        String title = matcher.group("title");
        if (title != null && title.trim().isEmpty()) {
            title = null;
        }

        String tags = matcher.group("tagArguments");
        return new EditCommand(
                panelType,
                index,
                title,
                matcher.group("deadline"),
                matcher.group("status"),
                matcher.group("schedule"),
                matcher.group("frequency"),
                tags.isEmpty() ? null : getTagsFromArgs(tags));
    }

    @Override
    public CommandResult execute() {
        assert model != null;

        try {
            initMembers(argsContainer);
        } catch (IllegalValueException e) {
            return new CommandResult(e.getMessage(), false);
        }

        try {
            model.deleteActivity(beforeEdit);
            model.addActivity(afterEdit);
            switch(activityType) {
                case EVENT: {
{'fraction_non_alphanumeric': 0.08908616187989556, 'fraction_numerical': 0.003968668407310705, 'mean_word_length': 2.8197048264858395, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2401897', 'n_tokens_mistral': 2613, 'n_tokens_neox': 2527, 'n_words': 601}