2
2
3
3
import com .intellij .codeInsight .intention .impl .BaseIntentionAction ;
4
4
import com .intellij .openapi .application .ApplicationManager ;
5
+ import com .intellij .openapi .command .CommandProcessor ;
5
6
import com .intellij .openapi .editor .Editor ;
6
7
import com .intellij .openapi .project .Project ;
7
8
import com .intellij .openapi .vfs .VfsUtil ;
8
9
import com .intellij .openapi .vfs .VirtualFile ;
9
10
import com .intellij .psi .PsiFile ;
10
11
import com .intellij .psi .PsiManager ;
12
+ import com .intellij .psi .xml .XmlFile ;
11
13
import com .intellij .util .IncorrectOperationException ;
12
14
import fr .adrienbrault .idea .symfony2plugin .translation .util .TranslationInsertUtil ;
13
15
import org .jetbrains .annotations .NotNull ;
14
16
import org .jetbrains .yaml .psi .YAMLFile ;
15
17
16
18
public class TranslationKeyIntentionAction extends BaseIntentionAction {
19
+ @ NotNull
20
+ private PsiFile psiFile ;
17
21
18
- protected YAMLFile yamlFile ;
19
- protected String keyName ;
22
+ @ NotNull
23
+ private String keyName ;
20
24
21
25
/**
22
- *
23
- * @param yamlFile Translation file as yaml
26
+ * @param psiFile Translation file as yaml
24
27
* @param keyName key name like "translation" or "translation.sub.name"
25
28
*/
26
- public TranslationKeyIntentionAction (YAMLFile yamlFile , String keyName ) {
27
- this .yamlFile = yamlFile ;
29
+ public TranslationKeyIntentionAction (@ NotNull PsiFile psiFile , @ NotNull String keyName ) {
30
+ this .psiFile = psiFile ;
28
31
this .keyName = keyName ;
29
32
}
30
33
31
34
@ NotNull
32
35
@ Override
33
36
public String getText () {
34
- String filename = yamlFile .getName ();
37
+ String filename = psiFile .getName ();
35
38
36
39
// try to find suitable presentable filename
37
- VirtualFile virtualFile = yamlFile .getVirtualFile ();
40
+ VirtualFile virtualFile = psiFile .getVirtualFile ();
38
41
if (virtualFile != null ) {
39
42
filename = virtualFile .getPath ();
40
- String relativePath = VfsUtil .getRelativePath (virtualFile , yamlFile .getProject ().getBaseDir (), '/' );
43
+ String relativePath = VfsUtil .getRelativePath (virtualFile , psiFile .getProject ().getBaseDir (), '/' );
41
44
if (relativePath != null ) {
42
45
filename = relativePath ;
43
46
}
@@ -59,20 +62,23 @@ public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file
59
62
60
63
@ Override
61
64
public void invoke (@ NotNull Project project , final Editor editor , final PsiFile file ) throws IncorrectOperationException {
62
-
63
- VirtualFile virtualFile = TranslationKeyIntentionAction .this .yamlFile .getVirtualFile ();
65
+ VirtualFile virtualFile = TranslationKeyIntentionAction .this .psiFile .getVirtualFile ();
64
66
if (virtualFile == null ) {
65
67
return ;
66
68
}
67
69
68
70
final PsiFile psiFile = PsiManager .getInstance (project ).findFile (virtualFile );
69
- if (!(psiFile instanceof YAMLFile )) {
70
- return ;
71
+ if (psiFile instanceof YAMLFile ) {
72
+ ApplicationManager .getApplication ().runWriteAction (() -> {
73
+ TranslationInsertUtil .invokeTranslation (editor , keyName , keyName , (YAMLFile ) psiFile , true );
74
+ });
75
+ } else if (psiFile instanceof XmlFile ) {
76
+ CommandProcessor .getInstance ().executeCommand (project , () -> ApplicationManager .getApplication ().runWriteAction (() ->
77
+ ApplicationManager .getApplication ().runWriteAction (() ->
78
+ TranslationInsertUtil .insertTranslation ((XmlFile ) psiFile , keyName , keyName )
79
+ )
80
+ ), null , null );
71
81
}
72
-
73
- ApplicationManager .getApplication ().runWriteAction (() -> {
74
- TranslationInsertUtil .invokeTranslation (editor , keyName , keyName , (YAMLFile ) psiFile , true );
75
- });
76
82
}
77
83
78
84
}
0 commit comments