feat: Move NamingConvention to presets package

Moved `NamingConvention` from the settings package to a new settings.presets package. This involved modifying various import statements across the application and updating methods that utilised this enum. Increased the `KeyCaseFormater` minimum and preferred width to 200 for better UI rendering.
This commit is contained in:
JPilson 2024-04-21 10:15:32 +02:00
parent 4c737e25fa
commit 340ab134e5
14 changed files with 54 additions and 24 deletions

View File

@ -1,6 +1,5 @@
package de.marhali.easyi18n.action;
import com.google.common.base.CaseFormat;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
@ -11,7 +10,7 @@ import com.intellij.openapi.project.Project;
import de.marhali.easyi18n.dialog.AddDialog;
import de.marhali.easyi18n.model.KeyPath;
import de.marhali.easyi18n.settings.NamingConvention;
import de.marhali.easyi18n.settings.presets.NamingConvention;
import de.marhali.easyi18n.settings.ProjectSettingsService;
import de.marhali.easyi18n.util.DocumentUtil;
@ -79,10 +78,16 @@ class LocalizeItAction extends AnAction {
*/
private String buildReplacement(String flavorTemplate, String key, DocumentUtil documentUtil) {
if (documentUtil.isVue() || documentUtil.isJsOrTs()) return flavorTemplate + "('" + key + "')";
return flavorTemplate + "(\"" + key + "\")";
}
/**
* Converts a given key to the specified naming convention.
*
* @param key the key to convert
* @param project the project where the key is being converted
* @return the converted key
*/
private String convertKeyToNamingCase(String key, Project project) {
return NamingConvention.convertKeyToConvention(key, ProjectSettingsService.get(project).getState().getCaseFormat());
}

View File

@ -1,9 +1,9 @@
package de.marhali.easyi18n.settings;
import com.google.common.base.CaseFormat;
import de.marhali.easyi18n.io.parser.ParserStrategyType;
import de.marhali.easyi18n.io.folder.FolderStrategyType;
import de.marhali.easyi18n.settings.presets.NamingConvention;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

View File

@ -1,6 +1,5 @@
package de.marhali.easyi18n.settings;
import com.google.common.base.CaseFormat;
import com.intellij.ide.BrowserUtil;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.project.Project;
@ -14,6 +13,7 @@ import com.intellij.util.ui.FormBuilder;
import de.marhali.easyi18n.io.parser.ArrayMapper;
import de.marhali.easyi18n.io.parser.ParserStrategyType;
import de.marhali.easyi18n.settings.presets.NamingConvention;
import de.marhali.easyi18n.settings.presets.Preset;
import javax.swing.*;
@ -233,7 +233,7 @@ public class ProjectSettingsComponent extends ProjectSettingsComponentState {
private JComponent constructKeyCaseFormater() {
KeyCaseFormater = new ComboBox<>(NamingConvention.getEnumNames());
KeyCaseFormater.setToolTipText(bundle.getString("settings.experimental.key-naming-format.tooltip"));
KeyCaseFormater.setMinimumAndPreferredWidth(120);
KeyCaseFormater.setMinimumAndPreferredWidth(200);
return KeyCaseFormater;
}

View File

@ -1,15 +1,14 @@
package de.marhali.easyi18n.settings;
import com.google.common.base.CaseFormat;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import de.marhali.easyi18n.io.parser.ParserStrategyType;
import de.marhali.easyi18n.io.folder.FolderStrategyType;
import de.marhali.easyi18n.settings.presets.NamingConvention;
import de.marhali.easyi18n.settings.presets.Preset;
import javax.swing.*;
import java.util.Objects;
/**
* Mandatory for state management for the project settings component.
@ -72,7 +71,7 @@ public class ProjectSettingsComponentState {
state.setFlavorTemplate(flavorTemplate.getText());
state.setCaseFormat(NamingConvention.fromSelector(KeyCaseFormater.getSelectedItem().toString()));
state.setCaseFormat(NamingConvention.fromString(KeyCaseFormater.getSelectedItem().toString()));
return state;
}

View File

@ -1,12 +1,12 @@
package de.marhali.easyi18n.settings;
import com.google.common.base.CaseFormat;
import com.intellij.util.xmlb.annotations.Property;
import de.marhali.easyi18n.io.parser.ParserStrategyType;
import de.marhali.easyi18n.io.folder.FolderStrategyType;
import de.marhali.easyi18n.settings.presets.DefaultPreset;
import de.marhali.easyi18n.settings.presets.NamingConvention;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

View File

@ -2,7 +2,6 @@ package de.marhali.easyi18n.settings.presets;
import de.marhali.easyi18n.io.parser.ParserStrategyType;
import de.marhali.easyi18n.io.folder.FolderStrategyType;
import de.marhali.easyi18n.settings.NamingConvention;
import de.marhali.easyi18n.settings.ProjectSettings;
import org.jetbrains.annotations.NotNull;

View File

@ -1,14 +1,18 @@
package de.marhali.easyi18n.settings;
package de.marhali.easyi18n.settings.presets;
import com.google.common.base.CaseFormat;
import java.util.Arrays;
/**
* Enum representing different naming conventions.
* Provides utility methods to convert keys to the specified convention.
*/
public enum NamingConvention {
CAMEL_CASE("Camel Case"),
CAMEL_CASE_UPPERCASE("Camel Case Uppercase"),
CAMEL_CASE_UPPERCASE("Camel Case (Uppercase)"),
SNAKE_CASE("Snake Case"),
SNAKE_CASE_UPPERCASE("Snake Case Uppercase");
SNAKE_CASE_UPPERCASE("Snake Case (Uppercase)");
private final String name;
@ -16,6 +20,11 @@ public enum NamingConvention {
this.name = name;
}
/**
* Retrieves the name of the current instance of the class.
*
* @return the name of the current instance
*/
public String getName() {
return this.name;
}
@ -25,17 +34,38 @@ public enum NamingConvention {
return super.name().toLowerCase();
}
static public NamingConvention fromSelector(String name) {
String formated = name.replace(" ", "_");
return valueOf(formated.toUpperCase());
/**
* Converts a string representation of a naming convention to the corresponding NamingConvention enum value.
*
* @param name the string representation of the naming convention
* @return the corresponding NamingConvention enum value
*/
static public NamingConvention fromString(String name) {
for (NamingConvention value : NamingConvention.values()) {
if (value.getName().equals(name))
return value;
}
return NamingConvention.CAMEL_CASE;
}
/**
* Returns an array of strings representing the names of the enum values in the {@link NamingConvention} enum.
*
* @return an array of strings representing the enum names
*/
static public String[] getEnumNames() {
return Arrays.stream(NamingConvention.values())
.map(NamingConvention::getName)
.toArray(String[]::new);
}
/**
* Converts a given key to the specified naming convention.
*
* @param key the key to convert
* @param convention the naming convention to convert the key to
* @return the converted key
*/
static public String convertKeyToConvention(String key, NamingConvention convention) {
String newKey = key.toLowerCase();
newKey = newKey.replaceAll("\\s+", "_");

View File

@ -1,9 +1,7 @@
package de.marhali.easyi18n.settings.presets;
import com.google.common.base.CaseFormat;
import de.marhali.easyi18n.io.parser.ParserStrategyType;
import de.marhali.easyi18n.io.folder.FolderStrategyType;
import de.marhali.easyi18n.settings.NamingConvention;
import de.marhali.easyi18n.settings.ProjectSettings;
import org.jetbrains.annotations.NotNull;

View File

@ -2,7 +2,6 @@ package de.marhali.easyi18n.settings.presets;
import de.marhali.easyi18n.io.parser.ParserStrategyType;
import de.marhali.easyi18n.io.folder.FolderStrategyType;
import de.marhali.easyi18n.settings.NamingConvention;
import de.marhali.easyi18n.settings.ProjectSettings;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

View File

@ -64,7 +64,6 @@ settings.experimental.flavor-template =I18n flavor template
settings.experimental.flavor-template-tooltip = Specify how to replace strings with i18n representation.
settings.experimental.key-naming-format.title=Key format of extracted translation
settings.experimental.key-naming-format.tooltip=Choose Naming Convention for the keys of extracted translation
settings.experimental.key-naming-format.items=Camel Case;Snake Case
error.io=An error occurred while processing translation files. \n\
Config: {0} => {1} ({2}) \n\

View File

@ -3,7 +3,7 @@ package de.marhali.easyi18n;
import de.marhali.easyi18n.io.parser.ParserStrategyType;
import de.marhali.easyi18n.io.folder.FolderStrategyType;
import de.marhali.easyi18n.model.KeyPath;
import de.marhali.easyi18n.settings.NamingConvention;
import de.marhali.easyi18n.settings.presets.NamingConvention;
import de.marhali.easyi18n.settings.ProjectSettings;
import de.marhali.easyi18n.util.KeyPathConverter;

View File

@ -7,7 +7,7 @@ import de.marhali.easyi18n.io.parser.properties.PropertiesMapper;
import de.marhali.easyi18n.io.parser.properties.SortableProperties;
import de.marhali.easyi18n.model.TranslationData;
import de.marhali.easyi18n.model.KeyPath;
import de.marhali.easyi18n.settings.NamingConvention;
import de.marhali.easyi18n.settings.presets.NamingConvention;
import de.marhali.easyi18n.settings.ProjectSettings;
import de.marhali.easyi18n.util.KeyPathConverter;

View File

@ -4,6 +4,7 @@ import com.intellij.testFramework.fixtures.BasePlatformTestCase;
import com.intellij.util.xmlb.XmlSerializerUtil;
import de.marhali.easyi18n.settings.presets.DefaultPreset;
import de.marhali.easyi18n.settings.presets.NamingConvention;
/**
* Tests for the project settings service itself.

View File

@ -1,9 +1,9 @@
package de.marhali.easyi18n.settings;
import com.google.common.base.CaseFormat;
import de.marhali.easyi18n.io.folder.FolderStrategyType;
import de.marhali.easyi18n.io.parser.ParserStrategyType;
import de.marhali.easyi18n.settings.presets.NamingConvention;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;