Skip to content

Commit 22acbf7

Browse files
committed
Add a FileWatcher for HTML templates
When an HTML template is touched we touch the associated Java component class to make the Annotation processor run on it
1 parent 00edcbd commit 22acbf7

File tree

6 files changed

+185
-11
lines changed

6 files changed

+185
-11
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
11
# Vue GWT IntelliJ Plugin
22

3-
Experimental IntelliJ plugin for [Vue GWT](https://github.com/Axellience/vue-gwt).
3+
Add support for [Vue GWT](https://github.com/Axellience/vue-gwt) in IntelliJ.
4+
5+
When you change your Vue Components templates this plugin will automatically touch the associated Java file.
6+
7+
8+
By default IntelliJ doesn't support automatic annotation processing on Java file change when the app is running.
9+
10+
After installing this plugin, follow these steps to enable it:
11+
12+
13+
Go to
14+
`File > Settings > Build, Execution, Deployment > Compiler` and enable "Make project automatically"
15+
<br/><br/>
16+
17+
Open the Action window :
18+
19+
* Linux : `CTRL+SHIFT+A`
20+
* MacOS : `SHIFT+COMMAND+A`
21+
* Windows : `CTRL+ALT+SHIFT+/`
22+
</ul>
23+
24+
Enter `Registry...` and enable `compiler.automake.allow.when.app.running`.
25+
26+
27+
You are good to go!

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ buildscript {
1515

1616
apply plugin: 'org.jetbrains.intellij'
1717

18-
version = "IU-173.3942.27"
18+
version = "IU-162.2228.15"
1919
intellij {
20-
version 'IU-173.3942.27'
20+
version 'IU-162.2228.15'
2121
pluginName 'vue-gwt-plugin'
2222
downloadSources false
2323
updateSinceUntilBuild false

src/main/java/com/axellience/vuegwtplugin/VueCustomInjector.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
import com.intellij.psi.xml.XmlAttribute;
1717
import com.intellij.psi.xml.XmlAttributeValue;
1818

19+
/**
20+
* Inject Java expression in Vue GWT templates
21+
* Disabled for now in plugin.xml
22+
*/
1923
public class VueCustomInjector implements MultiHostInjector
2024
{
2125
@Override
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.axellience.vuegwtplugin;
2+
3+
import static com.intellij.AppTopics.FILE_DOCUMENT_SYNC;
4+
5+
import org.jetbrains.annotations.NotNull;
6+
7+
import com.intellij.openapi.application.ApplicationManager;
8+
import com.intellij.openapi.components.ProjectComponent;
9+
import com.intellij.openapi.project.Project;
10+
import com.intellij.util.messages.MessageBusConnection;
11+
12+
public class VueGWTApplicationComponent implements ProjectComponent
13+
{
14+
private final Project project;
15+
private MessageBusConnection messageBusConnection;
16+
17+
public VueGWTApplicationComponent(Project project)
18+
{
19+
this.project = project;
20+
}
21+
22+
@Override
23+
public void projectOpened()
24+
{
25+
messageBusConnection = ApplicationManager.getApplication().getMessageBus().connect(project);
26+
messageBusConnection.subscribe(FILE_DOCUMENT_SYNC, new VueGWTFileWatcher(project));
27+
}
28+
29+
@Override
30+
public void projectClosed()
31+
{
32+
messageBusConnection.disconnect();
33+
}
34+
35+
@Override
36+
public void initComponent()
37+
{
38+
}
39+
40+
@Override
41+
public void disposeComponent()
42+
{
43+
}
44+
45+
@NotNull
46+
@Override
47+
public String getComponentName()
48+
{
49+
return "Vue GWT";
50+
}
51+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.axellience.vuegwtplugin;
2+
3+
import java.io.File;
4+
5+
import org.jetbrains.annotations.NotNull;
6+
7+
import com.intellij.openapi.editor.Document;
8+
import com.intellij.openapi.fileEditor.FileDocumentManager;
9+
import com.intellij.openapi.fileEditor.FileDocumentManagerAdapter;
10+
import com.intellij.openapi.project.Project;
11+
import com.intellij.openapi.vfs.VirtualFile;
12+
import com.intellij.psi.PsiDocumentManager;
13+
import com.intellij.psi.PsiFile;
14+
15+
public class VueGWTFileWatcher extends FileDocumentManagerAdapter
16+
{
17+
VueGWTFileWatcher(Project project)
18+
{
19+
this.project = project;
20+
}
21+
22+
@Override
23+
public void beforeDocumentSaving(@NotNull Document document)
24+
{
25+
PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
26+
if (psiFile == null)
27+
return;
28+
29+
processFile(psiFile.getVirtualFile());
30+
}
31+
32+
private void processFile(VirtualFile virtualFile)
33+
{
34+
if (!"html".equals(virtualFile.getExtension()))
35+
return;
36+
37+
String javaClassFileName = virtualFile.getNameWithoutExtension() + ".java";
38+
VirtualFile parent = virtualFile.getParent();
39+
if (parent == null)
40+
return;
41+
42+
for (VirtualFile file : parent.getChildren())
43+
{
44+
if (javaClassFileName.equals(file.getName()))
45+
{
46+
saveFileIfNeeded(FileDocumentManager.getInstance(), file);
47+
new File(file.getPath()).setLastModified(System.currentTimeMillis());
48+
file.refresh(false, false);
49+
return;
50+
}
51+
}
52+
}
53+
54+
private static void saveFileIfNeeded(@NotNull FileDocumentManager fileDocumentManager,
55+
@NotNull VirtualFile virtualFile)
56+
{
57+
// Gets the document to force its saving when the editor is closed.
58+
// If it is not cached, no editor was opened to edit it => nothing to be done
59+
final Document fileDocument = fileDocumentManager.getCachedDocument(virtualFile);
60+
61+
if (fileDocument != null && fileDocumentManager.isDocumentUnsaved(fileDocument))
62+
fileDocumentManager.saveDocument(fileDocument);
63+
}
64+
65+
private final Project project;
66+
}
Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,60 @@
11
<idea-plugin>
22
<id>com.axellience.vuegwt</id>
3-
<name>VueGWT</name>
3+
<name>Vue GWT</name>
44
<version>0.0.1</version>
55
<vendor email="[email protected]" url="http://www.genmymodel.com">Axellience</vendor>
66

77
<description><![CDATA[
8-
Add syntax highlighting and code completion in Vue GWT templates.
9-
]]></description>
8+
Add support for <a href="https://github.com/Axellience/vue-gwt">Vue GWT</a> in IntelliJ.
9+
<br/>
10+
When you change your Vue Components templates this plugin will automatically touch the associated Java file.
11+
<br/><br/>
12+
13+
By default IntelliJ doesn't support automatic annotation processing on Java file change when the app is running.
14+
<br/>
15+
After installing this plugin, follow these steps to enable it:
16+
<br/><br/>
17+
18+
Go to
19+
<b>File > Settings > Build, Execution, Deployment > Compiler</b> and enable "Make project automatically"
20+
<br/><br/>
21+
22+
Open the Action window :
23+
<ul>
24+
<li>Linux : <b>CTRL+SHIFT+A</b></li>
25+
<li>MacOS : <b>SHIFT+COMMAND+A</b></li>
26+
<li>Windows : <b>CTRL+ALT+SHIFT+/</b></li>
27+
</ul>
28+
29+
Enter <b>Registry...</b> and enable <b>compiler.automake.allow.when.app.running</b>.
30+
<br/><br/>
31+
32+
You are good to go!
33+
]]></description>
1034

1135
<change-notes><![CDATA[
1236
]]>
1337
</change-notes>
1438

1539
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
16-
<idea-version since-build="173.0"/>
40+
<idea-version since-build="162"/>
1741

1842
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
1943
on how to target different products -->
2044
<!-- uncomment to enable plugin in all products
2145
<depends>com.intellij.modules.lang</depends>
2246
-->
2347

24-
<extensions defaultExtensionNs="com.intellij">
48+
<!--<extensions defaultExtensionNs="com.intellij">
2549
<multiHostInjector implementation="com.axellience.vuegwtplugin.VueCustomInjector"/>
26-
</extensions>
50+
</extensions>-->
2751

28-
<actions>
29-
</actions>
52+
<actions></actions>
3053

54+
<project-components>
55+
<component>
56+
<implementation-class>com.axellience.vuegwtplugin.VueGWTApplicationComponent
57+
</implementation-class>
58+
</component>
59+
</project-components>
3160
</idea-plugin>

0 commit comments

Comments
 (0)