Remove ScriptModificationListener from 183 branch

Since 183 changes in .gradle.kts files are tracked correctly by platform and notification 'Gradle project needs to be imported' is shown when those files are modified
This commit is contained in:
Natalia Selezneva
2018-12-12 16:17:04 +03:00
parent 6042a3a0f7
commit ba2f211384
10 changed files with 158 additions and 110 deletions
@@ -17,7 +17,6 @@
package org.jetbrains.kotlin.idea.core.script
import com.intellij.execution.configurations.CommandLineTokenizer
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListenerAdapter
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskType
@@ -82,7 +81,7 @@ class GradleScriptDefinitionsContributor(private val project: Project) : ScriptD
}
project.messageBus.connect(project).subscribe(GradleSettingsListener.TOPIC, listener)
ServiceManager.getService(project, ScriptModificationListener::class.java)
initializeScriptModificationListener(project)
}
override fun getDefinitions(): List<KotlinScriptDefinition> {
@@ -5,111 +5,13 @@
package org.jetbrains.kotlin.idea.core.script
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.Result
import com.intellij.openapi.application.WriteAction
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.EditorFactory
import com.intellij.openapi.editor.event.DocumentEvent
import com.intellij.openapi.editor.event.DocumentListener
import com.intellij.openapi.externalSystem.service.project.manage.ExternalProjectsManager
import com.intellij.openapi.externalSystem.util.ExternalSystemUtil
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.openapi.vfs.newvfs.BulkFileListener
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
import com.intellij.psi.PsiDocumentManager
import com.intellij.util.ui.update.MergingUpdateQueue
import com.intellij.util.ui.update.MergingUpdateQueue.ANY_COMPONENT
import com.intellij.util.ui.update.Update
import org.jetbrains.plugins.gradle.service.project.GradleAutoImportAware
class ScriptModificationListener(private val project: Project) {
private val changedDocuments = HashSet<Document>()
private val changedDocumentsQueue = MergingUpdateQueue("ScriptModificationListener: Scripts queue", 1000, false, ANY_COMPONENT, project)
// BUNCH: 182
fun initializeScriptModificationListener(project: Project) {
init {
showNotificationIfScriptChangedListener()
saveScriptAfterModificationListener()
}
private fun showNotificationIfScriptChangedListener() {
project.messageBus.connect().subscribe(VirtualFileManager.VFS_CHANGES, object : BulkFileListener.Adapter() {
override fun after(events: List<VFileEvent>) {
if (ApplicationManager.getApplication().isUnitTestMode) return
val modifiedScripts = events.mapNotNull {
it.file?.takeIf { isGradleScript(it) }
}
// Workaround for IDEA-182367 (fixed in IDEA 181.3666)
if (modifiedScripts.isNotEmpty()) {
if (modifiedScripts.any {
GradleAutoImportAware().getAffectedExternalProjectPath(it.path, project) != null
}) {
return
}
ExternalProjectsManager.getInstance(project).externalProjectsWatcher.markDirty(project.basePath)
}
}
})
}
private fun saveScriptAfterModificationListener() {
// partially copied from ExternalSystemProjectsWatcherImpl before fix will be implemented in IDEA:
// "Gradle projects need to be imported" notification should be shown when kotlin script is modified
val busConnection = project.messageBus.connect(changedDocumentsQueue)
changedDocumentsQueue.activate()
EditorFactory.getInstance().eventMulticaster.addDocumentListener(object : DocumentListener {
override fun documentChanged(event: DocumentEvent) {
if (project.isDisposed) return
val doc = event.document
val file = FileDocumentManager.getInstance().getFile(doc) ?: return
if (isGradleScript(file) && event.newFragment.isNotBlank()) {
synchronized(changedDocuments) {
changedDocuments.add(doc)
}
changedDocumentsQueue.queue(object : Update(this) {
override fun run() {
var copy: Array<Document> = emptyArray()
synchronized(changedDocuments) {
copy = changedDocuments.toTypedArray()
changedDocuments.clear()
}
ExternalSystemUtil.invokeLater(project) {
object : WriteAction<Any>() {
override fun run(result: Result<Any>) {
for (each in copy) {
PsiDocumentManager.getInstance(project).commitDocument(each)
(FileDocumentManager.getInstance() as? FileDocumentManagerImpl)?.saveDocument(each, false)
}
}
}.execute()
}
}
})
}
}
}, busConnection)
}
private fun isGradleScript(file: VirtualFile): Boolean {
if (!ProjectRootManager.getInstance(project).fileIndex.isInContent(file)) return false
val contributor = ScriptDefinitionContributor.find<GradleScriptDefinitionsContributor>(project) ?: return false
return ScriptDefinitionsManager.getInstance(project).getDefinitionsBy(contributor).any {
it.isScript(file.name)
}
}
}
// This service was a workaround for the bug in the platform: the notification 'Gradle project needs to be imported' wasn't shown
// after the changes in .gradle.kts files
// It was fixed in >183 (see GradleAutoImportAware.getAffectedExternalProjectFiles)
@@ -0,0 +1,120 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.core.script
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.Result
import com.intellij.openapi.application.WriteAction
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.EditorFactory
import com.intellij.openapi.editor.event.DocumentEvent
import com.intellij.openapi.editor.event.DocumentListener
import com.intellij.openapi.externalSystem.service.project.manage.ExternalProjectsManager
import com.intellij.openapi.externalSystem.util.ExternalSystemUtil
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.openapi.vfs.newvfs.BulkFileListener
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
import com.intellij.psi.PsiDocumentManager
import com.intellij.util.ui.update.MergingUpdateQueue
import com.intellij.util.ui.update.MergingUpdateQueue.ANY_COMPONENT
import com.intellij.util.ui.update.Update
import org.jetbrains.plugins.gradle.service.project.GradleAutoImportAware
fun initializeScriptModificationListener(project: Project) {
ServiceManager.getService(project, ScriptModificationListener::class.java)
}
class ScriptModificationListener(private val project: Project) {
private val changedDocuments = HashSet<Document>()
private val changedDocumentsQueue = MergingUpdateQueue("ScriptModificationListener: Scripts queue", 1000, false, ANY_COMPONENT, project)
init {
showNotificationIfScriptChangedListener()
saveScriptAfterModificationListener()
}
private fun showNotificationIfScriptChangedListener() {
project.messageBus.connect().subscribe(VirtualFileManager.VFS_CHANGES, object : BulkFileListener.Adapter() {
override fun after(events: List<VFileEvent>) {
if (ApplicationManager.getApplication().isUnitTestMode) return
val modifiedScripts = events.mapNotNull {
it.file?.takeIf { isGradleScript(it) }
}
// Workaround for IDEA-182367 (fixed in IDEA 181.3666)
if (modifiedScripts.isNotEmpty()) {
if (modifiedScripts.any {
GradleAutoImportAware().getAffectedExternalProjectPath(it.path, project) != null
}) {
return
}
ExternalProjectsManager.getInstance(project).externalProjectsWatcher.markDirty(project.basePath)
}
}
})
}
private fun saveScriptAfterModificationListener() {
// partially copied from ExternalSystemProjectsWatcherImpl before fix will be implemented in IDEA:
// "Gradle projects need to be imported" notification should be shown when kotlin script is modified
val busConnection = project.messageBus.connect(changedDocumentsQueue)
changedDocumentsQueue.activate()
EditorFactory.getInstance().eventMulticaster.addDocumentListener(object : DocumentListener {
override fun documentChanged(event: DocumentEvent) {
if (project.isDisposed) return
val doc = event.document
val file = FileDocumentManager.getInstance().getFile(doc) ?: return
if (isGradleScript(file) && event.newFragment.isNotBlank()) {
synchronized(changedDocuments) {
changedDocuments.add(doc)
}
changedDocumentsQueue.queue(object : Update(this) {
override fun run() {
var copy: Array<Document> = emptyArray()
synchronized(changedDocuments) {
copy = changedDocuments.toTypedArray()
changedDocuments.clear()
}
ExternalSystemUtil.invokeLater(project) {
object : WriteAction<Any>() {
override fun run(result: Result<Any>) {
for (each in copy) {
PsiDocumentManager.getInstance(project).commitDocument(each)
(FileDocumentManager.getInstance() as? FileDocumentManagerImpl)?.saveDocument(each, false)
}
}
}.execute()
}
}
})
}
}
}, busConnection)
}
private fun isGradleScript(file: VirtualFile): Boolean {
if (!ProjectRootManager.getInstance(project).fileIndex.isInContent(file)) return false
val contributor = ScriptDefinitionContributor.find<GradleScriptDefinitionsContributor>(project) ?: return false
return ScriptDefinitionsManager.getInstance(project).getDefinitionsBy(contributor).any {
it.isScript(file.name)
}
}
}
@@ -334,9 +334,6 @@
<projectService serviceInterface="org.jetbrains.kotlin.idea.core.util.ProjectJob"
serviceImplementation="org.jetbrains.kotlin.idea.core.util.ProjectJob"/>
<projectService serviceInterface="org.jetbrains.kotlin.idea.core.script.ScriptModificationListener"
serviceImplementation="org.jetbrains.kotlin.idea.core.script.ScriptModificationListener"/>
<projectService serviceInterface="org.jetbrains.kotlin.resolve.lazy.ProbablyNothingCallableNames"
serviceImplementation="org.jetbrains.kotlin.idea.project.ProbablyNothingCallableNamesImpl"/>
+5
View File
@@ -64,5 +64,10 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
interface="org.jetbrains.kotlin.idea.update.PluginUpdateVerifier"/>
</extensionPoints>
<extensions defaultExtensionNs="com.intellij">
<projectService serviceInterface="org.jetbrains.kotlin.idea.core.script.ScriptModificationListener"
serviceImplementation="org.jetbrains.kotlin.idea.core.script.ScriptModificationListener"/>
</extensions>
<xi:include href="plugin-kotlin-extensions.xml" xpointer="xpointer(/idea-plugin/*)"/>
</idea-plugin>
+6 -1
View File
@@ -65,5 +65,10 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
interface="org.jetbrains.kotlin.idea.update.PluginUpdateVerifier"/>
</extensionPoints>
<xi:include href="plugin-kotlin-extensions.xml" xpointer="xpointer(/idea-plugin/*)"/>
<xi:include href="plugin-kotlin-extensions.xml" xpointer="xpointer(/idea-plugin/*)"/>
<extensions defaultExtensionNs="com.intellij">
<projectService serviceInterface="org.jetbrains.kotlin.idea.core.script.ScriptModificationListener"
serviceImplementation="org.jetbrains.kotlin.idea.core.script.ScriptModificationListener"/>
</extensions>
</idea-plugin>
+5
View File
@@ -74,4 +74,9 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<extensions defaultExtensionNs="com.intellij.jvm">
<declarationSearcher language="kotlin" implementationClass="org.jetbrains.kotlin.idea.jvm.KotlinDeclarationSearcher"/>
</extensions>
<extensions defaultExtensionNs="com.intellij">
<projectService serviceInterface="org.jetbrains.kotlin.idea.core.script.ScriptModificationListener"
serviceImplementation="org.jetbrains.kotlin.idea.core.script.ScriptModificationListener"/>
</extensions>
</idea-plugin>
+5
View File
@@ -65,4 +65,9 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
</extensionPoints>
<xi:include href="plugin-kotlin-extensions.xml" xpointer="xpointer(/idea-plugin/*)"/>
<extensions defaultExtensionNs="com.intellij">
<projectService serviceInterface="org.jetbrains.kotlin.idea.core.script.ScriptModificationListener"
serviceImplementation="org.jetbrains.kotlin.idea.core.script.ScriptModificationListener"/>
</extensions>
</idea-plugin>
+5
View File
@@ -73,4 +73,9 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<extensions defaultExtensionNs="org.jetbrains.kotlin">
<pluginUpdateVerifier implementation="org.jetbrains.kotlin.idea.update.GooglePluginUpdateVerifier"/>
</extensions>
<extensions defaultExtensionNs="com.intellij">
<projectService serviceInterface="org.jetbrains.kotlin.idea.core.script.ScriptModificationListener"
serviceImplementation="org.jetbrains.kotlin.idea.core.script.ScriptModificationListener"/>
</extensions>
</idea-plugin>
+5
View File
@@ -78,4 +78,9 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<extensions defaultExtensionNs="com.intellij.jvm">
<declarationSearcher language="kotlin" implementationClass="org.jetbrains.kotlin.idea.jvm.KotlinDeclarationSearcher"/>
</extensions>
<extensions defaultExtensionNs="com.intellij">
<projectService serviceInterface="org.jetbrains.kotlin.idea.core.script.ScriptModificationListener"
serviceImplementation="org.jetbrains.kotlin.idea.core.script.ScriptModificationListener"/>
</extensions>
</idea-plugin>