11package com .axellience .vuegwtplugin ;
22
3- import java .util .Arrays ;
43import org .jetbrains .annotations .NotNull ;
54import org .jetbrains .annotations .Nullable ;
5+ import org .jetbrains .concurrency .Promise ;
6+
7+ import com .intellij .ide .DataManager ;
8+ import com .intellij .openapi .actionSystem .CommonDataKeys ;
69import com .intellij .openapi .application .ApplicationManager ;
710import com .intellij .openapi .compiler .CompilerManager ;
811import com .intellij .openapi .diagnostic .Logger ;
912import com .intellij .openapi .editor .Document ;
1013import com .intellij .openapi .fileEditor .FileDocumentManagerListener ;
1114import com .intellij .openapi .module .Module ;
1215import com .intellij .openapi .project .Project ;
13- import com .intellij .openapi .project .ProjectManager ;
1416import com .intellij .openapi .roots .ProjectFileIndex ;
1517import com .intellij .openapi .roots .ProjectRootManager ;
1618import com .intellij .openapi .vfs .VirtualFile ;
2022public class VueGWTFileWatcher implements FileDocumentManagerListener {
2123
2224 private static final Logger LOGGER = Logger .getInstance (VueGWTFileWatcher .class );
23- private final Project project ;
24-
25- VueGWTFileWatcher () {
26- this .project = Arrays .stream (ProjectManager .getInstance ().getOpenProjects ())
27- .findFirst ()
28- .orElseThrow (RuntimeException ::new );
29- }
3025
3126 @ Override
3227 public void beforeDocumentSaving (@ NotNull Document document ) {
33- PsiFile psiFile = PsiDocumentManager .getInstance (project ).getPsiFile (document );
34- if (psiFile == null ) {
35- return ;
36- }
28+ getProjectAsync ().onSuccess (project -> {
29+ PsiFile psiFile = PsiDocumentManager .getInstance (project ).getPsiFile (document );
30+ if (psiFile == null ) {
31+ return ;
32+ }
3733
38- processFile (psiFile .getVirtualFile ());
34+ processFile (psiFile .getVirtualFile ());
35+ });
3936 }
4037
4138 private void processFile (VirtualFile changedFile ) {
@@ -64,9 +61,8 @@ private void processJavaFile(VirtualFile javaFile) {
6461 }
6562
6663 private void compileComponent (VirtualFile javaComponent , VirtualFile htmlTemplate ) {
67- try {
68- ProjectFileIndex projectFileIndex =
69- ProjectRootManager .getInstance (project ).getFileIndex ();
64+ getProjectAsync ().onSuccess (project -> {
65+ ProjectFileIndex projectFileIndex = ProjectRootManager .getInstance (project ).getFileIndex ();
7066 final Module module = projectFileIndex .getModuleForFile (javaComponent );
7167 if (module == null ) {
7268 return ;
@@ -77,9 +73,7 @@ private void compileComponent(VirtualFile javaComponent, VirtualFile htmlTemplat
7773 && !compilerManager .isExcludedFromCompilation (javaComponent )) {
7874 compilerManager .compile (new VirtualFile [] {javaComponent , htmlTemplate }, null );
7975 }
80- } catch (Exception e ) {
81- LOGGER .error (e .getMessage (), e );
82- }
76+ });
8377 }
8478
8579 @ Nullable
@@ -91,4 +85,10 @@ private VirtualFile getSibling(VirtualFile file, String siblingName) {
9185
9286 return parent .findChild (siblingName );
9387 }
88+
89+ private Promise <Project > getProjectAsync () {
90+ return DataManager .getInstance ().getDataContextFromFocusAsync ()
91+ .then (CommonDataKeys .PROJECT ::getData )
92+ .onError (throwable -> LOGGER .warn ("Unable to get project from focus" , throwable ));
93+ }
9494}
0 commit comments