diff --git a/shared/src/main/java/com/itsaky/androidide/utils/SourceClassTrie.kt b/shared/src/main/java/com/itsaky/androidide/utils/SourceClassTrie.kt index e7569b197e..e4760e7af5 100644 --- a/shared/src/main/java/com/itsaky/androidide/utils/SourceClassTrie.kt +++ b/shared/src/main/java/com/itsaky/androidide/utils/SourceClassTrie.kt @@ -28,153 +28,192 @@ import kotlin.io.path.nameWithoutExtension * * @author Akash Yadav */ -open class SourceClassTrie(root: SourcePackageNode = SourcePackageNode()) : ClassTrie(root) { - - companion object { - @JvmStatic - private val pkgNameMethod by lazy { - Class.forName("com.itsaky.androidide.projects.util.StringSearch") - .getDeclaredMethod("packageName", Path::class.java) - } - - @JvmStatic - private fun packageName(file: Path): String { - return try { - pkgNameMethod.invoke(null, file) as String - } catch (error: Throwable) { - throw RuntimeException(error) - } - } - } - - override fun append(name: String): Node { - throw UnsupportedOperationException("Method not supported!") - } - - open fun append(path: Path, sourceDir: Path): SourceNode { - val modified = Files.getLastModifiedTime(path).toInstant() - val packageName = packageName(path) - val segments = segments(packageName) - var node: SourcePackageNode = root as SourcePackageNode - var currentPath = sourceDir - for ((index, segment) in segments.withIndex()) { - if (segment.isEmpty() || !segment[0].isJavaIdentifierStart()) { - continue - } - - currentPath = currentPath.resolve(segment) - node = createNode(currentPath, node, segment, segments, index) - } - node.isClass = false - - val name = path.nameWithoutExtension - val klass = - SourceNode( - file = path, - packageName = packageName, - modified = modified, - parent = node, - name = name - ) - klass.isClass = true - node.children[name] = klass - return klass - } - - open fun createNode( - dir: Path, - node: SourcePackageNode, - segment: String, - segments: List, - index: Int - ) = node.createChild(dir, segment, segments.subList(0, index + 1).joinToString(separator = ".")) - - override fun createNode(node: Node, segment: String, segments: List, index: Int): Node { - throw UnsupportedOperationException("Method not supported") - } - - open fun allSources(): List { - return allClassNodes().filterIsInstance(SourceNode::class.java) - } - - fun findSource(file: Path): SourceNode? { - return findSourceInNode(file, root) - } - - fun findDirNode(dir: Path, node: Node): SourcePackageNode? { - var dirNode: SourcePackageNode? = null - for (child in node.children.values) { - if ( - child !is SourceNode && - child is SourcePackageNode && - DocumentUtils.isSameFile(child.dir, dir) - ) { - dirNode = child - } - - dirNode = dirNode ?: findDirNode(dir, child) - - if (dirNode != null) { - break - } - } - return dirNode - } - - fun getSourceFilesInDir(dir: Path): List { - val dirNode = findDirNode(dir, root) ?: return emptyList() - return dirNode.children.values.filterIsInstance(SourceNode::class.java) - } - - private fun findSourceInNode(file: Path, node: Node): SourceNode? { - - if (node is SourceNode && DocumentUtils.isSameFile(file, node.file)) { - return node - } - - for (child in node.children.values) { - if (child is SourceNode && DocumentUtils.isSameFile(file, child.file)) { - return child - } - - val childResult = findSourceInNode(file, child) - if (childResult != null) { - return childResult - } - } - return null - } - - open class SourceNode( - val file: Path, - val packageName: String, - val modified: Instant, - parent: SourcePackageNode? = null, - name: String - ) : Node(name = name, qualifiedName = "$packageName.$name", parent = parent) { - - override fun createChild(name: String, qualifiedName: String): Node { - throw UnsupportedOperationException("Method not supported!") - } - } - - open class SourcePackageNode( - val dir: Path, - parent: SourcePackageNode? = null, - name: String, - qualifiedName: String - ) : Node(name, qualifiedName, parent) { - - internal constructor() : this(Paths.get(""), null, "", "") - - override fun createChild(name: String, qualifiedName: String): Node { - throw UnsupportedOperationException("Method not suported!") - } - - open fun createChild(dir: Path, name: String, qualifiedName: String): SourcePackageNode { - return this.children.computeIfAbsent(name) { - SourcePackageNode(dir, this, name, qualifiedName) - } as SourcePackageNode - } - } +open class SourceClassTrie( + root: SourcePackageNode = SourcePackageNode(), +) : ClassTrie(root) { + companion object { + @JvmStatic + private val pkgNameMethod by lazy { + Class + .forName("com.itsaky.androidide.projects.util.StringSearch") + .getDeclaredMethod("packageName", Path::class.java) + } + + @JvmStatic + private fun packageName(file: Path): String = + try { + pkgNameMethod.invoke(null, file) as String + } catch (error: Throwable) { + throw RuntimeException(error) + } + } + + override fun append(name: String): Node = throw UnsupportedOperationException("Method not supported!") + + open fun append( + path: Path, + sourceDir: Path, + ): SourceNode { + val modified = Files.getLastModifiedTime(path).toInstant() + val packageName = packageName(path) + val segments = segments(packageName) + var node: SourcePackageNode = root as SourcePackageNode + var currentPath = sourceDir + for ((index, segment) in segments.withIndex()) { + if (segment.isEmpty() || !segment[0].isJavaIdentifierStart()) { + continue + } + + currentPath = currentPath.resolve(segment) + node = createNode(currentPath, node, segment, segments, index) + } + node.isClass = false + + val name = path.nameWithoutExtension + val klass = + SourceNode( + file = path, + packageName = packageName, + modified = modified, + parent = node, + name = name, + ) + klass.isClass = true + node.addSource(klass) + return klass + } + + open fun createNode( + dir: Path, + node: SourcePackageNode, + segment: String, + segments: List, + index: Int, + ) = node.createChild(dir, segment, segments.subList(0, index + 1).joinToString(separator = ".")) + + override fun createNode( + node: Node, + segment: String, + segments: List, + index: Int, + ): Node = throw UnsupportedOperationException("Method not supported") + + open fun allSources(): List = allClassNodes().filterIsInstance(SourceNode::class.java) + + fun findSource(file: Path): SourceNode? = findSourceInNode(file, root) + + fun findDirNode( + dir: Path, + node: Node, + ): SourcePackageNode? { + var dirNode: SourcePackageNode? = null + for (child in node.children.values) { + if ( + child !is SourceNode && + child is SourcePackageNode && + DocumentUtils.isSameFile(child.dir, dir) + ) { + dirNode = child + } + + dirNode = dirNode ?: findDirNode(dir, child) + + if (dirNode != null) { + break + } + } + return dirNode + } + + fun getSourceFilesInDir(dir: Path): List { + val dirNode = findDirNode(dir, root) ?: return emptyList() + return dirNode.children.values.filterIsInstance(SourceNode::class.java) + } + + private fun findSourceInNode( + file: Path, + node: Node, + ): SourceNode? { + if (node is SourceNode && DocumentUtils.isSameFile(file, node.file)) { + return node + } + + for (child in node.children.values) { + if (child is SourceNode && DocumentUtils.isSameFile(file, child.file)) { + return child + } + + val childResult = findSourceInNode(file, child) + if (childResult != null) { + return childResult + } + } + return null + } + + open class SourceNode( + val file: Path, + val packageName: String, + val modified: Instant, + parent: SourcePackageNode? = null, + name: String, + ) : Node(name = name, qualifiedName = "$packageName.$name", parent = parent) { + override fun createChild( + name: String, + qualifiedName: String, + ): Node = throw UnsupportedOperationException("Method not supported!") + } + + open class SourcePackageNode( + val dir: Path, + parent: SourcePackageNode? = null, + name: String, + qualifiedName: String, + ) : Node(name, qualifiedName, parent) { + internal constructor() : this(Paths.get(""), null, "", "") + + override fun createChild( + name: String, + qualifiedName: String, + ): Node = throw UnsupportedOperationException("Method not suported!") + + open fun createChild( + dir: Path, + name: String, + qualifiedName: String, + ): SourcePackageNode = + synchronized(this) { + when (val existing = children[name]) { + is SourcePackageNode -> { + existing + } + + is SourceNode -> { + children[existing.file.fileName.toString()] = existing + SourcePackageNode(dir, this, name, qualifiedName).also { children[name] = it } + } + + null -> { + SourcePackageNode(dir, this, name, qualifiedName).also { children[name] = it } + } + + else -> { + error("Unexpected ${existing.javaClass.name} at '$qualifiedName'") + } + } + } + + fun addSource(source: SourceNode) { + synchronized(this) { + val key = if (children[source.name] is SourcePackageNode) source.file.fileName.toString() else source.name + children[key] = source + } + } + + override fun removeChild(child: Node) { + synchronized(this) { + children.values.remove(child) + } + } + } } diff --git a/subprojects/projects/src/test/java/com/itsaky/androidide/utils/SourceClassTrieNameClashTest.kt b/subprojects/projects/src/test/java/com/itsaky/androidide/utils/SourceClassTrieNameClashTest.kt new file mode 100644 index 0000000000..809d47ee80 --- /dev/null +++ b/subprojects/projects/src/test/java/com/itsaky/androidide/utils/SourceClassTrieNameClashTest.kt @@ -0,0 +1,93 @@ +package com.itsaky.androidide.utils + +import com.google.common.truth.Truth.assertThat +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.nio.file.Path + +class SourceClassTrieNameClashTest { + @get:Rule + val temp = TemporaryFolder() + + private lateinit var sourceDir: Path + private lateinit var calculator: Path + private lateinit var helper: Path + + @Before + fun setUp() { + sourceDir = temp.newFolder("java").toPath() + calculator = source("com/example/Calculator.java", "package com.example;\n\npublic class Calculator {}\n") + helper = source("com/example/Calculator/Helper.java", "package com.example.Calculator;\n\npublic class Helper {}\n") + } + + @Test + fun fileIndexedBeforeASameNamedPackageDoesNotCrash() { + val trie = SourceClassTrie() + + trie.append(calculator, sourceDir) + trie.append(helper, sourceDir) + + assertThat(trie.allSources().map { it.file }).containsExactly(calculator, helper) + } + + @Test + fun fileIndexedAfterASameNamedPackageKeepsThePackagesSources() { + val trie = SourceClassTrie() + + trie.append(helper, sourceDir) + trie.append(calculator, sourceDir) + + assertThat(trie.allSources().map { it.file }).containsExactly(calculator, helper) + } + + @Test + fun thePackageNameStillResolvesToThePackage() { + val trie = indexedTrie() + + val node = trie.findNode("com.example.Calculator") + + assertThat(node).isInstanceOf(SourceClassTrie.SourcePackageNode::class.java) + assertThat( + node!! + .children.values + .filterIsInstance() + .map { it.file }, + ).containsExactly(helper) + } + + @Test + fun theClashingFileIsStillListedInItsDirectory() { + val trie = indexedTrie() + + assertThat(trie.getSourceFilesInDir(calculator.parent).map { it.file }).containsExactly(calculator) + } + + @Test + fun removingTheClashingFileKeepsThePackage() { + val trie = indexedTrie() + + val source = trie.findSource(calculator)!! + source.parent!!.removeChild(source) + + assertThat(trie.findSource(calculator)).isNull() + assertThat(trie.findSource(helper)).isNotNull() + } + + private fun indexedTrie() = + SourceClassTrie().apply { + append(calculator, sourceDir) + append(helper, sourceDir) + } + + private fun source( + relativePath: String, + content: String, + ): Path { + val file = sourceDir.resolve(relativePath) + file.parent.toFile().mkdirs() + file.toFile().writeText(content) + return file + } +}