Skip to content

Commit

Permalink
Merge #10760
Browse files Browse the repository at this point in the history
10760: Adjust intellisense file size limit for Rust files r=vlad20012 a=vlad20012

I finally figured out how to increase the file limit for Rust files. Now the limit for Rust files is 8MB. Previous attempt was #8699.

Fixes #10624

changelog: Adjust intellisense file size limit for Rust files


Co-authored-by: vlad20012 <beskvlad@gmail.com>
  • Loading branch information
bors[bot] and vlad20012 committed Jul 28, 2023
2 parents d024d4d + 9c0516b commit cc24993
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 40 deletions.
40 changes: 0 additions & 40 deletions src/main/kotlin/org/rust/cargo/toolchain/impl/CargoMetadata.kt
Expand Up @@ -9,8 +9,6 @@ import com.fasterxml.jackson.annotation.JsonProperty
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.vfs.*
import com.intellij.openapi.vfs.newvfs.RefreshQueue
import com.intellij.psi.SingleRootFileViewProvider
import com.intellij.util.PathUtil
import com.intellij.util.text.SemVer
import org.rust.cargo.CfgOptions
Expand All @@ -20,7 +18,6 @@ import org.rust.cargo.project.workspace.CargoWorkspace.LibKind
import org.rust.openapiext.RsPathManager
import org.rust.openapiext.findFileByMaybeRelativePath
import org.rust.stdext.HashCode
import org.rust.stdext.mapNotNullToSet
import org.rust.stdext.mapToSet
import java.io.IOException
import java.nio.file.Files
Expand Down Expand Up @@ -341,8 +338,6 @@ object CargoMetadata {
pkg.clean(fs, pkg.id in members, enabledFeatures, pkgBuildMessages)
}

adjustFileSizeLimitForFilesInOutDirs(packages)

return CargoWorkspaceData(
packages,
project.resolve.nodes.associate { node ->
Expand All @@ -362,41 +357,6 @@ object CargoMetadata {
)
}

/**
* Rust buildscripts (`build.rs`) often generate files that are larger than the default IntelliJ size limit.
* The default filesize limit is specified by [com.intellij.openapi.util.io.FileUtilRt.DEFAULT_INTELLISENSE_LIMIT]
* or "idea.max.intellisense.filesize" system property.
* Here we ensure that the file size limit is not less than [ADJUSTED_FILE_SIZE_LIMIT_FOR_OUTPUT_FILES] for
* cargo generated files.
*/
private fun adjustFileSizeLimitForFilesInOutDirs(packages: List<CargoWorkspaceData.Package>) {
if (PersistentFSConstants.getMaxIntellisenseFileSize() >= ADJUSTED_FILE_SIZE_LIMIT_FOR_OUTPUT_FILES) return

val outDirs = packages
.mapNotNull { it.outDirUrl }
.mapNotNullToSet { VirtualFileManager.getInstance().refreshAndFindFileByUrl(it) }

if (outDirs.isEmpty()) return

RefreshQueue.getInstance().refresh(false, true, null, outDirs)

for (outDir in outDirs) {
VfsUtilCore.visitChildrenRecursively(outDir,
object : VirtualFileVisitor<ArrayList<VirtualFile>>() {
override fun visitFile(outFile: VirtualFile): Boolean {
if (!outFile.isDirectory && outFile.length <= ADJUSTED_FILE_SIZE_LIMIT_FOR_OUTPUT_FILES) {
SingleRootFileViewProvider.doNotCheckFileSizeLimit(outFile)
}
return true
}
})
}
}

// Experimentally verified that 8Mb works with the default IDEA -Xmx768M. Larger values may
// lead to OOM, please verify before adjusting
private const val ADJUSTED_FILE_SIZE_LIMIT_FOR_OUTPUT_FILES: Int = 8 * 1024 * 1024

private fun Package.clean(
fs: LocalFileSystem,
isWorkspaceMember: Boolean,
Expand Down
@@ -0,0 +1,41 @@
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/

package org.rust.lang.core.psi

import com.intellij.lang.Language
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.FileViewProvider
import com.intellij.psi.FileViewProviderFactory
import com.intellij.psi.PsiManager
import com.intellij.psi.SingleRootFileViewProvider

/**
* Hacky adjust the file limit for Rust file.
* Coupled with [org.rust.lang.core.resolve.indexes.RsAliasIndex.getFileTypesWithSizeLimitNotApplicable].
*
* @see SingleRootFileViewProvider.isTooLargeForIntelligence
*/
class RsFileViewProviderFactory : FileViewProviderFactory {
override fun createFileViewProvider(
file: VirtualFile,
language: Language,
manager: PsiManager,
eventSystemEnabled: Boolean
): FileViewProvider {
val shouldAdjustFileLimit = SingleRootFileViewProvider.isTooLargeForIntelligence(file)
&& file.length <= RUST_FILE_SIZE_LIMIT_FOR_INTELLISENSE

if (shouldAdjustFileLimit) {
SingleRootFileViewProvider.doNotCheckFileSizeLimit(file)
}

return SingleRootFileViewProvider(manager, file, eventSystemEnabled)
}
}

// Experimentally verified that 8Mb works with the default IDEA -Xmx768M. Larger values may
// lead to OOM, please verify before adjusting
private const val RUST_FILE_SIZE_LIMIT_FOR_INTELLISENSE: Int = 8 * 1024 * 1024
Expand Up @@ -5,6 +5,7 @@

package org.rust.lang.core.resolve.indexes

import com.intellij.openapi.fileTypes.FileType
import com.intellij.openapi.project.Project
import com.intellij.psi.stubs.StubTree
import com.intellij.psi.stubs.StubTreeBuilder
Expand Down Expand Up @@ -76,6 +77,14 @@ class RsAliasIndex : FileBasedIndexExtension<TyFingerprint, List<String>>() {

override fun dependsOnFileContent(): Boolean = true

/**
* Hacky adjust the file limit for Rust file.
* Coupled with [org.rust.lang.core.psi.RsFileViewProviderFactory]
*/
override fun getFileTypesWithSizeLimitNotApplicable(): Collection<FileType> {
return listOf(RsFileType)
}

companion object {
fun findPotentialAliases(
project: Project,
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rust-core.xml
Expand Up @@ -43,6 +43,7 @@

<!-- PSI managing -->

<lang.fileViewProviderFactory language="Rust" implementationClass="org.rust.lang.core.psi.RsFileViewProviderFactory"/>
<projectService serviceInterface="org.rust.lang.core.psi.RsPsiManager"
serviceImplementation="org.rust.lang.core.psi.RsPsiManagerImpl"/>

Expand Down

0 comments on commit cc24993

Please sign in to comment.