|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.action; |
| 2 | + |
| 3 | +import com.intellij.openapi.actionSystem.AnActionEvent; |
| 4 | +import com.intellij.openapi.actionSystem.DataKey; |
| 5 | +import com.intellij.openapi.actionSystem.PlatformDataKeys; |
| 6 | +import com.intellij.openapi.application.ApplicationManager; |
| 7 | +import com.intellij.openapi.editor.Editor; |
| 8 | +import com.intellij.openapi.project.DumbAwareAction; |
| 9 | +import com.intellij.openapi.project.Project; |
| 10 | +import com.intellij.openapi.util.TextRange; |
| 11 | +import com.intellij.patterns.PlatformPatterns; |
| 12 | +import com.intellij.psi.PsiDocumentManager; |
| 13 | +import com.intellij.psi.PsiElement; |
| 14 | +import com.intellij.psi.PsiFile; |
| 15 | +import com.intellij.psi.tree.IElementType; |
| 16 | +import com.intellij.psi.xml.XmlTokenType; |
| 17 | +import com.jetbrains.twig.TwigFile; |
| 18 | +import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons; |
| 19 | +import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; |
| 20 | +import fr.adrienbrault.idea.symfony2plugin.action.comparator.PsiWeightListComparator; |
| 21 | +import fr.adrienbrault.idea.symfony2plugin.action.dict.TranslationFileModel; |
| 22 | +import fr.adrienbrault.idea.symfony2plugin.translation.dict.TranslationUtil; |
| 23 | +import fr.adrienbrault.idea.symfony2plugin.translation.form.TranslatorKeyExtractorDialog; |
| 24 | +import fr.adrienbrault.idea.symfony2plugin.translation.util.TranslationInsertUtil; |
| 25 | +import fr.adrienbrault.idea.symfony2plugin.util.SymfonyBundleUtil; |
| 26 | +import fr.adrienbrault.idea.symfony2plugin.util.dict.SymfonyBundle; |
| 27 | + |
| 28 | +import java.awt.*; |
| 29 | +import java.util.*; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +public class TwigExtractLanguageAction extends DumbAwareAction { |
| 33 | + |
| 34 | + public TwigExtractLanguageAction() { |
| 35 | + super("Extract Translation", "Extract Translation Key", Symfony2Icons.SYMFONY); |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + public void update(AnActionEvent event) { |
| 40 | + Project project = event.getData(PlatformDataKeys.PROJECT); |
| 41 | + |
| 42 | + if (project == null || !Symfony2ProjectComponent.isEnabled(project)) { |
| 43 | + this.setStatus(event, false); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // only since phpstorm 7.1; PlatformDataKeys.PSI_FILE |
| 48 | + Object psiFile = event.getData(DataKey.create("psi.File")); |
| 49 | + |
| 50 | + if(!(psiFile instanceof TwigFile)) { |
| 51 | + this.setStatus(event, false); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + Editor editor = event.getData(PlatformDataKeys.EDITOR); |
| 56 | + if(editor == null) { |
| 57 | + this.setStatus(event, false); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + PsiElement psiElement = ((TwigFile) psiFile).findElementAt(editor.getCaretModel().getOffset()); |
| 62 | + if(psiElement == null) { |
| 63 | + this.setStatus(event, false); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + // <a title="TEXT">TEXT</a> |
| 68 | + IElementType elementType = psiElement.getNode().getElementType(); |
| 69 | + if(elementType == XmlTokenType.XML_DATA_CHARACTERS || elementType == XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN) { |
| 70 | + this.setStatus(event, true); |
| 71 | + } else { |
| 72 | + this.setStatus(event, false); |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + private void setStatus(AnActionEvent event, boolean status) { |
| 78 | + event.getPresentation().setVisible(status); |
| 79 | + event.getPresentation().setEnabled(status); |
| 80 | + } |
| 81 | + |
| 82 | + public void actionPerformed(AnActionEvent event) { |
| 83 | + |
| 84 | + final Editor editor = event.getData(PlatformDataKeys.EDITOR); |
| 85 | + if(editor == null) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + Object psiFile = event.getData(DataKey.create("psi.File")); |
| 90 | + if(!(psiFile instanceof TwigFile)) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + final PsiElement psiElement = ((TwigFile) psiFile).findElementAt(editor.getCaretModel().getOffset()); |
| 95 | + if(psiElement == null) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + IElementType elementType = psiElement.getNode().getElementType(); |
| 100 | + if(!(elementType == XmlTokenType.XML_DATA_CHARACTERS || elementType == XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + final String translationText = psiElement.getText(); |
| 105 | + |
| 106 | + SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(psiElement.getProject()); |
| 107 | + final SymfonyBundle symfonyBundle = symfonyBundleUtil.getContainingBundle(psiElement.getContainingFile()); |
| 108 | + |
| 109 | + final List<PsiFile> psiFiles = TranslationUtil.getAllTranslationFiles(psiElement.getProject()); |
| 110 | + |
| 111 | + final List<TranslationFileModel> psiFilesSorted = new ArrayList<TranslationFileModel>(); |
| 112 | + for(PsiFile psiFile1: psiFiles) { |
| 113 | + TranslationFileModel psiWeightList = new TranslationFileModel(psiFile1); |
| 114 | + |
| 115 | + if(symfonyBundle != null && symfonyBundle.isInBundle(psiFile1)) { |
| 116 | + psiWeightList.setSymfonyBundle(symfonyBundle); |
| 117 | + psiWeightList.addWeight(2); |
| 118 | + } else { |
| 119 | + psiWeightList.setSymfonyBundle(symfonyBundleUtil.getContainingBundle(psiFile1)); |
| 120 | + } |
| 121 | + |
| 122 | + String relativePath = psiWeightList.getRelativePath(); |
| 123 | + if(relativePath != null && (relativePath.startsWith("src") || relativePath.startsWith("app"))) { |
| 124 | + psiWeightList.addWeight(1); |
| 125 | + } |
| 126 | + |
| 127 | + psiFilesSorted.add(psiWeightList); |
| 128 | + } |
| 129 | + |
| 130 | + Collections.sort(psiFilesSorted, new PsiWeightListComparator()); |
| 131 | + |
| 132 | + TranslatorKeyExtractorDialog extractorDialog = new TranslatorKeyExtractorDialog(psiFilesSorted, new TranslatorKeyExtractorDialog.OnOkCallback() { |
| 133 | + @Override |
| 134 | + public void onClick(List<TranslationFileModel> files, final String keyName) { |
| 135 | + |
| 136 | + PsiDocumentManager.getInstance(psiElement.getProject()).doPostponedOperationsAndUnblockDocument(editor.getDocument()); |
| 137 | + final TextRange range = psiElement.getTextRange(); |
| 138 | + |
| 139 | + String domainName = files.get(0).getPsiFile().getName(); |
| 140 | + int indexOfPoint = domainName.indexOf("."); |
| 141 | + if(indexOfPoint > 0) { |
| 142 | + domainName = domainName.substring(0, indexOfPoint); |
| 143 | + } |
| 144 | + |
| 145 | + final String finalDomainName = domainName; |
| 146 | + ApplicationManager.getApplication().runWriteAction(new Runnable() { |
| 147 | + @Override |
| 148 | + public void run() { |
| 149 | + editor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), String.format("{{ '%s'|trans({}, '%s') }}", keyName, finalDomainName)); |
| 150 | + editor.getCaretModel().moveToOffset(range.getStartOffset()); |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + for(TranslationFileModel transPsiFile: files) { |
| 155 | + TranslationInsertUtil.invokeTranslation(keyName, translationText, transPsiFile.getPsiFile(), false); |
| 156 | + } |
| 157 | + |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + extractorDialog.setTitle("Extract Key"); |
| 162 | + extractorDialog.setMinimumSize(new Dimension(600, 300)); |
| 163 | + extractorDialog.pack(); |
| 164 | + extractorDialog.setLocationRelativeTo(null); |
| 165 | + extractorDialog.setVisible(true); |
| 166 | + |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | +} |
| 171 | + |
| 172 | + |
0 commit comments