1
+ package fr .adrienbrault .idea .symfony2plugin .translation ;
2
+
3
+ import com .intellij .codeInspection .IntentionAndQuickFixAction ;
4
+ import com .intellij .openapi .application .ApplicationManager ;
5
+ import com .intellij .openapi .command .CommandProcessor ;
6
+ import com .intellij .openapi .editor .Editor ;
7
+ import com .intellij .openapi .project .Project ;
8
+ import com .intellij .openapi .ui .popup .JBPopupFactory ;
9
+ import com .intellij .openapi .vfs .VfsUtil ;
10
+ import com .intellij .openapi .vfs .VirtualFile ;
11
+ import com .intellij .psi .PsiFile ;
12
+ import com .intellij .ui .components .JBList ;
13
+ import fr .adrienbrault .idea .symfony2plugin .translation .dict .TranslationUtil ;
14
+ import fr .adrienbrault .idea .symfony2plugin .translation .util .TranslationInsertUtil ;
15
+ import org .apache .commons .lang .StringUtils ;
16
+ import org .jetbrains .annotations .Nls ;
17
+ import org .jetbrains .annotations .NotNull ;
18
+ import org .jetbrains .annotations .Nullable ;
19
+ import org .jetbrains .yaml .psi .YAMLFile ;
20
+
21
+ import javax .swing .*;
22
+ import java .awt .*;
23
+ import java .util .ArrayList ;
24
+ import java .util .Collection ;
25
+ import java .util .List ;
26
+
27
+ /**
28
+ * @author Daniel Espendiller <daniel@espendiller.net>
29
+ */
30
+ public class TranslationKeyIntentionAndQuickFixAction extends IntentionAndQuickFixAction {
31
+ @ NotNull
32
+ private final String key ;
33
+
34
+ @ NotNull
35
+ private final String domain ;
36
+
37
+ @ NotNull
38
+ private final DomainCollector domainCollector ;
39
+
40
+ public TranslationKeyIntentionAndQuickFixAction (@ NotNull String key , @ NotNull String domain ) {
41
+ this (key , domain , new AllDomainCollector ());
42
+ }
43
+
44
+ public TranslationKeyIntentionAndQuickFixAction (@ NotNull String key , @ NotNull String domain , @ NotNull DomainCollector domainCollector ) {
45
+ this .key = key ;
46
+ this .domain = domain ;
47
+ this .domainCollector = domainCollector ;
48
+ }
49
+
50
+ @ NotNull
51
+ @ Override
52
+ public String getName () {
53
+ return "Symfony: add translations" ;
54
+ }
55
+
56
+ @ Nls
57
+ @ NotNull
58
+ @ Override
59
+ public String getFamilyName () {
60
+ return "Symfony" ;
61
+ }
62
+
63
+ @ NotNull
64
+ private String getPresentableName (@ NotNull Project project , @ NotNull VirtualFile virtualFile ) {
65
+ // try to find suitable presentable filename
66
+ String filename = virtualFile .getPath ();
67
+
68
+ String relativePath = VfsUtil .getRelativePath (virtualFile , project .getBaseDir (), '/' );
69
+ if (relativePath != null ) {
70
+ filename = relativePath ;
71
+ }
72
+
73
+ return StringUtils .abbreviate (filename , 180 );
74
+ }
75
+
76
+ @ Override
77
+ public void applyFix (@ NotNull Project project , @ NotNull PsiFile psiFile , @ Nullable Editor editor ) {
78
+ if (editor == null ) {
79
+ return ;
80
+ }
81
+
82
+ List <PsiFile > files = new ArrayList <>();
83
+
84
+ for (PsiFile translationPsiFile : this .domainCollector .collect (project , key , domain )) {
85
+ if (translationPsiFile instanceof YAMLFile || TranslationUtil .isSupportedXlfFile (translationPsiFile )) {
86
+ String relativePath = VfsUtil .getRelativePath (translationPsiFile .getVirtualFile (), project .getBaseDir (), '/' );
87
+
88
+ // sort collection. eg vendor last
89
+ if (relativePath != null && (relativePath .startsWith ("app" ) || relativePath .startsWith ("src" ))) {
90
+ files .add (0 , translationPsiFile );
91
+ } else {
92
+ files .add (translationPsiFile );
93
+ }
94
+ }
95
+ }
96
+
97
+ JBList <PsiFile > list = new JBList <>(files );
98
+
99
+ list .setCellRenderer (new JBList .StripedListCellRenderer () {
100
+ @ Override
101
+ public Component getListCellRendererComponent (JList list , Object value , int index , boolean isSelected , boolean cellHasFocus ) {
102
+ Component renderer = super .getListCellRendererComponent (list , value , index , isSelected , cellHasFocus );
103
+ if (renderer instanceof JLabel && value instanceof PsiFile ) {
104
+ ((JLabel ) renderer ).setText (getPresentableName (project , ((PsiFile ) value ).getVirtualFile ()));
105
+ }
106
+
107
+ return renderer ;
108
+ }
109
+ });
110
+
111
+ JBPopupFactory .getInstance ().createListPopupBuilder (list )
112
+ .setTitle ("Symfony: Translation files" )
113
+ .setItemChoosenCallback (() -> {
114
+ PsiFile selectedFile = list .getSelectedValue ();
115
+
116
+ CommandProcessor .getInstance ().executeCommand (selectedFile .getProject (), () -> ApplicationManager .getApplication ().runWriteAction (() -> {
117
+ TranslationInsertUtil .invokeTranslation (selectedFile , key , domain );
118
+ }), "Translation insert " + selectedFile .getName (), null );
119
+ })
120
+ .createPopup ()
121
+ .showInBestPositionFor (editor );
122
+ }
123
+
124
+ public interface DomainCollector {
125
+ @ NotNull
126
+ Collection <PsiFile > collect (@ NotNull Project project , @ NotNull String key , @ NotNull String domain );
127
+ }
128
+
129
+ private static class AllDomainCollector implements DomainCollector {
130
+ @ Override
131
+ @ NotNull
132
+ public Collection <PsiFile > collect (@ NotNull Project project , @ NotNull String key , @ NotNull String domain ) {
133
+ return TranslationUtil .getDomainPsiFiles (project , domain );
134
+ }
135
+ }
136
+ }
0 commit comments