Minor: reformat
This commit is contained in:
+51
-52
@@ -48,17 +48,17 @@ import kotlin.script.experimental.dependencies.DependenciesResolver
|
|||||||
import kotlin.script.experimental.dependencies.ScriptDependencies
|
import kotlin.script.experimental.dependencies.ScriptDependencies
|
||||||
|
|
||||||
class ScriptDependenciesUpdater(
|
class ScriptDependenciesUpdater(
|
||||||
private val project: Project,
|
private val project: Project,
|
||||||
private val cache: ScriptDependenciesCache,
|
private val cache: ScriptDependenciesCache,
|
||||||
private val scriptDefinitionProvider: ScriptDefinitionProvider
|
private val scriptDefinitionProvider: ScriptDefinitionProvider
|
||||||
) {
|
) {
|
||||||
private val requests = ConcurrentHashMap<String, ModStampedRequest>()
|
private val requests = ConcurrentHashMap<String, ModStampedRequest>()
|
||||||
private val contentLoader = ScriptContentLoader(project)
|
private val contentLoader = ScriptContentLoader(project)
|
||||||
private val asyncUpdatesDispatcher = Executors.newFixedThreadPool(1).asCoroutineDispatcher()
|
private val asyncUpdatesDispatcher = Executors.newFixedThreadPool(1).asCoroutineDispatcher()
|
||||||
private val legacyUpdatesDispatcher =
|
private val legacyUpdatesDispatcher =
|
||||||
Executors.newFixedThreadPool(
|
Executors.newFixedThreadPool(
|
||||||
(Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(1)
|
(Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(1)
|
||||||
).asCoroutineDispatcher()
|
).asCoroutineDispatcher()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
listenToVfsChanges()
|
listenToVfsChanges()
|
||||||
@@ -69,8 +69,8 @@ class ScriptDependenciesUpdater(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private class ModStampedRequest(
|
private class ModStampedRequest(
|
||||||
val modificationStamp: Long,
|
val modificationStamp: Long,
|
||||||
val job: TimeStampedJob?
|
val job: TimeStampedJob?
|
||||||
) {
|
) {
|
||||||
fun cancel() = job?.actualJob?.cancel()
|
fun cancel() = job?.actualJob?.cancel()
|
||||||
}
|
}
|
||||||
@@ -90,7 +90,8 @@ class ScriptDependenciesUpdater(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun tryUsingDefault(file: VirtualFile) {
|
private fun tryUsingDefault(file: VirtualFile) {
|
||||||
val defaults = DefaultScriptDependenciesProvider.getInstances(project).firstNotNullResult { it.defaultDependenciesFor(file) } ?: return
|
val defaults =
|
||||||
|
DefaultScriptDependenciesProvider.getInstances(project).firstNotNullResult { it.defaultDependenciesFor(file) } ?: return
|
||||||
saveToCache(defaults, file)
|
saveToCache(defaults, file)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,17 +110,15 @@ class ScriptDependenciesUpdater(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun requestUpdate(files: Iterable<VirtualFile>) =
|
private fun requestUpdate(files: Iterable<VirtualFile>) =
|
||||||
files.map { file ->
|
files.map { file ->
|
||||||
if (!file.isValid) {
|
if (!file.isValid) {
|
||||||
return cache.delete(file)
|
return cache.delete(file)
|
||||||
}
|
} else if (cache[file] != null) { // only update dependencies for scripts that were touched recently
|
||||||
else if (cache[file] != null) { // only update dependencies for scripts that were touched recently
|
performUpdate(file)
|
||||||
performUpdate(file)
|
} else {
|
||||||
}
|
false
|
||||||
else {
|
}
|
||||||
false
|
}.contains(true)
|
||||||
}
|
|
||||||
}.contains(true)
|
|
||||||
|
|
||||||
private fun performUpdate(file: VirtualFile): Boolean {
|
private fun performUpdate(file: VirtualFile): Boolean {
|
||||||
val scriptDef = scriptDefinitionProvider.findScriptDefinition(file) ?: return false
|
val scriptDef = scriptDefinitionProvider.findScriptDefinition(file) ?: return false
|
||||||
@@ -134,8 +133,8 @@ class ScriptDependenciesUpdater(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun updateAsync(
|
private fun updateAsync(
|
||||||
file: VirtualFile,
|
file: VirtualFile,
|
||||||
scriptDefinition: KotlinScriptDefinition
|
scriptDefinition: KotlinScriptDefinition
|
||||||
) {
|
) {
|
||||||
val path = file.path
|
val path = file.path
|
||||||
val lastRequest = requests[path]
|
val lastRequest = requests[path]
|
||||||
@@ -157,8 +156,8 @@ class ScriptDependenciesUpdater(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun sendRequest(
|
private fun sendRequest(
|
||||||
file: VirtualFile,
|
file: VirtualFile,
|
||||||
scriptDef: KotlinScriptDefinition
|
scriptDef: KotlinScriptDefinition
|
||||||
): TimeStampedJob {
|
): TimeStampedJob {
|
||||||
val currentTimeStamp = TimeStamps.next()
|
val currentTimeStamp = TimeStamps.next()
|
||||||
|
|
||||||
@@ -167,10 +166,9 @@ class ScriptDependenciesUpdater(
|
|||||||
val environment = contentLoader.getEnvironment(scriptDef)
|
val environment = contentLoader.getEnvironment(scriptDef)
|
||||||
val newJob = if (dependenciesResolver is AsyncDependenciesResolver) {
|
val newJob = if (dependenciesResolver is AsyncDependenciesResolver) {
|
||||||
launchAsyncUpdate(asyncUpdatesDispatcher, file, currentTimeStamp, scriptDef) {
|
launchAsyncUpdate(asyncUpdatesDispatcher, file, currentTimeStamp, scriptDef) {
|
||||||
dependenciesResolver.resolveAsync(scriptContents, environment)
|
dependenciesResolver.resolveAsync(scriptContents, environment)
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
assert(dependenciesResolver is LegacyResolverWrapper)
|
assert(dependenciesResolver is LegacyResolverWrapper)
|
||||||
launchAsyncUpdate(legacyUpdatesDispatcher, file, currentTimeStamp, scriptDef) {
|
launchAsyncUpdate(legacyUpdatesDispatcher, file, currentTimeStamp, scriptDef) {
|
||||||
dependenciesResolver.resolve(scriptContents, environment)
|
dependenciesResolver.resolve(scriptContents, environment)
|
||||||
@@ -180,16 +178,15 @@ class ScriptDependenciesUpdater(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun launchAsyncUpdate(
|
private fun launchAsyncUpdate(
|
||||||
dispatcher: CoroutineDispatcher,
|
dispatcher: CoroutineDispatcher,
|
||||||
file: VirtualFile,
|
file: VirtualFile,
|
||||||
currentTimeStamp: TimeStamp,
|
currentTimeStamp: TimeStamp,
|
||||||
scriptDef: KotlinScriptDefinition,
|
scriptDef: KotlinScriptDefinition,
|
||||||
doResolve: suspend () -> DependenciesResolver.ResolveResult
|
doResolve: suspend () -> DependenciesResolver.ResolveResult
|
||||||
) = launch(dispatcher + project.cancelOnDisposal) {
|
) = launch(dispatcher + project.cancelOnDisposal) {
|
||||||
val result = try {
|
val result = try {
|
||||||
doResolve()
|
doResolve()
|
||||||
}
|
} catch (t: Throwable) {
|
||||||
catch (t: Throwable) {
|
|
||||||
t.asResolveFailure(scriptDef)
|
t.asResolveFailure(scriptDef)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,10 +194,10 @@ class ScriptDependenciesUpdater(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun processResult(
|
private fun processResult(
|
||||||
file: VirtualFile,
|
file: VirtualFile,
|
||||||
currentTimeStamp: TimeStamp,
|
currentTimeStamp: TimeStamp,
|
||||||
result: DependenciesResolver.ResolveResult,
|
result: DependenciesResolver.ResolveResult,
|
||||||
scriptDef: KotlinScriptDefinition
|
scriptDef: KotlinScriptDefinition
|
||||||
) {
|
) {
|
||||||
val lastRequest = requests[file.path]
|
val lastRequest = requests[file.path]
|
||||||
val lastTimeStamp = lastRequest?.job?.timeStamp
|
val lastTimeStamp = lastRequest?.job?.timeStamp
|
||||||
@@ -225,8 +222,8 @@ class ScriptDependenciesUpdater(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun saveNewDependencies(
|
private fun saveNewDependencies(
|
||||||
new: ScriptDependencies,
|
new: ScriptDependencies,
|
||||||
file: VirtualFile
|
file: VirtualFile
|
||||||
): Boolean {
|
): Boolean {
|
||||||
val rootsChanged = cache.hasNotCachedRoots(new)
|
val rootsChanged = cache.hasNotCachedRoots(new)
|
||||||
if (cache.save(file, new)) {
|
if (cache.save(file, new)) {
|
||||||
@@ -248,8 +245,7 @@ class ScriptDependenciesUpdater(
|
|||||||
val application = ApplicationManager.getApplication()
|
val application = ApplicationManager.getApplication()
|
||||||
if (application.isUnitTestMode) {
|
if (application.isUnitTestMode) {
|
||||||
rootsChangesRunnable.invoke()
|
rootsChangesRunnable.invoke()
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
launch(EDT(project)) {
|
launch(EDT(project)) {
|
||||||
rootsChangesRunnable()
|
rootsChangesRunnable()
|
||||||
}
|
}
|
||||||
@@ -267,13 +263,13 @@ class ScriptDependenciesUpdater(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (requestUpdate(events.mapNotNull {
|
if (requestUpdate(events.mapNotNull {
|
||||||
// The check is partly taken from the BuildManager.java
|
// The check is partly taken from the BuildManager.java
|
||||||
it.file?.takeIf {
|
it.file?.takeIf {
|
||||||
// the isUnitTestMode check fixes ScriptConfigurationHighlighting & Navigation tests, since they are not trigger proper update mechanims
|
// the isUnitTestMode check fixes ScriptConfigurationHighlighting & Navigation tests, since they are not trigger proper update mechanims
|
||||||
// TODO: find out the reason, then consider to fix tests and remove this check
|
// TODO: find out the reason, then consider to fix tests and remove this check
|
||||||
(application.isUnitTestMode || projectFileIndex.isInContent(it)) && !isProjectOrWorkspaceFile(it)
|
(application.isUnitTestMode || projectFileIndex.isInContent(it)) && !isProjectOrWorkspaceFile(it)
|
||||||
}
|
}
|
||||||
})) {
|
})) {
|
||||||
notifyRootsChanged()
|
notifyRootsChanged()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -297,14 +293,17 @@ private object TimeStamps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@set: TestOnly
|
@set: TestOnly
|
||||||
var Application.isScriptDependenciesUpdaterDisabled by NotNullableUserDataProperty(Key.create("SCRIPT_DEPENDENCIES_UPDATER_DISABLED"), false)
|
var Application.isScriptDependenciesUpdaterDisabled by NotNullableUserDataProperty(
|
||||||
|
Key.create("SCRIPT_DEPENDENCIES_UPDATER_DISABLED"),
|
||||||
|
false
|
||||||
|
)
|
||||||
|
|
||||||
interface DefaultScriptDependenciesProvider {
|
interface DefaultScriptDependenciesProvider {
|
||||||
fun defaultDependenciesFor(scriptFile: VirtualFile): ScriptDependencies?
|
fun defaultDependenciesFor(scriptFile: VirtualFile): ScriptDependencies?
|
||||||
|
|
||||||
companion object : ProjectExtensionDescriptor<DefaultScriptDependenciesProvider>(
|
companion object : ProjectExtensionDescriptor<DefaultScriptDependenciesProvider>(
|
||||||
"org.jetbrains.kotlin.defaultScriptDependenciesProvider",
|
"org.jetbrains.kotlin.defaultScriptDependenciesProvider",
|
||||||
DefaultScriptDependenciesProvider::class.java
|
DefaultScriptDependenciesProvider::class.java
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user