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();
} }
@ -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);
}
} }