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
- Support for IntelliJ Platform version 2024.1
- "Localize It" action to extract translations based on current selection. Thanks to @JPilson
### Changed

View File

@ -7,10 +7,12 @@ import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.command.WriteCommandAction;
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 de.marhali.easyi18n.settings.ProjectSettingsService;
import de.marhali.easyi18n.util.DocumentUtil;
import org.jetbrains.annotations.NotNull;
/**
@ -22,29 +24,33 @@ class LocalizeItAction extends AnAction {
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
DataContext dataContext = anActionEvent.getDataContext();
Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
if (editor == null) return;
if (editor == null) {
return;
}
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("'"))) {
text = text.substring(1);
text = text.substring(0, text.length() - 1);
}
Project project = anActionEvent.getProject();
if (project == null) {
throw new RuntimeException("Project is null!");
}
AddDialog dialog = new AddDialog(project, new KeyPath(text), text, (key) -> replaceSelectedText(project, editor, key));
dialog.showAndHandle();
}
/**
* Replaces the selected text in the editor with a new text generated from the provided key.
*

View File

@ -61,9 +61,13 @@ public class AddDialog extends TranslationDialog {
@Override
protected @Nullable TranslationUpdate handleExit(int exitCode) {
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 null;
}
}

View File

@ -37,10 +37,6 @@ abstract class TranslationDialog extends DialogWrapper {
protected final @NotNull KeyPathConverter converter;
protected final @NotNull Translation origin;
public JTextField getKeyField() {
return keyField;
}
protected final JTextField keyField;
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.
* 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.
*
* @author marhali
*/
public class ProjectSettingsComponent extends ProjectSettingsComponentState {
@ -70,7 +69,6 @@ public class ProjectSettingsComponent extends ProjectSettingsComponentState {
.getPanel();
}
private JComponent constructPresetField() {
preset = new ComboBox<>(Preset.values());
preset.setToolTipText(bundle.getString("settings.preset.tooltip"));

View File

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

View File

@ -217,6 +217,7 @@ public class ProjectSettingsState implements ProjectSettings {
public void setAlwaysFold(Boolean alwaysFold) {
this.alwaysFold = alwaysFold;
}
public void setFlavorTemplate(String 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.vfs.VirtualFile;
public class DocumentUtil {
protected Document document;
FileType fileType;
private FileType fileType;
public DocumentUtil(Document document) {
setDocument(document);
}
public Document getDocument() {
return document;
@ -23,10 +26,6 @@ public class DocumentUtil {
}
}
public DocumentUtil(Document document) {
setDocument(document);
}
public boolean isJsOrTs() {
return (fileType.getDefaultExtension().contains("js") || fileType.getDescription().contains("ts"));
}
@ -34,5 +33,4 @@ public class DocumentUtil {
public boolean isVue() {
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.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-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\
Config: {0} => {1} ({2}) \n\
Path: {3} \n\