Skip to content

Commit 07aeef0

Browse files
committed
chore: add index file tests
1 parent 6d1d0c9 commit 07aeef0

File tree

3 files changed

+92
-39
lines changed

3 files changed

+92
-39
lines changed

app/src/main/kotlin/org/kotlinlsp/index/db/File.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package org.kotlinlsp.index.db
22

33
import com.intellij.openapi.project.Project
44
import kotlinx.serialization.Serializable
5-
import org.jetbrains.kotlin.analysis.api.analyze
65
import org.jetbrains.kotlin.psi.KtFile
76
import org.kotlinlsp.common.read
87
import org.kotlinlsp.index.db.adapters.get

app/src/main/kotlin/org/kotlinlsp/index/worker/IndexKtFile.kt

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import org.jetbrains.kotlin.psi.psiUtil.isAbstract
99
import org.kotlinlsp.common.read
1010
import org.kotlinlsp.common.warn
1111
import org.kotlinlsp.index.db.*
12-
import java.time.Instant
1312

1413
fun indexKtFile(project: Project, ktFile: KtFile, db: Database) {
1514
val newFile = File.fromKtFile(ktFile, project, indexed = true)
@@ -20,51 +19,29 @@ fun indexKtFile(project: Project, ktFile: KtFile, db: Database) {
2019
existingFile?.indexed == true // Already indexed
2120
) return
2221

23-
// Update the file timestamp and package
24-
db.setFile(newFile)
25-
2622
// TODO Remove declarations for this file first
27-
project.read {
23+
val declarations = project.read {
24+
val list = mutableListOf<KtDeclaration>()
2825
ktFile.accept(object : KtTreeVisitorVoid() {
2926
override fun visitDeclaration(dcl: KtDeclaration) {
30-
val decl = analyze(dcl) {
31-
analyzeDeclaration(newFile.path, dcl)
32-
} ?: return
33-
34-
db.putDeclaration(decl) // TODO Put all declarations in bulk to improve performance
35-
27+
list.add(dcl)
3628
super.visitDeclaration(dcl)
3729
}
30+
})
31+
return@read list
32+
}
3833

39-
// TODO Store references
40-
/*override fun visitReferenceExpression(e: KtReferenceExpression) {
41-
super.visitReferenceExpression(e)
42-
if (e !is KtNameReferenceExpression) return
43-
44-
val target = try {
45-
e.mainReference.resolve()
46-
} catch (_: Exception) {
47-
null
48-
}
49-
50-
if (target == null) {
51-
warn("Unresolved reference: ${e.text}")
52-
return
53-
}
54-
55-
val referenceRecord = ReferenceRecord(
56-
id = -1,
57-
symbolId = 1,
58-
startOffset = e.textOffset,
59-
endOffset = e.endOffset
60-
)
34+
declarations.forEach {
35+
val record = analyze(it) {
36+
analyzeDeclaration(newFile.path, it)
37+
} ?: return@forEach
6138

62-
debug("REFERENCE: $referenceRecord")
63-
debug("-> Name: ${e.text}")
64-
debug("-> Target: ${target?.containingFile?.virtualFile?.url}")
65-
}*/
66-
})
39+
// TODO Put all declarations in bulk to improve performance
40+
db.putDeclaration(record)
6741
}
42+
43+
// Update the file timestamp and package
44+
db.setFile(newFile)
6845
}
6946

7047
private fun KaSession.analyzeDeclaration(path: String, dcl: KtDeclaration): Declaration? {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.kotlinlsp.index
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.params.ParameterizedTest
5+
import org.junit.jupiter.params.provider.Arguments
6+
import org.junit.jupiter.params.provider.MethodSource
7+
import org.kotlinlsp.index.db.File
8+
import java.time.Instant
9+
import java.util.stream.Stream
10+
11+
class FileTests {
12+
companion object {
13+
@JvmStatic
14+
fun provideData(): Stream<Arguments> = Stream.of(
15+
// Unmodified file
16+
Arguments.of(
17+
buildFile(modificationStamp = 0, lastModified = Instant.ofEpochMilli(100)),
18+
buildFile(modificationStamp = 0, lastModified = Instant.ofEpochMilli(100)),
19+
true
20+
),
21+
// File modified on disk
22+
Arguments.of(
23+
buildFile(modificationStamp = 0, lastModified = Instant.ofEpochMilli(50)),
24+
buildFile(modificationStamp = 0, lastModified = Instant.ofEpochMilli(100)),
25+
false
26+
),
27+
// File modified in memory
28+
Arguments.of(
29+
buildFile(modificationStamp = 0, lastModified = Instant.ofEpochMilli(50)),
30+
buildFile(modificationStamp = 1, lastModified = Instant.ofEpochMilli(100)),
31+
false
32+
),
33+
Arguments.of(
34+
buildFile(modificationStamp = 1, lastModified = Instant.ofEpochMilli(50)),
35+
buildFile(modificationStamp = 2, lastModified = Instant.ofEpochMilli(100)),
36+
false
37+
),
38+
// Reloading file from disk (after being edited in memory but not saved)
39+
Arguments.of(
40+
buildFile(modificationStamp = 10, lastModified = Instant.ofEpochMilli(100)),
41+
buildFile(modificationStamp = 0, lastModified = Instant.ofEpochMilli(100)),
42+
false
43+
),
44+
// Old in memory file trying to be indexed
45+
Arguments.of(
46+
buildFile(modificationStamp = 10, lastModified = Instant.ofEpochMilli(50)),
47+
buildFile(modificationStamp = 9, lastModified = Instant.ofEpochMilli(50)),
48+
true
49+
),
50+
// New file should always be indexed
51+
Arguments.of(
52+
null,
53+
buildFile(modificationStamp = 9, lastModified = Instant.ofEpochMilli(50)),
54+
false
55+
),
56+
)
57+
}
58+
59+
@ParameterizedTest
60+
@MethodSource("provideData")
61+
fun `test file index skip logic`(existingFile: File?, newFile: File, result: Boolean) {
62+
assertEquals(File.shouldBeSkipped(existingFile = existingFile, newFile = newFile), result)
63+
}
64+
65+
}
66+
67+
private fun buildFile(lastModified: Instant, modificationStamp: Long, indexed: Boolean = false): File {
68+
val path = "/sample/path.kt"
69+
val packageFqName = "com.example"
70+
return File(
71+
path = path,
72+
modificationStamp = modificationStamp,
73+
packageFqName = packageFqName,
74+
lastModified = lastModified,
75+
indexed = indexed
76+
)
77+
}

0 commit comments

Comments
 (0)