From 046db96825c428f65d7cf854ae6b54df04d46444 Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Wed, 28 Oct 2026 11:25:27 +0000 Subject: [PATCH 1/2] ADFA-6564: Spotless reformat of ProjectStructureProvider, no functional change --- .../services/ProjectStructureProvider.kt | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProvider.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProvider.kt index e9b5704529..d8aca5d964 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProvider.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProvider.kt @@ -21,12 +21,12 @@ import java.nio.file.Paths import java.util.concurrent.ConcurrentHashMap import kotlin.io.path.pathString -internal class ProjectStructureProvider : KtLspService, KotlinProjectStructureProviderBase() { - +internal class ProjectStructureProvider : + KotlinProjectStructureProviderBase(), + KtLspService { companion object { - fun getInstance(project: Project): ProjectStructureProvider { - return KotlinProjectStructureProvider.getInstance(project) as ProjectStructureProvider - } + fun getInstance(project: Project): ProjectStructureProvider = + KotlinProjectStructureProvider.getInstance(project) as ProjectStructureProvider } private lateinit var modules: List @@ -35,7 +35,10 @@ internal class ProjectStructureProvider : KtLspService, KotlinProjectStructurePr private val inMemoryVfToModule = ConcurrentHashMap() private val pathToInMemoryVf = ConcurrentHashMap() - fun registerInMemoryFile(sourcePath: String, vf: VirtualFile) { + fun registerInMemoryFile( + sourcePath: String, + vf: VirtualFile, + ) { pathToInMemoryVf.remove(sourcePath)?.let { inMemoryVfToModule.remove(it) } val module = findModuleForSourceId(sourcePath) ?: return @@ -61,7 +64,7 @@ internal class ProjectStructureProvider : KtLspService, KotlinProjectStructurePr project: MockProject, index: KtSymbolIndex, modules: List, - libraryRoots: List + libraryRoots: List, ) { this.modules = modules this.project = project @@ -69,22 +72,24 @@ internal class ProjectStructureProvider : KtLspService, KotlinProjectStructurePr override fun getModule( element: PsiElement, - useSiteModule: KaModule? + useSiteModule: KaModule?, ): KaModule = getModuleImpl(element, useSiteModule) private fun getModuleImpl( element: PsiElement, - useSiteModule: KaModule? + useSiteModule: KaModule?, ): KaModule { - val virtualFile = element.containingFile?.virtualFile - ?: return notUnderContentRootModuleWithoutPsiFile + val virtualFile = + element.containingFile?.virtualFile + ?: return notUnderContentRootModuleWithoutPsiFile // Fast path: in-memory file registered by onFileContentChanged. inMemoryVfToModule[virtualFile]?.let { return it } - val backingFilePath = (element.containingFile as? KtFile)?.let { - it.backingFilePath ?: it.originalKtFile?.backingFilePath - } + val backingFilePath = + (element.containingFile as? KtFile)?.let { + it.backingFilePath ?: it.originalKtFile?.backingFilePath + } if (backingFilePath != null) { findModuleForSourceId(backingFilePath.pathString)?.let { return it } @@ -148,14 +153,12 @@ internal class ProjectStructureProvider : KtLspService, KotlinProjectStructurePr } @OptIn(KaPlatformInterface::class) - override fun getNotUnderContentRootModule(project: Project): KaNotUnderContentRootModule { - return notUnderContentRootModuleWithoutPsiFile - } + override fun getNotUnderContentRootModule(project: Project): KaNotUnderContentRootModule = notUnderContentRootModuleWithoutPsiFile private fun searchVirtualFileInModule( vf: VirtualFile, module: KaModule, - visited: MutableSet + visited: MutableSet, ): KaModule? { if (visited.contains(module)) return null if (module.contentScope.contains(vf)) return module From 237e1dff7230ce6f19bca96d42d14f9568e522a2 Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Wed, 28 Oct 2026 11:26:41 +0000 Subject: [PATCH 2/2] ADFA-6564: Analyse completion's file copy in a dangling module Typing any non-newline character triggers completion, which analyses a placeholder copy of the file. getModuleImpl mapped that copy into its original's source module through originalKtFile.backingFilePath, so when completion was the first analysis after an edit minted a new KtFile, FIR saw the class declared twice and diagnostics reported the whole class as a redeclaration of itself. Copies now go through computeSpecialModule, which gives them a dangling module whose context is the original's. Live files are unaffected: the check is on copyOrigin, not isDangling, because the live in-memory files report isDangling too. --- .../services/ProjectStructureProvider.kt | 9 +- .../index/InBodyEditRedeclarationTest.kt | 82 +++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/InBodyEditRedeclarationTest.kt diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProvider.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProvider.kt index d8aca5d964..a744793ceb 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProvider.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProvider.kt @@ -10,6 +10,7 @@ import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinProject import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinProjectStructureProviderBase import org.jetbrains.kotlin.analysis.api.projectStructure.KaModule import org.jetbrains.kotlin.analysis.api.projectStructure.KaNotUnderContentRootModule +import org.jetbrains.kotlin.analysis.api.projectStructure.copyOrigin import org.jetbrains.kotlin.analysis.low.level.api.fir.util.originalKtFile import org.jetbrains.kotlin.cli.jvm.index.JavaRoot import org.jetbrains.kotlin.com.intellij.mock.MockProject @@ -79,8 +80,14 @@ internal class ProjectStructureProvider : element: PsiElement, useSiteModule: KaModule?, ): KaModule { + // A copy in its original's source module makes FIR see every declaration twice; give it a dangling module. + val containingFile = element.containingFile + if (containingFile is KtFile && containingFile.copyOrigin != null) { + computeSpecialModule(containingFile)?.let { return it } + } + val virtualFile = - element.containingFile?.virtualFile + containingFile?.virtualFile ?: return notUnderContentRootModuleWithoutPsiFile // Fast path: in-memory file registered by onFileContentChanged. diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/InBodyEditRedeclarationTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/InBodyEditRedeclarationTest.kt new file mode 100644 index 0000000000..6f7069cc85 --- /dev/null +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/InBodyEditRedeclarationTest.kt @@ -0,0 +1,82 @@ +package com.itsaky.androidide.lsp.kotlin.compiler.index + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.eventbus.events.editor.ChangeType +import com.itsaky.androidide.eventbus.events.editor.DocumentChangeEvent +import com.itsaky.androidide.eventbus.events.editor.DocumentCloseEvent +import com.itsaky.androidide.eventbus.events.editor.DocumentOpenEvent +import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisPriority +import com.itsaky.androidide.lsp.kotlin.fixtures.KtLspTest +import com.itsaky.androidide.models.Range +import com.itsaky.androidide.projects.FileManager +import org.jetbrains.kotlin.analysis.api.components.KaDiagnosticCheckerFilter +import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.kotlin.psi.KtElement +import org.junit.After +import org.junit.Test +import java.nio.file.Path + +/** + * Typing inside a function body must not make the enclosing class a redeclaration of itself. + * + * Reproduces GitHub issue #2152: open a file, let diagnostics run, type one space inside an empty + * function body, and the next diagnostics run reports `Redeclaration: class LocalC : Any` across the + * whole class. + */ +internal class InBodyEditRedeclarationTest : KtLspTest() { + override val enableParserEventSystem = true + + private val openedPaths = mutableListOf() + + @After + fun closeDocs() { + openedPaths.forEach { FileManager.onDocumentClose(DocumentCloseEvent(it)) } + openedPaths.clear() + } + + private fun localC(bodyLine: String) = "package p\n\nclass LocalC {\n fun ori() {\n$bodyLine\n }\n}\n" + + private val original = localC(" ") + + private val withSpaceInBody = localC(" ") + + private fun diagnosticsFor(path: Path): List = + env.ktSymbolIndex.withLiveKtFile(path) { live -> + live.analyzing(AnalysisPriority.DIAGNOSTICS, noopCancelChecker()) { ktFile -> + ktFile + .collectDiagnostics(KaDiagnosticCheckerFilter.EXTENDED_AND_COMMON_CHECKERS) + .map { "${it.factoryName}: ${it.defaultMessage}" } + } + }!! + + /** What a completion request does with the buffer: analyse a placeholder copy and resolve the caret's scope. */ + private fun completeAt( + path: Path, + offset: Int, + ) { + val buffer = FileManager.getDocumentContents(path) + val withPlaceholder = buffer.substring(0, offset) + "KT_COMPLETION_PLACEHOLDER" + buffer.substring(offset) + env.ktSymbolIndex.withLiveKtFile(path) { live -> + live.analyzingVariant(path.fileName.toString(), withPlaceholder, AnalysisPriority.INTERACTIVE, noopCancelChecker()) { variant -> + val position = PsiTreeUtil.getParentOfType(variant.findElementAt(offset), KtElement::class.java)!! + variant.scopeContext(position).scopes.size + } + } + } + + @Test + fun `typing a space inside a function body reports no redeclaration`() { + createSourceFile("LocalC.kt", original) + val path = env.sourceRoots.first().resolve("LocalC.kt") + FileManager.onDocumentOpen(DocumentOpenEvent(path, original, 1)) + openedPaths.add(path) + assertThat(diagnosticsFor(path)).isEmpty() + + FileManager.onDocumentContentChange( + DocumentChangeEvent(path, " ", withSpaceInBody, 2, ChangeType.INSERT, 1, Range.NONE), + ) + completeAt(path, withSpaceInBody.indexOf("ori() {\n") + "ori() {\n ".length) + + assertThat(diagnosticsFor(path)).isEmpty() + } +}