feat: add key naming convention converter

New methods have been added to convert keys to either camel or snake case depending on a boolean flag. These methods are used to format the text that is input into the Add Dialog in 'LocalizeItAction' class. This improves the versatility of key handling within the project.
This commit is contained in:
JPilson 2024-04-16 20:33:05 +02:00
parent 562de99470
commit 08d8326523

View File

@ -1,5 +1,6 @@
package de.marhali.easyi18n.action; package de.marhali.easyi18n.action;
import com.google.common.base.CaseFormat;
import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.CommonDataKeys;
@ -47,7 +48,7 @@ class LocalizeItAction extends AnAction {
throw new RuntimeException("Project is null!"); 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, true)), text, (key) -> replaceSelectedText(project, editor, key));
dialog.showAndHandle(); dialog.showAndHandle();
} }
@ -55,8 +56,8 @@ class LocalizeItAction extends AnAction {
* Replaces the selected text in the editor with a new text generated from the provided key. * 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 project the project where the editor belongs
* @param editor the editor where the text is selected * @param editor the editor where the text is selected
* @param key the key used to generate the replacement text * @param key the key used to generate the replacement text
*/ */
private void replaceSelectedText(Project project, @NotNull Editor editor, @NotNull String key) { private void replaceSelectedText(Project project, @NotNull Editor editor, @NotNull String key) {
int selectionStart = editor.getSelectionModel().getSelectionStart(); int selectionStart = editor.getSelectionModel().getSelectionStart();
@ -71,8 +72,8 @@ class LocalizeItAction extends AnAction {
* Builds a replacement string based on the provided flavor template, key, and document util. * Builds a replacement string based on the provided flavor template, key, and document util.
* *
* @param flavorTemplate the flavor template string * @param flavorTemplate the flavor template string
* @param key the key used to generate the replacement text * @param key the key used to generate the replacement text
* @param documentUtil the document util object used to determine the document type * @param documentUtil the document util object used to determine the document type
* @return the built replacement string * @return the built replacement string
*/ */
private String buildReplacement(String flavorTemplate, String key, DocumentUtil documentUtil) { private String buildReplacement(String flavorTemplate, String key, DocumentUtil documentUtil) {
@ -80,4 +81,19 @@ class LocalizeItAction extends AnAction {
return flavorTemplate + "(\"" + key + "\")"; return flavorTemplate + "(\"" + key + "\")";
} }
private String convertKeyToNamingCase(String key, boolean camelCase) {
String newKey = key.toLowerCase();
return camelCase ? convertKeyToCamelCase(newKey) : convertKeyToSnakeCase(newKey);
}
private String convertKeyToCamelCase(String key) {
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, key);
}
private String convertKeyToSnakeCase(String key) {
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_UNDERSCORE, key);
}
} }