Skip to content

Commit

Permalink
Merge pull request #399 from JPilson/feature/key_naming_convention
Browse files Browse the repository at this point in the history
Feature/Automated Key Naming Convention Suggestion for Localize-It Action
  • Loading branch information
marhali authored May 31, 2024
2 parents afe8a4f + 4583404 commit 5428ae3
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 41 deletions.
24 changes: 18 additions & 6 deletions src/main/java/de/marhali/easyi18n/action/LocalizeItAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import de.marhali.easyi18n.dialog.AddDialog;
import de.marhali.easyi18n.model.KeyPath;
import de.marhali.easyi18n.settings.presets.NamingConvention;
import de.marhali.easyi18n.settings.ProjectSettingsService;
import de.marhali.easyi18n.util.DocumentUtil;

Expand Down Expand Up @@ -47,16 +48,16 @@ public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
throw new RuntimeException("Project is null!");
}

AddDialog dialog = new AddDialog(project, new KeyPath(text), text, (key) -> replaceSelectedText(project, editor, key));
AddDialog dialog = new AddDialog(project, new KeyPath(convertKeyToNamingCase(text, project)), text, (key) -> replaceSelectedText(project, editor, key));
dialog.showAndHandle();
}

/**
* Replaces the selected text in the editor with a new text generated from the provided key.
*
* @param project the project where the editor belongs
* @param editor the editor where the text is selected
* @param key the key used to generate the replacement text
* @param editor the editor where the text is selected
* @param key the key used to generate the replacement text
*/
private void replaceSelectedText(Project project, @NotNull Editor editor, @NotNull String key) {
int selectionStart = editor.getSelectionModel().getSelectionStart();
Expand All @@ -71,13 +72,24 @@ private void replaceSelectedText(Project project, @NotNull Editor editor, @NotNu
* Builds a replacement string based on the provided flavor template, key, and document util.
*
* @param flavorTemplate the flavor template string
* @param key the key used to generate the replacement text
* @param documentUtil the document util object used to determine the document type
* @param key the key used to generate the replacement text
* @param documentUtil the document util object used to determine the document type
* @return the built replacement string
*/
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());
}

}
46 changes: 36 additions & 10 deletions src/main/java/de/marhali/easyi18n/settings/ProjectSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,61 @@
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;

/**
* API to access the project-specific configuration for this plugin.
*
* @author marhaliu
*/
public interface ProjectSettings {
// Resource Configuration
@Nullable String getLocalesDirectory();
@NotNull FolderStrategyType getFolderStrategy();
@NotNull ParserStrategyType getParserStrategy();
@NotNull String getFilePattern();
@Nullable
String getLocalesDirectory();

@NotNull
FolderStrategyType getFolderStrategy();

@NotNull
ParserStrategyType getParserStrategy();

@NotNull
String getFilePattern();

boolean isIncludeSubDirs();

boolean isSorting();

// Editor Configuration
@Nullable String getNamespaceDelimiter();
@NotNull String getSectionDelimiter();
@Nullable String getContextDelimiter();
@Nullable String getPluralDelimiter();
@Nullable String getDefaultNamespace();
@NotNull String getPreviewLocale();
@Nullable
String getNamespaceDelimiter();

@NotNull
String getSectionDelimiter();

@Nullable
String getContextDelimiter();

@Nullable
String getPluralDelimiter();

@Nullable
String getDefaultNamespace();

@NotNull
String getPreviewLocale();

boolean isNestedKeys();

boolean isAssistance();

// Experimental Configuration
boolean isAlwaysFold();

String getFlavorTemplate();

@NotNull
NamingConvention getCaseFormat();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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.*;
Expand All @@ -26,6 +27,7 @@

/**
* Configuration panel with all possible options for this plugin.
*
* @author marhali
*/
public class ProjectSettingsComponent extends ProjectSettingsComponentState {
Expand Down Expand Up @@ -64,7 +66,9 @@ public ProjectSettingsComponent(Project project) {
.addVerticalGap(24)
.addComponent(new TitledSeparator(bundle.getString("settings.experimental.title")))
.addComponent(constructAlwaysFoldField())
.addVerticalGap(12)
.addLabeledComponent(bundle.getString("settings.experimental.flavor-template"), constructFlavorTemplate(), 1, false)
.addLabeledComponent(bundle.getString("settings.experimental.key-naming-format.title"), constructKeyCaseFormater(), 1, false)
.addComponentFillVertically(new JPanel(), 0)
.getPanel();
}
Expand Down Expand Up @@ -226,6 +230,14 @@ private JComponent constructFlavorTemplate() {
return flavorTemplate;
}

private JComponent constructKeyCaseFormater() {
KeyCaseFormater = new ComboBox<>(NamingConvention.getEnumNames());
KeyCaseFormater.setToolTipText(bundle.getString("settings.experimental.key-naming-format.tooltip"));
KeyCaseFormater.setMinimumAndPreferredWidth(200);
return KeyCaseFormater;
}


private ItemListener handleParserChange() {
return e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

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.*;

/**
* Mandatory for state management for the project settings component.
*
* @author marhali
*/
public class ProjectSettingsComponentState {
Expand Down Expand Up @@ -41,6 +43,7 @@ public class ProjectSettingsComponentState {
protected JCheckBox alwaysFold;

protected JTextField flavorTemplate;
protected ComboBox<String> KeyCaseFormater;

protected ProjectSettingsState getState() {
// Every field needs to provide its state
Expand All @@ -65,8 +68,11 @@ protected ProjectSettingsState getState() {
state.setAssistance(assistance.isSelected());

state.setAlwaysFold(alwaysFold.isSelected());

state.setFlavorTemplate(flavorTemplate.getText());

state.setCaseFormat(NamingConvention.fromString(KeyCaseFormater.getSelectedItem().toString()));

return state;
}

Expand All @@ -92,5 +98,7 @@ protected void setState(ProjectSettings state) {

alwaysFold.setSelected(state.isAlwaysFold());
flavorTemplate.setText(state.getFlavorTemplate());
KeyCaseFormater.setSelectedItem(state.getCaseFormat().getName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,56 @@
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;

import java.util.Objects;

/**
* Represents the project-specific configuration of this plugin.
*
* @author marhali
*/
public class ProjectSettingsState implements ProjectSettings {

// Resource Configuration
@Property private String localesDirectory;
@Property private FolderStrategyType folderStrategy;
@Property private ParserStrategyType parserStrategy;
@Property private String filePattern;

@Property private Boolean includeSubDirs;
@Property private boolean sorting;
@Property
private String localesDirectory;
@Property
private FolderStrategyType folderStrategy;
@Property
private ParserStrategyType parserStrategy;
@Property
private String filePattern;

@Property
private Boolean includeSubDirs;
@Property
private boolean sorting;

// Editor configuration
@Property private String namespaceDelimiter;
@Property private String sectionDelimiter;
@Property private String contextDelimiter;
@Property private String pluralDelimiter;
@Property private String defaultNamespace;
@Property private String previewLocale;

@Property private Boolean nestedKeys;
@Property private Boolean assistance;
@Property
private String namespaceDelimiter;
@Property
private String sectionDelimiter;
@Property
private String contextDelimiter;
@Property
private String pluralDelimiter;
@Property
private String defaultNamespace;
@Property
private String previewLocale;

@Property
private Boolean nestedKeys;
@Property
private Boolean assistance;

// Experimental configuration
@Property private Boolean alwaysFold;
@Property
private Boolean alwaysFold;

/**
* The `flavorTemplate` specifies the format used for replacing strings with their i18n (internationalization) counterparts.
Expand All @@ -47,7 +64,11 @@ public class ProjectSettingsState implements ProjectSettings {
* the specific framework or developers' preferences for handling i18n. The ability to dynamically change this template adds flexibility and customization
* to cater to different i18n handling methods.
*/
@Property private String flavorTemplate;
@Property
private String flavorTemplate;

@Property
private NamingConvention caseFormat;

public ProjectSettingsState() {
this(new DefaultPreset());
Expand Down Expand Up @@ -75,6 +96,7 @@ public ProjectSettingsState(ProjectSettings defaults) {

this.alwaysFold = defaults.isAlwaysFold();
this.flavorTemplate = defaults.getFlavorTemplate();
this.caseFormat = defaults.getCaseFormat();
}

@Override
Expand Down Expand Up @@ -158,6 +180,11 @@ public String getFlavorTemplate() {
return this.flavorTemplate;
}

@Override
public @NotNull NamingConvention getCaseFormat() {
return this.caseFormat;
}

public void setLocalesDirectory(String localesDirectory) {
this.localesDirectory = localesDirectory;
}
Expand Down Expand Up @@ -218,10 +245,15 @@ public void setAlwaysFold(Boolean alwaysFold) {
this.alwaysFold = alwaysFold;
}

public void setFlavorTemplate(String flavorTemplate){
public void setFlavorTemplate(String flavorTemplate) {
this.flavorTemplate = flavorTemplate;
}

public void setCaseFormat(NamingConvention caseFormat) {
this.caseFormat = caseFormat;
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -242,15 +274,16 @@ public boolean equals(Object o) {
&& Objects.equals(nestedKeys, that.nestedKeys)
&& Objects.equals(assistance, that.assistance)
&& Objects.equals(alwaysFold, that.alwaysFold)
&& Objects.equals(flavorTemplate,that.flavorTemplate);
&& Objects.equals(flavorTemplate, that.flavorTemplate)
&& Objects.equals(caseFormat, that.caseFormat);
}

@Override
public int hashCode() {
return Objects.hash(
localesDirectory, folderStrategy, parserStrategy, filePattern, includeSubDirs,
sorting, namespaceDelimiter, sectionDelimiter, contextDelimiter, pluralDelimiter,
defaultNamespace, previewLocale, nestedKeys, assistance, alwaysFold,flavorTemplate
defaultNamespace, previewLocale, nestedKeys, assistance, alwaysFold, flavorTemplate, caseFormat
);
}

Expand All @@ -273,6 +306,7 @@ public String toString() {
", assistance=" + assistance +
", alwaysFold=" + alwaysFold +
", flavorTemplate=" + flavorTemplate +
", caseFormat=" + caseFormat.toString() +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,9 @@ public boolean isAlwaysFold() {
public String getFlavorTemplate() {
return "$i18n.t";
}

@Override
public @NotNull NamingConvention getCaseFormat() {
return NamingConvention.CAMEL_CASE;
}
}
Loading

0 comments on commit 5428ae3

Please sign in to comment.