Skip to content

Commit

Permalink
Merge pull request #93 from briyanii/feature-syntax-highlight
Browse files Browse the repository at this point in the history
Add support for syntax highlighting and some basic autofill
  • Loading branch information
briyanii committed Oct 16, 2019
2 parents c1080f4 + 0865fd3 commit c98e0a9
Show file tree
Hide file tree
Showing 7 changed files with 487 additions and 18 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Expand Up @@ -64,6 +64,8 @@ dependencies {
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: jUnitVersion

testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: jUnitVersion

compile group: 'org.fxmisc.richtext', name: 'richtextfx', version: '0.10.2'
}

shadowJar {
Expand Down
63 changes: 50 additions & 13 deletions src/main/java/seedu/address/ui/CommandBox.java
@@ -1,11 +1,16 @@
package seedu.address.ui;

import javafx.collections.ObservableList;
import java.util.List;

import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.Prefix;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand All @@ -17,25 +22,38 @@ public class CommandBox extends UiPart<Region> {
private static final String FXML = "CommandBox.fxml";

private final CommandExecutor commandExecutor;
private final SyntaxHighlightTextArea syntaxHighlightTextArea;


@FXML
private TextField commandTextField;
private StackPane commandInputAreaPlaceholder;

public CommandBox(CommandExecutor commandExecutor) {
super(FXML);
this.commandExecutor = commandExecutor;
// calls #setStyleToDefault() whenever there is a change to the text of the command box.
commandTextField.textProperty().addListener((unused1, unused2, unused3) -> setStyleToDefault());
syntaxHighlightTextArea = new SyntaxHighlightTextArea();
syntaxHighlightTextArea.textProperty().addListener((unused1, unused2, unused3) -> setStyleToDefault());
commandInputAreaPlaceholder.getChildren().add(syntaxHighlightTextArea);

syntaxHighlightTextArea.addEventHandler(KeyEvent.KEY_PRESSED, keyEvent -> {
if (keyEvent.getCode().equals(KeyCode.ENTER)) {
handleCommandEntered();
}
});
}

public void importSyntaxStyleSheet(Scene scene) {
syntaxHighlightTextArea.importStyleSheet(scene);
}

/**
* Handles the Enter button pressed event.
*/
@FXML
private void handleCommandEntered() {
try {
commandExecutor.execute(commandTextField.getText());
commandTextField.setText("");
commandExecutor.execute(syntaxHighlightTextArea.getText());
syntaxHighlightTextArea.clear();
} catch (CommandException | ParseException e) {
setStyleToIndicateCommandFailure();
}
Expand All @@ -45,22 +63,41 @@ private void handleCommandEntered() {
* Sets the command box style to use the default style.
*/
private void setStyleToDefault() {
commandTextField.getStyleClass().remove(ERROR_STYLE_CLASS);
// enable syntax highlighting
syntaxHighlightTextArea.enableSyntaxHighlighting();
}

/**
* Sets the command box style to indicate a failed command.
*/
private void setStyleToIndicateCommandFailure() {
ObservableList<String> styleClass = commandTextField.getStyleClass();
//override style and disable syntax highlighting
syntaxHighlightTextArea.overrideStyle(ERROR_STYLE_CLASS);
}

if (styleClass.contains(ERROR_STYLE_CLASS)) {
return;
}
/**
* Adds a command to enable syntax highlighting for
* @param com The command word of the command
* @param pre The prefix of the command
* @param syntax The minimum syntax required
*/
public void enableSyntaxHightlightingForCommand(String com, List<Prefix> pre, String syntax) {
syntaxHighlightTextArea.createPattern(com, pre, syntax);
}

/**
* Disable syntax highlighting for the specified command.
* @param command The command word of the command.
*/
public void disableSyntaxHighlightingForCommand(String command) {
syntaxHighlightTextArea.removePattern(command);
}

styleClass.add(ERROR_STYLE_CLASS);
public void enableSyntaxHighlighting() {
syntaxHighlightTextArea.enableSyntaxHighlighting();
}


/**
* Represents a function that can execute commands.
*/
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/seedu/address/ui/MainWindow.java
@@ -1,5 +1,15 @@
package seedu.address.ui;

import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION;
import static seedu.address.logic.parser.CliSyntax.PREFIX_END_DATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PERIOD;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PRICE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_START_DATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TIMESTAMP;

import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;

import javafx.event.ActionEvent;
Expand Down Expand Up @@ -118,6 +128,33 @@ void fillInnerParts() {
statusbarPlaceholder.getChildren().add(statusBarFooter.getRoot());

CommandBox commandBox = new CommandBox(this::executeCommand);
commandBox.importSyntaxStyleSheet(getRoot().getScene());

// add supported commands (not all yet)
commandBox.enableSyntaxHightlightingForCommand("add",
List.of(PREFIX_PRICE, PREFIX_DESCRIPTION),
"add d/<description_here> p/ <price here>");
commandBox.enableSyntaxHightlightingForCommand("alias",
Collections.emptyList(),
"alias <alias_name> <input>");
commandBox.enableSyntaxHightlightingForCommand("budget",
List.of(PREFIX_DESCRIPTION, PREFIX_PRICE, PREFIX_START_DATE, PREFIX_PERIOD),
"budget d/ <description> p/ <amount> sd/ <start_date> pr/ <period>");
commandBox.enableSyntaxHightlightingForCommand("event",
List.of(PREFIX_DESCRIPTION, PREFIX_PRICE, PREFIX_TAG, PREFIX_TIMESTAMP),
"event d/ <description> p/ <amount> date/ <date>");
commandBox.enableSyntaxHightlightingForCommand("stats",
List.of(PREFIX_DESCRIPTION, PREFIX_START_DATE, PREFIX_END_DATE),
"stats sd/ <start_date> ed/ <end_date>");
commandBox.enableSyntaxHightlightingForCommand("undo",
Collections.emptyList(),
"undo");
commandBox.enableSyntaxHightlightingForCommand("redo",
Collections.emptyList(),
"redo");

commandBox.enableSyntaxHighlighting();

commandBoxPlaceholder.getChildren().add(commandBox.getRoot());
}

Expand Down

0 comments on commit c98e0a9

Please sign in to comment.