generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #388 from marhali/feature/localize-it
Feature/localize it
- Loading branch information
Showing
17 changed files
with
218 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/java/de/marhali/easyi18n/action/LocalizeItAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
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.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; | ||
|
||
/** | ||
* Represents an action to localize text in the editor. | ||
*/ | ||
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; | ||
} | ||
|
||
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. | ||
* | ||
* @param project the project where the editor belongs | ||
* @param editor the editor where the text is selected | ||
* @param key the key used to generate the replacement text | ||
*/ | ||
private void replaceSelectedText(Project project, @NotNull Editor editor, @NotNull String key) { | ||
int selectionStart = editor.getSelectionModel().getSelectionStart(); | ||
int selectionEnd = editor.getSelectionModel().getSelectionEnd(); | ||
String flavorTemplate = ProjectSettingsService.get(project).getState().getFlavorTemplate(); | ||
DocumentUtil documentUtil = new DocumentUtil(editor.getDocument()); | ||
String replacement = buildReplacement(flavorTemplate, key, documentUtil); | ||
WriteCommandAction.runWriteCommandAction(editor.getProject(), () -> documentUtil.getDocument().replaceString(selectionStart, selectionEnd, replacement)); | ||
} | ||
|
||
/** | ||
* Builds a replacement string based on the provided flavor template, key, and document util. | ||
* | ||
* @param flavorTemplate the flavor template string | ||
* @param key the key used to generate the replacement text | ||
* @param documentUtil the document util object used to determine the document type | ||
* @return the built replacement string | ||
*/ | ||
private String buildReplacement(String flavorTemplate, String key, DocumentUtil documentUtil) { | ||
if (documentUtil.isVue() || documentUtil.isJsOrTs()) return flavorTemplate + "('" + key + "')"; | ||
|
||
return flavorTemplate + "(\"" + key + "\")"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package de.marhali.easyi18n.util; | ||
|
||
import com.intellij.openapi.editor.Document; | ||
import com.intellij.openapi.fileEditor.FileDocumentManager; | ||
import com.intellij.openapi.fileTypes.FileType; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
|
||
public class DocumentUtil { | ||
protected Document document; | ||
private FileType fileType; | ||
|
||
public DocumentUtil(Document document) { | ||
setDocument(document); | ||
} | ||
|
||
public Document getDocument() { | ||
return document; | ||
} | ||
|
||
public void setDocument(Document document) { | ||
this.document = document; | ||
FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance(); | ||
VirtualFile virtualFile = fileDocumentManager.getFile(document); | ||
if (virtualFile != null) { | ||
fileType = virtualFile.getFileType(); | ||
} | ||
} | ||
|
||
public boolean isJsOrTs() { | ||
return (fileType.getDefaultExtension().contains("js") || fileType.getDescription().contains("ts")); | ||
} | ||
|
||
public boolean isVue() { | ||
return fileType.getDefaultExtension().contains("vue"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.