small refactoring

This commit is contained in:
marhali 2024-04-10 00:25:31 +02:00
parent 8ddc7ff6ac
commit 71e1106423
9 changed files with 30 additions and 23 deletions

View File

@ -7,6 +7,7 @@
### Added ### Added
- Support for IntelliJ Platform version 2024.1 - Support for IntelliJ Platform version 2024.1
- "Localize It" action to extract translations based on current selection. Thanks to @JPilson
### Changed ### Changed

View File

@ -7,10 +7,12 @@ import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import de.marhali.easyi18n.dialog.AddDialog; import de.marhali.easyi18n.dialog.AddDialog;
import de.marhali.easyi18n.model.KeyPath; import de.marhali.easyi18n.model.KeyPath;
import de.marhali.easyi18n.settings.ProjectSettingsService; import de.marhali.easyi18n.settings.ProjectSettingsService;
import de.marhali.easyi18n.util.DocumentUtil; import de.marhali.easyi18n.util.DocumentUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@ -22,29 +24,33 @@ class LocalizeItAction extends AnAction {
public void actionPerformed(@NotNull AnActionEvent anActionEvent) { public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
DataContext dataContext = anActionEvent.getDataContext(); DataContext dataContext = anActionEvent.getDataContext();
Editor editor = CommonDataKeys.EDITOR.getData(dataContext); Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
if (editor == null) return;
if (editor == null) {
return;
}
String text = editor.getSelectionModel().getSelectedText(); String text = editor.getSelectionModel().getSelectedText();
if (text == null || text.isEmpty()) return; if (text == null || text.isEmpty()) {
return;
}
if ((text.startsWith("\"") && text.endsWith("\"")) || (text.startsWith("'") && text.endsWith("'"))) { if ((text.startsWith("\"") && text.endsWith("\"")) || (text.startsWith("'") && text.endsWith("'"))) {
text = text.substring(1); text = text.substring(1);
text = text.substring(0, text.length() - 1); text = text.substring(0, text.length() - 1);
} }
Project project = anActionEvent.getProject(); Project project = anActionEvent.getProject();
if (project == null) { if (project == null) {
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(text), text, (key) -> replaceSelectedText(project, editor, key));
dialog.showAndHandle(); dialog.showAndHandle();
} }
/** /**
* 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.
* *
@ -74,4 +80,4 @@ class LocalizeItAction extends AnAction {
return flavorTemplate + "(\"" + key + "\")"; return flavorTemplate + "(\"" + key + "\")";
} }
} }

View File

@ -61,9 +61,13 @@ public class AddDialog extends TranslationDialog {
@Override @Override
protected @Nullable TranslationUpdate handleExit(int exitCode) { protected @Nullable TranslationUpdate handleExit(int exitCode) {
if(exitCode == DialogWrapper.OK_EXIT_CODE) { if(exitCode == DialogWrapper.OK_EXIT_CODE) {
if(onCreated != null) onCreated.accept(this.getKeyField().getText()); if(onCreated != null) {
onCreated.accept(this.getKeyField().getText());
}
return new TranslationCreate(getState()); return new TranslationCreate(getState());
} }
return null; return null;
} }
} }

View File

@ -37,10 +37,6 @@ abstract class TranslationDialog extends DialogWrapper {
protected final @NotNull KeyPathConverter converter; protected final @NotNull KeyPathConverter converter;
protected final @NotNull Translation origin; protected final @NotNull Translation origin;
public JTextField getKeyField() {
return keyField;
}
protected final JTextField keyField; protected final JTextField keyField;
protected final Map<String, JTextField> localeValueFields; protected final Map<String, JTextField> localeValueFields;
@ -72,6 +68,10 @@ abstract class TranslationDialog extends DialogWrapper {
} }
} }
public JTextField getKeyField() {
return keyField;
}
/** /**
* Registers a callback that is called on dialog close with the final state. * Registers a callback that is called on dialog close with the final state.
* If the user aborts the dialog no callback is called. * If the user aborts the dialog no callback is called.

View File

@ -26,7 +26,6 @@ import java.util.ResourceBundle;
/** /**
* Configuration panel with all possible options for this plugin. * Configuration panel with all possible options for this plugin.
*
* @author marhali * @author marhali
*/ */
public class ProjectSettingsComponent extends ProjectSettingsComponentState { public class ProjectSettingsComponent extends ProjectSettingsComponentState {
@ -70,7 +69,6 @@ public class ProjectSettingsComponent extends ProjectSettingsComponentState {
.getPanel(); .getPanel();
} }
private JComponent constructPresetField() { private JComponent constructPresetField() {
preset = new ComboBox<>(Preset.values()); preset = new ComboBox<>(Preset.values());
preset.setToolTipText(bundle.getString("settings.preset.tooltip")); preset.setToolTipText(bundle.getString("settings.preset.tooltip"));

View File

@ -91,7 +91,6 @@ public class ProjectSettingsComponentState {
assistance.setSelected(state.isAssistance()); assistance.setSelected(state.isAssistance());
alwaysFold.setSelected(state.isAlwaysFold()); alwaysFold.setSelected(state.isAlwaysFold());
flavorTemplate.setText(state.getFlavorTemplate()); flavorTemplate.setText(state.getFlavorTemplate());
} }
} }

View File

@ -217,6 +217,7 @@ public class ProjectSettingsState implements ProjectSettings {
public void setAlwaysFold(Boolean alwaysFold) { public void setAlwaysFold(Boolean alwaysFold) {
this.alwaysFold = alwaysFold; this.alwaysFold = alwaysFold;
} }
public void setFlavorTemplate(String flavorTemplate){ public void setFlavorTemplate(String flavorTemplate){
this.flavorTemplate = flavorTemplate; this.flavorTemplate = flavorTemplate;
} }

View File

@ -5,10 +5,13 @@ import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFile;
public class DocumentUtil { public class DocumentUtil {
protected Document document; protected Document document;
FileType fileType; private FileType fileType;
public DocumentUtil(Document document) {
setDocument(document);
}
public Document getDocument() { public Document getDocument() {
return document; return document;
@ -23,10 +26,6 @@ public class DocumentUtil {
} }
} }
public DocumentUtil(Document document) {
setDocument(document);
}
public boolean isJsOrTs() { public boolean isJsOrTs() {
return (fileType.getDefaultExtension().contains("js") || fileType.getDescription().contains("ts")); return (fileType.getDefaultExtension().contains("js") || fileType.getDescription().contains("ts"));
} }
@ -34,5 +33,4 @@ public class DocumentUtil {
public boolean isVue() { public boolean isVue() {
return fileType.getDefaultExtension().contains("vue"); return fileType.getDefaultExtension().contains("vue");
} }
} }

View File

@ -61,7 +61,7 @@ settings.experimental.title=Experimental Configuration
settings.experimental.always-fold.title=Always fold translation keys settings.experimental.always-fold.title=Always fold translation keys
settings.experimental.always-fold.tooltip=Forces the editor to always display the value behind a translation key. The value cannot be unfolded when this function is active. settings.experimental.always-fold.tooltip=Forces the editor to always display the value behind a translation key. The value cannot be unfolded when this function is active.
settings.experimental.flavor-template =I18n flavor template settings.experimental.flavor-template =I18n flavor template
settings.experimental.flavor-template-tooltip = Specify How to replace strings with i18n representation. settings.experimental.flavor-template-tooltip = Specify how to replace strings with i18n representation.
error.io=An error occurred while processing translation files. \n\ error.io=An error occurred while processing translation files. \n\
Config: {0} => {1} ({2}) \n\ Config: {0} => {1} ({2}) \n\
Path: {3} \n\ Path: {3} \n\