diff --git a/platform/built-in-server/src/org/jetbrains/builtInWebServer/DefaultWebServerRootsProvider.kt b/platform/built-in-server/src/org/jetbrains/builtInWebServer/DefaultWebServerRootsProvider.kt index b97cac439bef..0fceb10e200b 100644 --- a/platform/built-in-server/src/org/jetbrains/builtInWebServer/DefaultWebServerRootsProvider.kt +++ b/platform/built-in-server/src/org/jetbrains/builtInWebServer/DefaultWebServerRootsProvider.kt @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2014 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.jetbrains.builtInWebServer import com.intellij.openapi.application.runReadAction @@ -33,7 +19,6 @@ import com.intellij.openapi.vfs.JarFileSystem import com.intellij.openapi.vfs.VfsUtilCore import com.intellij.openapi.vfs.VirtualFile import com.intellij.util.PlatformUtils -import com.intellij.util.containers.computeIfAny internal data class SuitableRoot(val file: VirtualFile, val moduleQualifier: String?) @@ -50,7 +35,7 @@ private class DefaultWebServerRootsProvider : WebServerRootsProvider() { if (module != null && !module.isDisposed) { effectivePath = effectivePath.substring(index + 1) val resolver = pathToFileManager.getResolver(effectivePath) - val result = RootProvider.values().computeIfAny { findByRelativePath(effectivePath, it.getRoots(module.rootManager), resolver, moduleName, pathQuery) } + val result = RootProvider.values().asSequence().map { findByRelativePath(effectivePath, it.getRoots(module.rootManager), resolver, moduleName, pathQuery) }.find { it != null } ?: findInModuleLibraries(effectivePath, module, resolver, pathQuery) if (result != null) { return result @@ -63,8 +48,8 @@ private class DefaultWebServerRootsProvider : WebServerRootsProvider() { val modules = runReadAction { ModuleManager.getInstance(project).modules } if (pathQuery.useVfs) { var oldestParent = path.indexOf("/").let { if (it > 0) path.substring(0, it) else null } - if (oldestParent == null && !path.isEmpty() && !path.contains('.')) { - // maybe it is top level directory? (in case of dart projects - web) + if (oldestParent == null && path.isNotEmpty() && !path.contains('.')) { + // maybe it is a top-level directory? (in case of dart projects - web) oldestParent = path } @@ -218,11 +203,11 @@ private fun findInModuleLibraries(path: String, module: Module, resolver: FileRe val libraryFileName = path.substring(0, index) val relativePath = path.substring(index + 1) - return ORDER_ROOT_TYPES.computeIfAny { + return ORDER_ROOT_TYPES.asSequence().map { findInModuleLevelLibraries(module, it) { root, _ -> if (StringUtil.equalsIgnoreCase(root.nameSequence, libraryFileName)) resolver.resolve(relativePath, root, isLibrary = true, pathQuery = pathQuery) else null } - } + }.find { it != null } } private fun findInLibraries(project: Project, path: String, resolver: FileResolver, pathQuery: PathQuery): PathInfo? { @@ -245,37 +230,42 @@ private fun getInfoForDocJar(file: VirtualFile, project: Project): PathInfo? { } } -internal fun getModuleNameQualifier(project: Project, module: Module?): String? { - if (module != null && PlatformUtils.isIntelliJ() && !(module.name.equals(project.name, ignoreCase = true) || compareNameAndProjectBasePath(module.name, project))) { - return module.name - } - return null -} +internal fun getModuleNameQualifier(project: Project, module: Module?): String? = + if (module != null && PlatformUtils.isIntelliJ() && + !(module.name.equals(project.name, ignoreCase = true) || compareNameAndProjectBasePath(module.name, project))) module.name + else null -private fun findByRelativePath(path: String, roots: Array, resolver: FileResolver, moduleName: String?, pathQuery: PathQuery) = roots.computeIfAny { resolver.resolve(path, it, moduleName, pathQuery = pathQuery) } +private fun findByRelativePath(path: String, roots: Array, resolver: FileResolver, moduleName: String?, pathQuery: PathQuery) = + roots.asSequence().map { resolver.resolve(path, it, moduleName, pathQuery = pathQuery) }.find { it != null } private fun findInLibrariesAndSdk(project: Project, rootTypes: Array, fileProcessor: (root: VirtualFile, module: Module?) -> PathInfo?): PathInfo? { - fun findInLibraryTable(table: LibraryTable, rootType: OrderRootType) = table.libraryIterator.computeIfAny { it.getFiles(rootType).computeIfAny { fileProcessor(it, null) } } + fun findInLibraryTable(table: LibraryTable, rootType: OrderRootType) = + table.libraryIterator.asSequence() + .flatMap { it.getFiles(rootType).asSequence() } + .map { fileProcessor(it, null) } + .find { it != null } fun findInProjectSdkOrInAll(rootType: OrderRootType): PathInfo? { - val inSdkFinder = { sdk: Sdk -> sdk.rootProvider.getFiles(rootType).computeIfAny { fileProcessor(it, null) } } + val inSdkFinder = { sdk: Sdk -> sdk.rootProvider.getFiles(rootType).asSequence().map { fileProcessor(it, null) }.find { it != null } } val projectSdk = ProjectRootManager.getInstance(project).projectSdk - return projectSdk?.let(inSdkFinder) ?: ProjectJdkTable.getInstance().allJdks.computeIfAny { if (it === projectSdk) null else inSdkFinder(it) } + return projectSdk?.let(inSdkFinder) + ?: ProjectJdkTable.getInstance().allJdks.asSequence().filter { it === projectSdk }.map { inSdkFinder(it) }.find { it != null } } - return rootTypes.computeIfAny { rootType -> + return rootTypes.asSequence().map { rootType -> runReadAction { findInLibraryTable(LibraryTablesRegistrar.getInstance().getLibraryTable(project), rootType) - ?: findInProjectSdkOrInAll(rootType) - ?: ModuleManager.getInstance(project).modules.computeIfAny { if (it.isDisposed) null else findInModuleLevelLibraries(it, rootType, fileProcessor) } - ?: findInLibraryTable(LibraryTablesRegistrar.getInstance().libraryTable, rootType) + ?: findInProjectSdkOrInAll(rootType) + ?: ModuleManager.getInstance(project).modules.asSequence().filter { !it.isDisposed }.map { findInModuleLevelLibraries(it, rootType, fileProcessor) }.find { it != null } + ?: findInLibraryTable(LibraryTablesRegistrar.getInstance().libraryTable, rootType) } - } + }.find { it != null } } -private fun findInModuleLevelLibraries(module: Module, rootType: OrderRootType, fileProcessor: (root: VirtualFile, module: Module?) -> PathInfo?): PathInfo? { - return module.rootManager.orderEntries.computeIfAny { - if (it is LibraryOrderEntry && it.isModuleLevel) it.getFiles(rootType).computeIfAny { fileProcessor(it, module) } else null - } -} \ No newline at end of file +private fun findInModuleLevelLibraries(module: Module, rootType: OrderRootType, fileProcessor: (root: VirtualFile, module: Module?) -> PathInfo?): PathInfo? = + module.rootManager.orderEntries.asSequence() + .filter { it is LibraryOrderEntry && it.isModuleLevel } + .flatMap { it.getFiles(rootType).asSequence() } + .map { fileProcessor(it, module) } + .find { it != null } \ No newline at end of file diff --git a/platform/built-in-server/src/org/jetbrains/builtInWebServer/WebServerPathToFileManager.kt b/platform/built-in-server/src/org/jetbrains/builtInWebServer/WebServerPathToFileManager.kt index 30a79fa41f2c..57a64baa3cec 100644 --- a/platform/built-in-server/src/org/jetbrains/builtInWebServer/WebServerPathToFileManager.kt +++ b/platform/built-in-server/src/org/jetbrains/builtInWebServer/WebServerPathToFileManager.kt @@ -21,10 +21,10 @@ import com.intellij.openapi.vfs.newvfs.BulkFileListener import com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent import com.intellij.openapi.vfs.newvfs.events.VFileEvent import com.intellij.util.SmartList -import com.intellij.util.containers.computeIfAny import com.intellij.util.io.exists import java.nio.file.Paths import java.util.concurrent.TimeUnit +import kotlin.streams.asSequence private const val cacheSize: Long = 4096 * 4 @@ -138,7 +138,7 @@ class WebServerPathToFileManager(private val project: Project) { fun getPathInfo(child: VirtualFile): PathInfo? { var result = virtualFileToPathInfo.getIfPresent(child) if (result == null) { - result = WebServerRootsProvider.EP_NAME.extensions.computeIfAny { it.getPathInfo(child, project) } + result = WebServerRootsProvider.EP_NAME.extensions().asSequence().map { it.getPathInfo(child, project) }.find { it != null } if (result != null) { virtualFileToPathInfo.put(child, result) } @@ -147,7 +147,7 @@ class WebServerPathToFileManager(private val project: Project) { } internal fun doFindByRelativePath(path: String, pathQuery: PathQuery): PathInfo? { - val result = WebServerRootsProvider.EP_NAME.extensions.computeIfAny { it.resolve(path, project, pathQuery) } ?: return null + val result = WebServerRootsProvider.EP_NAME.extensions().asSequence().map { it.resolve(path, project, pathQuery) }.find { it != null } ?: return null result.file?.let { virtualFileToPathInfo.put(it, result) } diff --git a/platform/configuration-store-impl/src/ModuleStoreImpl.kt b/platform/configuration-store-impl/src/ModuleStoreImpl.kt index 8d34e49a39ca..5d764bc82eb5 100644 --- a/platform/configuration-store-impl/src/ModuleStoreImpl.kt +++ b/platform/configuration-store-impl/src/ModuleStoreImpl.kt @@ -6,9 +6,9 @@ import com.intellij.openapi.components.impl.stores.ModuleStore import com.intellij.openapi.diagnostic.runAndLogException import com.intellij.openapi.module.Module import com.intellij.project.isDirectoryBased -import com.intellij.util.containers.computeIfAny import com.intellij.util.io.exists import java.nio.file.Paths +import kotlin.streams.asSequence private val MODULE_FILE_STORAGE_ANNOTATION = FileStorageAnnotation(StoragePathMacros.MODULE_FILE, false) @@ -23,14 +23,13 @@ private open class ModuleStoreImpl(module: Module) : ModuleStoreBase() { // todo what about Upsource? For now this implemented not in the ModuleStoreBase because `project` and `module` are available only in this class (ModuleStoreImpl) override fun getStorageSpecs(component: PersistentStateComponent, stateSpec: State, operation: StateStorageOperation): List { - val result = super.getStorageSpecs(component, stateSpec, operation) + val result = super.getStorageSpecs(component, stateSpec, operation) if (!project.isDirectoryBased) { return result } - - return StreamProviderFactory.EP_NAME.getExtensions(project).computeIfAny { - LOG.runAndLogException { it.customizeStorageSpecs(component, storageManager, stateSpec, result, operation) } - } ?: result + return StreamProviderFactory.EP_NAME.extensions(project).asSequence() + .map { LOG.runAndLogException { it.customizeStorageSpecs(component, storageManager, stateSpec, result, operation) } } + .find { it != null } ?: result } } @@ -39,7 +38,6 @@ private class TestModuleStore(module: Module) : ModuleStoreImpl(module) { override fun setPath(path: String, isNew: Boolean) { super.setPath(path, isNew) - if (!isNew && Paths.get(path).exists()) { moduleComponentLoadPolicy = StateLoadPolicy.LOAD } @@ -53,19 +51,11 @@ private class TestModuleStore(module: Module) : ModuleStoreImpl(module) { abstract class ModuleStoreBase : ChildlessComponentStore(), ModuleStore { abstract override val storageManager: StateStorageManagerImpl - override fun getStorageSpecs(component: PersistentStateComponent, stateSpec: State, operation: StateStorageOperation): List { - val storages = stateSpec.storages - return if (storages.isEmpty()) { - listOf(MODULE_FILE_STORAGE_ANNOTATION) - } - else { - super.getStorageSpecs(component, stateSpec, operation) - } - } + override fun getStorageSpecs(component: PersistentStateComponent, stateSpec: State, operation: StateStorageOperation): List = + if (stateSpec.storages.isEmpty()) listOf(MODULE_FILE_STORAGE_ANNOTATION) + else super.getStorageSpecs(component, stateSpec, operation) - final override fun setPath(path: String) { - setPath(path, false) - } + final override fun setPath(path: String) = setPath(path, false) override fun setPath(path: String, isNew: Boolean) { val isMacroAdded = storageManager.addMacro(StoragePathMacros.MODULE_FILE, path) @@ -81,7 +71,8 @@ abstract class ModuleStoreBase : ChildlessComponentStore(), ModuleStore { // https://youtrack.jetbrains.com/issue/IDEA-147530 if (isMacroAdded) { - // preload to ensure that we will get FileNotFound error (no module file) during init, and not later in some unexpected place (because otherwise will be loaded by demand) + // preload to ensure that we will get FileNotFound error (no module file) during initialization, + // and not later in some unexpected place (because otherwise will be loaded by demand) preloadStorageData(isNew) } else { diff --git a/platform/configuration-store-impl/src/ProjectStoreBase.kt b/platform/configuration-store-impl/src/ProjectStoreBase.kt index 92310a77990a..25bf1c9c7635 100644 --- a/platform/configuration-store-impl/src/ProjectStoreBase.kt +++ b/platform/configuration-store-impl/src/ProjectStoreBase.kt @@ -13,14 +13,12 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.project.ProjectCoreUtil import com.intellij.openapi.project.getProjectCacheFileName import com.intellij.openapi.util.io.FileUtil -import com.intellij.openapi.util.io.FileUtilRt import com.intellij.openapi.util.registry.Registry import com.intellij.openapi.vfs.LocalFileSystem import com.intellij.openapi.vfs.VfsUtil import com.intellij.openapi.vfs.VirtualFile -import com.intellij.util.PathUtilRt +import com.intellij.util.PathUtil import com.intellij.util.SmartList -import com.intellij.util.containers.computeIfAny import com.intellij.util.containers.isNullOrEmpty import com.intellij.util.io.exists import com.intellij.util.io.move @@ -29,6 +27,7 @@ import com.intellij.util.text.nullize import kotlinx.coroutines.runBlocking import java.nio.file.Path import java.nio.file.Paths +import kotlin.streams.asSequence internal const val PROJECT_FILE = "\$PROJECT_FILE$" internal const val PROJECT_CONFIG_DIR = "\$PROJECT_CONFIG_DIR$" @@ -38,7 +37,7 @@ private val DEPRECATED_PROJECT_FILE_STORAGE_ANNOTATION = FileStorageAnnotation(P // cannot be `internal`, used in Upsource abstract class ProjectStoreBase(final override val project: Project) : ComponentStoreWithExtraComponents(), IProjectStore { - // protected setter used in upsource + // the protected setter used in Upsource // Zelix KlassMaster - ERROR: Could not find method 'getScheme()' var scheme = StorageScheme.DEFAULT @@ -69,9 +68,7 @@ abstract class ProjectStoreBase(final override val project: Project) : Component final override fun getWorkspaceFilePath() = storageManager.expandMacro(StoragePathMacros.WORKSPACE_FILE) - final override fun clearStorages() { - storageManager.clearStorages() - } + final override fun clearStorages() = storageManager.clearStorages() private fun loadProjectFromTemplate(defaultProject: Project) { val stateStore = defaultProject.stateStore as DefaultProjectStoreImpl @@ -92,15 +89,12 @@ abstract class ProjectStoreBase(final override val project: Project) : Component final override fun getProjectBasePath(): String { if (isDirectoryBased) { - val path = PathUtilRt.getParentPath(storageManager.expandMacro(PROJECT_CONFIG_DIR)) - if (Registry.`is`("store.basedir.parent.detection", true) && PathUtilRt.getFileName( - path).startsWith("${Project.DIRECTORY_STORE_FOLDER}.")) { - return PathUtilRt.getParentPath(PathUtilRt.getParentPath(path)) - } - return path + val path = PathUtil.getParentPath(storageManager.expandMacro(PROJECT_CONFIG_DIR)) + val parent = Registry.`is`("store.basedir.parent.detection", true) && PathUtil.getFileName(path).startsWith("${Project.DIRECTORY_STORE_FOLDER}.") + return if (parent) PathUtil.getParentPath(PathUtil.getParentPath(path)) else path } else { - return PathUtilRt.getParentPath(projectFilePath) + return PathUtil.getParentPath(projectFilePath) } } @@ -194,7 +188,7 @@ abstract class ProjectStoreBase(final override val project: Project) : Component if (storages.size == 2 && ApplicationManager.getApplication().isUnitTestMode && isSpecialStorage(storages.first()) && - storages.get(1).path == StoragePathMacros.WORKSPACE_FILE) { + storages[1].path == StoragePathMacros.WORKSPACE_FILE) { return listOf(storages.first()) } @@ -213,12 +207,10 @@ abstract class ProjectStoreBase(final override val project: Project) : Component else { result!!.sortWith(deprecatedComparator) if (isDirectoryBased) { - StreamProviderFactory.EP_NAME.getExtensions(project).computeIfAny { - LOG.runAndLogException { it.customizeStorageSpecs(component, storageManager, stateSpec, result!!, operation) } - }?.let { - // yes, DEPRECATED_PROJECT_FILE_STORAGE_ANNOTATION is not added in this case - return it - } + StreamProviderFactory.EP_NAME.extensions(project).asSequence() + .map { LOG.runAndLogException { it.customizeStorageSpecs(component, storageManager, stateSpec, result!!, operation) } } + .find { it != null } + ?.let { return it } // yes, DEPRECATED_PROJECT_FILE_STORAGE_ANNOTATION is not added in this case } // if we create project from default, component state written not to own storage file, but to project file, @@ -267,29 +259,24 @@ abstract class ProjectStoreBase(final override val project: Project) : Component if (!isDirectoryBased) { return filePath == projectFilePath || filePath == workspaceFilePath } - return FileUtil.isAncestor(PathUtilRt.getParentPath(projectFilePath), filePath, false) + + return FileUtil.isAncestor(PathUtil.getParentPath(projectFilePath), filePath, false) } - override fun getDirectoryStorePath(ignoreProjectStorageScheme: Boolean): String? { - return when { - !ignoreProjectStorageScheme && !isDirectoryBased -> null - else -> PathUtilRt.getParentPath(projectFilePath).nullize() - } - } + override fun getDirectoryStorePath(ignoreProjectStorageScheme: Boolean): String? = + if (!ignoreProjectStorageScheme && !isDirectoryBased) null + else PathUtil.getParentPath(projectFilePath).nullize() override fun getDirectoryStoreFile(): VirtualFile? = directoryStorePath?.let { LocalFileSystem.getInstance().findFileByPath(it) } - override fun getDirectoryStorePathOrBase(): String = PathUtilRt.getParentPath(projectFilePath) + override fun getDirectoryStorePathOrBase(): String = PathUtil.getParentPath(projectFilePath) - override suspend fun doSave(result: SaveResult, forceSavingAllSettings: Boolean) { - // do nothing, dummy implementation for Upsource - } + override suspend fun doSave(result: SaveResult, forceSavingAllSettings: Boolean) { } // dummy implementation for Upsource } -private fun composeFileBasedProjectWorkSpacePath(filePath: String) = "${FileUtilRt.getNameWithoutExtension(filePath)}${WorkspaceFileType.DOT_DEFAULT_EXTENSION}" +private fun composeFileBasedProjectWorkSpacePath(filePath: String) = "${FileUtil.getNameWithoutExtension(filePath)}${WorkspaceFileType.DOT_DEFAULT_EXTENSION}" private fun isSpecialStorage(storage: Storage) = isSpecialStorage(storage.path) -internal fun isSpecialStorage(collapsedPath: String): Boolean { - return collapsedPath == StoragePathMacros.CACHE_FILE || collapsedPath == StoragePathMacros.PRODUCT_WORKSPACE_FILE -} \ No newline at end of file +internal fun isSpecialStorage(collapsedPath: String): Boolean = + collapsedPath == StoragePathMacros.CACHE_FILE || collapsedPath == StoragePathMacros.PRODUCT_WORKSPACE_FILE \ No newline at end of file diff --git a/platform/util-ex/src/com/intellij/util/containers/util.kt b/platform/util-ex/src/com/intellij/util/containers/util.kt index 80ac3f9eed74..36094feeb145 100644 --- a/platform/util-ex/src/com/intellij/util/containers/util.kt +++ b/platform/util-ex/src/com/intellij/util/containers/util.kt @@ -27,36 +27,6 @@ fun MutableMap>.putValue(key: K, value: V) { fun Collection<*>?.isNullOrEmpty(): Boolean = this == null || isEmpty() -inline fun Iterator.computeIfAny(processor: (T) -> R): R? { - for (item in this) { - val result = processor(item) - if (result != null) { - return result - } - } - return null -} - -inline fun Array.computeIfAny(processor: (T) -> R): R? { - for (file in this) { - val result = processor(file) - if (result != null) { - return result - } - } - return null -} - -inline fun List.computeIfAny(processor: (T) -> R): R? { - for (item in this) { - val result = processor(item) - if (result != null) { - return result - } - } - return null -} - val List.tail: List get() = this.subList(1, this.size) fun List.toHeadAndTail(): Pair>? = if (this.isEmpty()) null else this.first() to this.tail @@ -130,7 +100,7 @@ fun Stream?.getIfSingle(): T? = * There probably could be some performance issues if there is lots of streams to concat. See * http://mail.openjdk.java.net/pipermail/lambda-dev/2013-July/010659.html for some details. * - * Also see [Stream.concat] documentation for other possible issues of concatenating large number of streams. + * See also [Stream.concat] documentation for other possible issues of concatenating large number of streams. */ fun concat(vararg streams: Stream): Stream = Stream.of(*streams).reduce(Stream.empty()) { a, b -> Stream.concat(a, b) } @@ -270,4 +240,4 @@ inline fun Iterator.stopAfter(crossinline predicate: (T) -> Boolean): Ite break } } -} +} \ No newline at end of file