refactor converter initialization

- push lazyInit() down to IDE and Cli implementations
- no file listeners created in cli implementation anymore
- fix tests
This commit is contained in:
Mikhail Mutcianko
2014-07-21 12:37:36 +04:00
committed by Yan Zhulanow
parent d557fa8d5d
commit d107ad92a0
6 changed files with 52 additions and 32 deletions
@@ -296,7 +296,7 @@ public class JetCoreEnvironment {
);
String s = configuration.get(JVMConfigurationKeys.ANDROID_RES_PATH);
project.registerService(AndroidUIXmlParser.class, new CliAndroidUIXmlParser(s));
project.registerService(AndroidUIXmlParser.class, new CliAndroidUIXmlParser(project, s));
project.registerService(VirtualFileFinderFactory.class, new CliVirtualFileFinderFactory(classPath));
}
@@ -118,7 +118,7 @@ abstract class AndroidUIXmlParser {
private fun doParse(): CacheAction? {
if (searchPath == null || searchPath == "") return null
lazySetupFileListener()
lazySetup()
if (invalidateCaches) invalidateCaches()
var overallCacheMiss = false
var file = filesToProcess.poll()
@@ -144,31 +144,7 @@ abstract class AndroidUIXmlParser {
invalidateCaches = false
}
private fun lazySetupFileListener() {
if (listenerSetUp) return
val fileManager = VirtualFileManager.getInstance()
val watchDir = fileManager.findFileByUrl("file://" + searchPath)
filesToProcess.addAll(watchDir?.getChildren()?.toArrayList() ?: ArrayList(0))
fileManager.addVirtualFileListener(object : VirtualFileAdapter() {
override fun contentsChanged(event: VirtualFileEvent) {
if (event.getParent() == watchDir)
filesToProcess.add(event.getFile())
}
override fun fileCreated(event: VirtualFileEvent) {
if (event.getParent() == watchDir)
super<VirtualFileAdapter>.fileCreated(event)
}
override fun fileDeleted(event: VirtualFileEvent) {
if (event.getParent() == watchDir) {
// ignore potential synchronisation issues - it doesn't really matter if invalidation and
// file processing will be handled in different passes
invalidateCaches = true
filesToProcess.addAll(watchDir?.getChildren()?.toArrayList() ?: ArrayList(0))
}
}
})
listenerSetUp = true
}
protected abstract fun lazySetup()
private fun produceKotlinProperties(kw: KotlinStringWriter, ids: Collection<AndroidWidget>): StringBuffer {
for (id in ids) {
@@ -1,4 +1,15 @@
package org.jetbrains.jet.lang.resolve.android
class CliAndroidUIXmlParser(override val searchPath: String?): AndroidUIXmlParser()
import com.intellij.openapi.vfs.VirtualFileManager
import java.util.ArrayList
import com.intellij.openapi.project.Project
class CliAndroidUIXmlParser(val project: Project, override val searchPath: String?): AndroidUIXmlParser() {
override fun lazySetup() {
val fileManager = VirtualFileManager.getInstance()
val watchDir = fileManager.findFileByUrl("file://" + searchPath)
filesToProcess.addAll(watchDir?.getChildren()?.toArrayList() ?: ArrayList(0))
}
}
@@ -20,7 +20,7 @@ import java.util.Scanner;
public class AndroidXmlTest extends TestCaseWithTmpdir {
private final File singleFile = new File(getTestDataPath() + "/converter/singleFile/layout.xml");
private final File singleFileDir = new File(getTestDataPath() + "/converter/singleFile/res/");
private final File fakeActivitySrc = new File(getTestDataPath() + "/fakeHelpers/Activity.kt");
private final File fakeViewSrc = new File(getTestDataPath() + "/fakeHelpers/View.kt");
private final File fakeWidgetsSrc = new File(getTestDataPath() + "/fakeHelpers/Widgets.kt");
@@ -50,10 +50,11 @@ public class AndroidXmlTest extends TestCaseWithTmpdir {
}
public void testCompileResult() throws Exception {
String text = new CliAndroidUIXmlParser(singleFile.getAbsolutePath()).parseToString();
JetCoreEnvironment jetCoreEnvironment = JetTestUtils.createEnvironmentWithMockJdkAndIdeaAnnotations(getTestRootDisposable(),
ConfigurationKind.ALL);
JetFile psiFile = JetTestUtils.createFile(singleFile.getName(), text, jetCoreEnvironment.getProject());
String text = new CliAndroidUIXmlParser(jetCoreEnvironment.getProject(), singleFileDir.getAbsolutePath()).parseToString();
JetFile psiFile = JetTestUtils.createFile("dummy.kt", text, jetCoreEnvironment.getProject());
JetFile fakeActivity = JetTestUtils.loadJetFile(jetCoreEnvironment.getProject(), fakeActivitySrc);
JetFile fakeView = JetTestUtils.loadJetFile(jetCoreEnvironment.getProject(), fakeViewSrc);
JetFile fakeWidgets = JetTestUtils.loadJetFile(jetCoreEnvironment.getProject(), fakeWidgetsSrc);
@@ -70,7 +71,9 @@ public class AndroidXmlTest extends TestCaseWithTmpdir {
}
public void testConverterOneFile() throws Exception {
AndroidUIXmlParser parser = new CliAndroidUIXmlParser(singleFile.getAbsolutePath());
JetCoreEnvironment jetCoreEnvironment = JetTestUtils.createEnvironmentWithMockJdkAndIdeaAnnotations(getTestRootDisposable(),
ConfigurationKind.ALL);
AndroidUIXmlParser parser = new CliAndroidUIXmlParser(jetCoreEnvironment.getProject(), singleFileDir.getAbsolutePath());
String actual = parser.parseToString();
String expected = loadOrCreate(new File(getTestDataPath() + "/converter/singleFile/layout.kt"), actual);
@@ -2,8 +2,38 @@ package org.jetbrains.jet.plugin.android
import org.jetbrains.jet.lang.resolve.android.AndroidUIXmlParser
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFileManager
import java.util.ArrayList
import com.intellij.openapi.vfs.VirtualFileAdapter
import com.intellij.openapi.vfs.VirtualFileEvent
class IDEAndroidUIXmlParser(project: Project): AndroidUIXmlParser() {
override val searchPath: String? = project.getBasePath() + "/res/layout/"
override protected fun lazySetup() {
if (listenerSetUp) return
val fileManager = VirtualFileManager.getInstance()
val watchDir = fileManager.findFileByUrl("file://" + searchPath)
filesToProcess.addAll(watchDir?.getChildren()?.toArrayList() ?: ArrayList(0))
fileManager.addVirtualFileListener(object : VirtualFileAdapter() {
override fun contentsChanged(event: VirtualFileEvent) {
if (event.getParent() == watchDir)
filesToProcess.add(event.getFile())
}
override fun fileCreated(event: VirtualFileEvent) {
if (event.getParent() == watchDir)
super<VirtualFileAdapter>.fileCreated(event)
}
override fun fileDeleted(event: VirtualFileEvent) {
if (event.getParent() == watchDir) {
// ignore potential synchronisation issues - it doesn't really matter if invalidation and
// file processing will be handled in different passes
invalidateCaches = true
filesToProcess.addAll(watchDir?.getChildren()?.toArrayList() ?: ArrayList(0))
}
}
})
listenerSetUp = true
}
}