|
13 | 13 | import com.intellij.psi.PsiDocumentManager; |
14 | 14 | import com.intellij.psi.PsiFile; |
15 | 15 | import org.jetbrains.annotations.NotNull; |
| 16 | +import org.jetbrains.annotations.Nullable; |
16 | 17 |
|
17 | | -public class VueGWTFileWatcher extends FileDocumentManagerAdapter |
18 | | -{ |
19 | | - private static final Logger LOGGER = Logger.getInstance(VueGWTFileWatcher.class); |
20 | | - private final Project project; |
| 18 | +public class VueGWTFileWatcher extends FileDocumentManagerAdapter { |
21 | 19 |
|
22 | | - VueGWTFileWatcher(Project project) |
23 | | - { |
24 | | - this.project = project; |
| 20 | + private static final Logger LOGGER = Logger.getInstance(VueGWTFileWatcher.class); |
| 21 | + private final Project project; |
| 22 | + |
| 23 | + VueGWTFileWatcher(Project project) { |
| 24 | + this.project = project; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public void beforeDocumentSaving(@NotNull Document document) { |
| 29 | + PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document); |
| 30 | + if (psiFile == null) { |
| 31 | + return; |
25 | 32 | } |
26 | 33 |
|
27 | | - @Override |
28 | | - public void beforeDocumentSaving(@NotNull Document document) |
29 | | - { |
30 | | - PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document); |
31 | | - if (psiFile == null) |
32 | | - return; |
| 34 | + processFile(psiFile.getVirtualFile()); |
| 35 | + } |
| 36 | + |
| 37 | + private void processFile(VirtualFile changedFile) { |
| 38 | + if ("html".equals(changedFile.getExtension())) { |
| 39 | + processHtmlFile(changedFile); |
| 40 | + } else if ("java".equals(changedFile.getExtension())) { |
| 41 | + processJavaFile(changedFile); |
| 42 | + } |
| 43 | + } |
33 | 44 |
|
34 | | - processFile(psiFile.getVirtualFile()); |
| 45 | + private void processHtmlFile(VirtualFile htmlFile) { |
| 46 | + VirtualFile javaFile = getSibling(htmlFile, htmlFile.getNameWithoutExtension() + ".java"); |
| 47 | + if (javaFile == null) { |
| 48 | + return; |
35 | 49 | } |
36 | 50 |
|
37 | | - private void processFile(VirtualFile changedFile) |
38 | | - { |
39 | | - if (!"html".equals(changedFile.getExtension())) |
40 | | - return; |
| 51 | + ApplicationManager.getApplication().invokeLater(() -> compileComponent(javaFile, htmlFile)); |
| 52 | + } |
| 53 | + |
| 54 | + private void processJavaFile(VirtualFile javaFile) { |
| 55 | + VirtualFile htmlFile = getSibling(javaFile, javaFile.getNameWithoutExtension() + ".html"); |
| 56 | + if (htmlFile == null) |
| 57 | + return; |
41 | 58 |
|
42 | | - String javaClassFileName = changedFile.getNameWithoutExtension() + ".java"; |
43 | | - VirtualFile parent = changedFile.getParent(); |
44 | | - if (parent == null) |
45 | | - return; |
| 59 | + ApplicationManager.getApplication().invokeLater(() -> compileComponent(javaFile, htmlFile)); |
| 60 | + } |
46 | 61 |
|
47 | | - VirtualFile javaFile = parent.findChild(javaClassFileName); |
48 | | - if (javaFile == null) |
49 | | - return; |
| 62 | + private void compileComponent(VirtualFile javaComponent, VirtualFile htmlTemplate) { |
| 63 | + try { |
| 64 | + ProjectFileIndex projectFileIndex = |
| 65 | + ProjectRootManager.getInstance(project).getFileIndex(); |
| 66 | + final Module module = projectFileIndex.getModuleForFile(javaComponent); |
| 67 | + if (module == null) { |
| 68 | + return; |
| 69 | + } |
50 | 70 |
|
51 | | - ApplicationManager.getApplication().invokeLater(() -> compileComponent(javaFile, changedFile)); |
| 71 | + CompilerManager compilerManager = CompilerManager.getInstance(project); |
| 72 | + if (!compilerManager.isCompilationActive() |
| 73 | + && !compilerManager.isExcludedFromCompilation(javaComponent)) { |
| 74 | + compilerManager.compile(new VirtualFile[]{javaComponent, htmlTemplate}, null); |
| 75 | + } |
| 76 | + } catch (Exception e) { |
| 77 | + LOGGER.error(e.getMessage(), e); |
52 | 78 | } |
| 79 | + } |
53 | 80 |
|
54 | | - private void compileComponent(VirtualFile javaComponent, VirtualFile htmlTemplate) |
55 | | - { |
56 | | - try |
57 | | - { |
58 | | - ProjectFileIndex projectFileIndex = |
59 | | - ProjectRootManager.getInstance(project).getFileIndex(); |
60 | | - final Module module = projectFileIndex.getModuleForFile(javaComponent); |
61 | | - if (module == null) |
62 | | - return; |
| 81 | + @Nullable |
| 82 | + private VirtualFile getSibling(VirtualFile file, String siblingName) { |
| 83 | + VirtualFile parent = file.getParent(); |
| 84 | + if (parent == null) { |
| 85 | + return null; |
| 86 | + } |
63 | 87 |
|
64 | | - CompilerManager compilerManager = CompilerManager.getInstance(project); |
65 | | - if (!compilerManager.isCompilationActive() |
66 | | - && !compilerManager.isExcludedFromCompilation(javaComponent)) |
67 | | - { |
68 | | - compilerManager.compile(new VirtualFile[] { javaComponent, htmlTemplate }, null); |
69 | | - } |
70 | | - } |
71 | | - catch (Exception e) |
72 | | - { |
73 | | - LOGGER.error(e.getMessage(), e); |
74 | | - } |
| 88 | + VirtualFile siblingFile = parent.findChild(siblingName); |
| 89 | + if (siblingFile == null) { |
| 90 | + return null; |
75 | 91 | } |
| 92 | + return siblingFile; |
| 93 | + } |
76 | 94 | } |
0 commit comments