|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.templating; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.CodeInsightActionHandler; |
| 4 | +import com.intellij.codeInsight.actions.CodeInsightAction; |
| 5 | +import com.intellij.codeInsight.lookup.LookupElement; |
| 6 | +import com.intellij.openapi.command.WriteCommandAction; |
| 7 | +import com.intellij.openapi.editor.Editor; |
| 8 | +import com.intellij.openapi.project.Project; |
| 9 | +import com.intellij.openapi.ui.popup.JBPopupFactory; |
| 10 | +import com.intellij.psi.PsiElement; |
| 11 | +import com.intellij.psi.PsiFile; |
| 12 | +import com.intellij.psi.impl.source.html.HtmlFileImpl; |
| 13 | +import com.intellij.ui.components.JBList; |
| 14 | +import com.jetbrains.php.completion.insert.PhpInsertHandlerUtil; |
| 15 | +import com.jetbrains.twig.TwigFile; |
| 16 | +import fr.adrienbrault.idea.symfony2plugin.action.TwigExtractLanguageAction; |
| 17 | +import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil; |
| 18 | +import fr.adrienbrault.idea.symfony2plugin.translation.dict.TranslationUtil; |
| 19 | +import org.apache.commons.lang.StringUtils; |
| 20 | +import org.jetbrains.annotations.NotNull; |
| 21 | +import org.jetbrains.annotations.Nullable; |
| 22 | + |
| 23 | +import java.util.List; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.TreeMap; |
| 26 | +import java.util.TreeSet; |
| 27 | +import java.util.stream.Collectors; |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 31 | + */ |
| 32 | +public class TwigTranslationGeneratorAction extends CodeInsightAction { |
| 33 | + @Override |
| 34 | + protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { |
| 35 | + return file instanceof TwigFile |
| 36 | + || file instanceof HtmlFileImpl && file.getName().toLowerCase().endsWith(".twig") |
| 37 | + || getInjectedTwigElement(file, editor) != null; |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + @NotNull |
| 42 | + @Override |
| 43 | + protected CodeInsightActionHandler getHandler() { |
| 44 | + return new MyCodeInsightActionHandler(); |
| 45 | + } |
| 46 | + |
| 47 | + @Nullable |
| 48 | + private static PsiElement getInjectedTwigElement(@NotNull PsiFile psiFile, @NotNull Editor editor) { |
| 49 | + int caretOffset = editor.getCaretModel().getOffset(); |
| 50 | + if(caretOffset <= 0) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + PsiElement psiElement = psiFile.findElementAt(caretOffset); |
| 55 | + if(psiElement == null) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + return TwigUtil.getElementOnTwigViewProvider(psiElement); |
| 60 | + } |
| 61 | + |
| 62 | + private static class MyCodeInsightActionHandler implements CodeInsightActionHandler { |
| 63 | + @Override |
| 64 | + public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile psiFile) { |
| 65 | + int caretOffset = editor.getCaretModel().getOffset(); |
| 66 | + if(caretOffset <= 0) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + PsiElement psiElement = getInjectedTwigElement(psiFile, editor); |
| 71 | + if(psiElement == null) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + PsiFile containingFile = psiElement.getContainingFile(); |
| 76 | + if(!(containingFile instanceof TwigFile)) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + String defaultDomain = TwigUtil.getTransDefaultDomainOnScope(psiElement); |
| 81 | + if(defaultDomain == null) { |
| 82 | + defaultDomain = "messages"; |
| 83 | + } |
| 84 | + |
| 85 | + final Set<String> domainNames = TranslationUtil.getTranslationDomainLookupElements(project).stream() |
| 86 | + .map(LookupElement::getLookupString) |
| 87 | + .collect(Collectors.toCollection(TreeSet::new)); |
| 88 | + |
| 89 | + TreeMap<String, Integer> sortedMap = TwigExtractLanguageAction.getPossibleDomainTreeMap((TwigFile) containingFile, domainNames); |
| 90 | + |
| 91 | + // we want to have mostly used domain preselected |
| 92 | + String domain = defaultDomain; |
| 93 | + if(sortedMap.size() > 0) { |
| 94 | + domain = sortedMap.firstKey(); |
| 95 | + } |
| 96 | + |
| 97 | + List<String> collect = TranslationUtil.getTranslationLookupElementsOnDomain(project, domain) |
| 98 | + .stream() |
| 99 | + .map(LookupElement::getLookupString) |
| 100 | + .sorted() |
| 101 | + .collect(Collectors.toList()); |
| 102 | + |
| 103 | + final JBList<String> list = new JBList<>(collect); |
| 104 | + |
| 105 | + String finalDefaultDomain = defaultDomain; |
| 106 | + String finalDomain = domain; |
| 107 | + |
| 108 | + JBPopupFactory.getInstance().createListPopupBuilder(list) |
| 109 | + .setTitle(String.format("Symfony: Translations \"%s\"", StringUtils.abbreviate(domain, 20))) |
| 110 | + .setItemChoosenCallback(() -> { |
| 111 | + String selectedValue = list.getSelectedValue(); |
| 112 | + |
| 113 | + new WriteCommandAction.Simple(editor.getProject(), String.format("Symfony: Add Translation \"%s\"", StringUtils.abbreviate(selectedValue, 20))) { |
| 114 | + @Override |
| 115 | + protected void run() { |
| 116 | + String s; |
| 117 | + |
| 118 | + if (!finalDomain.equals(finalDefaultDomain)) { |
| 119 | + s = String.format("{{ '%s'|trans({}, '%s') }}", selectedValue, finalDomain); |
| 120 | + } else { |
| 121 | + s = String.format("{{ '%s'|trans }}", selectedValue); |
| 122 | + } |
| 123 | + |
| 124 | + PhpInsertHandlerUtil.insertStringAtCaret(editor, s); |
| 125 | + } |
| 126 | + }.execute(); |
| 127 | + }) |
| 128 | + .createPopup() |
| 129 | + .showInBestPositionFor(editor); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments