Skip to content

Commit

Permalink
refactor: clean up TogglesConfig interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Noorts committed Feb 10, 2025
1 parent 3f825c5 commit 6f72ff4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 83 deletions.
9 changes: 5 additions & 4 deletions src/main/java/core/AppSettingsComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void promptResetButtonAction() {
public void executeResetButtonAction() {
SettingsState settingsState = AppSettings.getInstance().getState();
settingsState.resetSettingsToDefault();
setJsonText(JsonParser.toJson(settingsState.toggles.getToggles()));
setJsonText(settingsState.toggles.toString());
setPartialMatchingCheckboxStatus(settingsState.isPartialMatchingEnabled());
}

Expand All @@ -86,7 +86,8 @@ private void importTogglesFromJsonFile() {
// Load and parse the contents of the JSON file and set the toggles to
// the loaded toggles.
try {
settingsState.toggles.setToggles(JsonParser.parseJsonToToggles(FileHandler.loadContentFromFileToString()));
String fileContent = FileHandler.loadContentFromFileToString();
settingsState.toggles.overwriteToggles(fileContent);
} catch (JsonParser.TogglesFormatException e) {
setStatusErrorMessage(e.getMessage());
return;
Expand All @@ -100,15 +101,15 @@ private void importTogglesFromJsonFile() {

// Reset the settings menu JsonText textarea to the toggles that have
// been loaded.
setJsonText(JsonParser.toJson(settingsState.toggles.getToggles()));
setJsonText(settingsState.toggles.toString());
setStatusMessage("Importing toggles from file was successful.");
}

private void exportTogglesToJsonFile() {
SettingsState settingsState = AppSettings.getInstance().getState();
// Save the toggles to a file in JSON format.
try {
FileHandler.saveTextToDisk(JsonParser.toJson(settingsState.toggles.getToggles()));
FileHandler.saveTextToDisk(settingsState.toggles.toString());
} catch (FileHandler.FileSelectionCancelledException e) {
setStatusErrorMessage("No file was saved, exporting toggles failed.");
return;
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/core/AppSettingsConfigurable.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public JComponent createComponent() {
@Override
public boolean isModified() {
SettingsState settings = AppSettings.getInstance().getState();
boolean modified = !mySettingsComponent.getJsonText().equals(JsonParser.toJson(settings.toggles.getToggles()));
boolean modified = !mySettingsComponent.getJsonText().equals(settings.toggles.toString());
modified |= mySettingsComponent.getPartialMatchingCheckboxStatus() != settings.isPartialMatchingEnabled();
return modified;
}
Expand All @@ -51,15 +51,13 @@ public void apply() {
/* Set whether the partial matching functionality is enabled. */
settings.setPartialMatchingIsEnabled(mySettingsComponent.getPartialMatchingCheckboxStatus());

List<List<String>> currentSettingsFromMenu = JsonParser.parseJsonToToggles(
mySettingsComponent.getJsonText());
settings.toggles.setToggles(currentSettingsFromMenu);
settings.toggles.overwriteToggles(mySettingsComponent.getJsonText());

/* Set the JsonTextarea in the settings menu to the toggles saved to
* the plugin. The side effect is that eventual errors entered by
* the user that aren't included by the JsonParser are removed from
* the textarea input as the input is forcefully reset. */
mySettingsComponent.setJsonText(JsonParser.toJson(currentSettingsFromMenu));
mySettingsComponent.setJsonText(settings.toggles.toString());
mySettingsComponent.setStatusMessage("Saving was successful.");
} catch (JsonParser.TogglesFormatException e) {
mySettingsComponent.setStatusErrorMessage(e.getMessage());
Expand All @@ -71,7 +69,7 @@ public void apply() {
@Override
public void reset() {
SettingsState settings = AppSettings.getInstance().getState();
mySettingsComponent.setJsonText(JsonParser.toJson(settings.toggles.getToggles()));
mySettingsComponent.setJsonText(settings.toggles.toString());
mySettingsComponent.setPartialMatchingCheckboxStatus(settings.isPartialMatchingEnabled());
mySettingsComponent.setStatusMessage("Loaded previous settings.");
}
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/core/TogglerStructureConverter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package core;

import com.intellij.notification.NotificationType;
import com.intellij.util.xmlb.Converter;
import org.jetbrains.annotations.NotNull;
import utils.JsonParser;
import utils.NotificationHandler;

/**
* A converter used by the AppSettingsState to write and load the state of the
Expand All @@ -10,10 +13,17 @@
*/
class TogglerStructureConverter extends Converter<TogglesConfig> {
public TogglesConfig fromString(@NotNull String togglesString) {
return TogglesConfig.deserialize(togglesString);
try {
return new TogglesConfig(togglesString);
} catch (JsonParser.TogglesFormatException e) {
NotificationHandler.notify("The toggles couldn't be parsed from the " +
"plugin settings storage successfully.",
NotificationType.ERROR);
return null;
}
}

public String toString(@NotNull TogglesConfig togglesConfig) {
return togglesConfig.serialize();
return togglesConfig.toString();
}
}
44 changes: 19 additions & 25 deletions src/main/java/core/TogglesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,29 @@
public class TogglesConfig {
private List<List<String>> toggles;

/**
* Instantiated with default toggles.
*/
public TogglesConfig() {
resetTogglesToDefault();
}

/**
* Parses and loads provided toggles.
*/
public TogglesConfig(@NotNull String togglesString) throws JsonParser.TogglesFormatException {
this.overwriteToggles(togglesString);
}

@Override
public String toString() {
return JsonParser.toJson(this.toggles);
}

public void overwriteToggles(@NotNull String togglesString) throws JsonParser.TogglesFormatException {
this.toggles = JsonParser.parseJsonToToggles(togglesString);
}

public void resetTogglesToDefault() {
try {
toggles = JsonParser.parseJsonToToggles(Constants.DEFAULT_TOGGLES);
Expand Down Expand Up @@ -88,29 +107,4 @@ public String createRegexPatternOfToggles() {
stringBuilder.append(")");
return stringBuilder.toString();
}

public static TogglesConfig deserialize(@NotNull String togglesString) {
TogglesConfig config = new TogglesConfig();
try {
config.toggles = JsonParser.parseJsonToToggles(togglesString);
return config;
} catch (JsonParser.TogglesFormatException e) {
NotificationHandler.notify("The toggles couldn't be parsed from the " +
"plugin setting storage successfully.",
NotificationType.ERROR);
return null;
}
}

public String serialize() {
return JsonParser.toJson(this.toggles);
}

public List<List<String>> getToggles() {
return toggles;
}

public void setToggles(List<List<String>> toggles) {
this.toggles = toggles;
}
}
71 changes: 25 additions & 46 deletions src/test/java/core/ToggleActionTest.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
package core;

import org.junit.Test;
import utils.JsonParser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class ToggleActionTest {
@Test
public void regexPatternIsCorrectlyCreatedFromTheTogglePairs() {
public void regexPatternIsCorrectlyCreatedFromTheTogglePairs() throws JsonParser.TogglesFormatException {
// Arrange
List<String> smallTogglePair = new ArrayList<>(Arrays.asList("add", "remove"));
List<String> longTogglePair = new ArrayList<>(Arrays.asList("addClass", "removeClass"));
List<List<String>> toggleActionStructure = new ArrayList<>(Arrays.asList(smallTogglePair, longTogglePair));
String toggles = "[[\"add\", \"remove\"], [\"addClass\", \"removeClass\"]]";

// Act
TogglesConfig togglesConfig = new TogglesConfig();
togglesConfig.setToggles(toggleActionStructure);
TogglesConfig togglesConfig = new TogglesConfig(toggles);
String regexPattern = togglesConfig.createRegexPatternOfToggles();

// Assert
Expand All @@ -28,14 +25,11 @@ public void regexPatternIsCorrectlyCreatedFromTheTogglePairs() {
}

@Test
public void fullMatchIsFoundCorrectlyWithPartialMatchingEnabled() {
public void fullMatchIsFoundCorrectlyWithPartialMatchingEnabled() throws JsonParser.TogglesFormatException {
// Arrange
List<String> smallTogglePair = new ArrayList<>(Arrays.asList("add", "remove"));
List<String> longTogglePair = new ArrayList<>(Arrays.asList("addClass", "removeClass"));
List<List<String>> toggleActionStructure = new ArrayList<>(Arrays.asList(smallTogglePair, longTogglePair));
String toggles = "[[\"add\", \"remove\"], [\"addClass\", \"removeClass\"]]";
ToggleAction newToggleAction = new ToggleAction();
TogglesConfig togglesConfig = new TogglesConfig();
togglesConfig.setToggles(toggleActionStructure);
TogglesConfig togglesConfig = new TogglesConfig(toggles);
String regexPattern = togglesConfig.createRegexPatternOfToggles();

String input = "addClass";
Expand All @@ -51,13 +45,11 @@ public void fullMatchIsFoundCorrectlyWithPartialMatchingEnabled() {
}

@Test
public void partialMatchIsFoundCorrectlyWithPartialMatchingEnabled() {
public void partialMatchIsFoundCorrectlyWithPartialMatchingEnabled() throws JsonParser.TogglesFormatException {
// Arrange
List<String> smallTogglePair = new ArrayList<>(Arrays.asList("add", "remove"));
List<List<String>> toggleActionStructure = new ArrayList<>(Collections.singletonList(smallTogglePair));
String toggles = "[[\"add\", \"remove\"]]";
ToggleAction newToggleAction = new ToggleAction();
TogglesConfig togglesConfig = new TogglesConfig();
togglesConfig.setToggles(toggleActionStructure);
TogglesConfig togglesConfig = new TogglesConfig(toggles);
String regexPattern = togglesConfig.createRegexPatternOfToggles();

String input = "addClass";
Expand All @@ -73,14 +65,11 @@ public void partialMatchIsFoundCorrectlyWithPartialMatchingEnabled() {
}

@Test
public void fullMatchIsFoundCorrectlyWithPartialMatchingDisabled() {
public void fullMatchIsFoundCorrectlyWithPartialMatchingDisabled() throws JsonParser.TogglesFormatException {
// Arrange
List<String> smallTogglePair = new ArrayList<>(Arrays.asList("add", "remove"));
List<String> longTogglePair = new ArrayList<>(Arrays.asList("addClass", "removeClass"));
List<List<String>> toggleActionStructure = new ArrayList<>(Arrays.asList(smallTogglePair, longTogglePair));
String toggles = "[[\"add\", \"remove\"], [\"addClass\", \"removeClass\"]]";
ToggleAction newToggleAction = new ToggleAction();
TogglesConfig togglesConfig = new TogglesConfig();
togglesConfig.setToggles(toggleActionStructure);
TogglesConfig togglesConfig = new TogglesConfig(toggles);
String regexPattern = togglesConfig.createRegexPatternOfToggles();

String input = "addClass";
Expand All @@ -96,13 +85,11 @@ public void fullMatchIsFoundCorrectlyWithPartialMatchingDisabled() {
}

@Test
public void partialMatchIsNotFoundCorrectlyWithPartialMatchingDisabled() {
public void partialMatchIsNotFoundCorrectlyWithPartialMatchingDisabled() throws JsonParser.TogglesFormatException {
// Arrange
List<String> smallTogglePair = new ArrayList<>(Arrays.asList("add", "remove"));
List<List<String>> toggleActionStructure = new ArrayList<>(Collections.singletonList(smallTogglePair));
String toggles = "[[\"add\", \"remove\"]]";
ToggleAction newToggleAction = new ToggleAction();
TogglesConfig togglesConfig = new TogglesConfig();
togglesConfig.setToggles(toggleActionStructure);
TogglesConfig togglesConfig = new TogglesConfig(toggles);
String regexPattern = togglesConfig.createRegexPatternOfToggles();

String input = "addClass";
Expand All @@ -117,14 +104,11 @@ public void partialMatchIsNotFoundCorrectlyWithPartialMatchingDisabled() {
}

@Test
public void partialMatchUnderCaretIsFoundCorrectly() {
public void partialMatchUnderCaretIsFoundCorrectly() throws JsonParser.TogglesFormatException {
// Arrange
List<String> firstTogglePair = new ArrayList<>(Arrays.asList("add", "remove"));
List<String> secondTogglePair = new ArrayList<>(Arrays.asList("class", "interface"));
List<List<String>> toggleActionStructure = new ArrayList<>(Arrays.asList(firstTogglePair, secondTogglePair));
String toggles = "[[\"add\", \"remove\"], [\"class\", \"interface\"]]";
ToggleAction newToggleAction = new ToggleAction();
TogglesConfig togglesConfig = new TogglesConfig();
togglesConfig.setToggles(toggleActionStructure);
TogglesConfig togglesConfig = new TogglesConfig(toggles);
String regexPattern = togglesConfig.createRegexPatternOfToggles();

String input = "addClass";
Expand All @@ -147,13 +131,11 @@ public void partialMatchUnderCaretIsFoundCorrectly() {
}

@Test
public void partialMatchIsNotFoundCorrectlyWhenCaretPositionIsOutsideOfIt() {
public void partialMatchIsNotFoundCorrectlyWhenCaretPositionIsOutsideOfIt() throws JsonParser.TogglesFormatException {
// Arrange
List<String> firstTogglePair = new ArrayList<>(Arrays.asList("add", "remove"));
List<List<String>> toggleActionStructure = new ArrayList<>(Collections.singletonList(firstTogglePair));
String toggles = "[[\"add\", \"remove\"]]";
ToggleAction newToggleAction = new ToggleAction();
TogglesConfig togglesConfig = new TogglesConfig();
togglesConfig.setToggles(toggleActionStructure);
TogglesConfig togglesConfig = new TogglesConfig(toggles);
String regexPattern = togglesConfig.createRegexPatternOfToggles();

String input = "addClass";
Expand All @@ -174,14 +156,11 @@ public void partialMatchIsNotFoundCorrectlyWhenCaretPositionIsOutsideOfIt() {
}

@Test
public void ifThereArePartialMatchesOnEitherSideOfTheCaretThenTheOneOnTheRightIsCorrectlyReturned() {
public void ifThereArePartialMatchesOnEitherSideOfTheCaretThenTheOneOnTheRightIsCorrectlyReturned() throws JsonParser.TogglesFormatException {
// Arrange
List<String> smallTogglePair = new ArrayList<>(Arrays.asList("lov", "add"));
List<String> longTogglePair = new ArrayList<>(Arrays.asList("ely", "remove"));
List<List<String>> toggleActionStructure = new ArrayList<>(Arrays.asList(smallTogglePair, longTogglePair));
String toggles = "[[\"lov\", \"add\"], [\"ely\", \"remove\"]]";
ToggleAction newToggleAction = new ToggleAction();
TogglesConfig togglesConfig = new TogglesConfig();
togglesConfig.setToggles(toggleActionStructure);
TogglesConfig togglesConfig = new TogglesConfig(toggles);
String regexPattern = togglesConfig.createRegexPatternOfToggles();

String input = "Lovely";
Expand Down

0 comments on commit 6f72ff4

Please sign in to comment.