From 7038e4205579235c3aa8b3587b2530964b5dd3da Mon Sep 17 00:00:00 2001 From: JPilson Date: Sat, 9 Dec 2023 15:01:47 +0100 Subject: [PATCH] feat(Localize Selected): add LocalizeItAction for localizing selected text This commit introduces a new action, `LocalizeItAction`, into the easyi18n toolset, which allows users to automatically apply localization to a selected string in the text editor. The action is now accessible from the editor's context menu. This provides a more efficient way to localize selected strings compared to manually adding them. --- .../easyi18n/action/LocalizeItAction.java | 43 +++++++++++++++++++ src/main/resources/META-INF/plugin.xml | 7 +++ 2 files changed, 50 insertions(+) create mode 100644 src/main/java/de/marhali/easyi18n/action/LocalizeItAction.java diff --git a/src/main/java/de/marhali/easyi18n/action/LocalizeItAction.java b/src/main/java/de/marhali/easyi18n/action/LocalizeItAction.java new file mode 100644 index 0000000..7591bc6 --- /dev/null +++ b/src/main/java/de/marhali/easyi18n/action/LocalizeItAction.java @@ -0,0 +1,43 @@ +package de.marhali.easyi18n.action; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import de.marhali.easyi18n.dialog.AddDialog; +import de.marhali.easyi18n.model.KeyPath; +import org.jetbrains.annotations.NotNull; + +/** + * The LocalizeItAction class represents an IntelliJ IDEA action that localizes selected text. + * + *

When this action is performed, it retrieves the selected text from the editor, checks if it is not empty, + * and then displays a dialog to add the selected text as a localized string key to the project. + * + *

If the selected text is empty or the project is null, the action does nothing. + * + *

This class extends the AnAction class provided by IntelliJ IDEA. + */ +class LocalizeItAction extends AnAction { + + @Override + public void actionPerformed(@NotNull AnActionEvent anActionEvent) { + DataContext dataContext = anActionEvent.getDataContext(); + Editor editor = CommonDataKeys.EDITOR.getData(dataContext); + if (editor == null) + return; + String text = editor.getSelectionModel().getSelectedText(); + if (text == null || text.isEmpty()) + return; + + Project project = anActionEvent.getProject(); + if (project == null) { + throw new RuntimeException("Project is null!"); + } + + AddDialog dialog = new AddDialog(project, new KeyPath(text), text); + dialog.showAndHandle(); + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 45f9611..1553229 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -22,6 +22,13 @@ > + + +