-
-
Notifications
You must be signed in to change notification settings - Fork 30
ADFA-4610: Fix Add-import action never appearing on unresolved references #1544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
1fc89fc
ADFA-4610: Fix Add-import action never appearing on unresolved refere…
itsaky-adfa edcbc52
ADFA-4610: Make AddImportAction.prepare() in-memory (no main-thread I/O)
itsaky-adfa f731911
ADFA-4610: Report no-imports-found for optimistic Add-import action
itsaky-adfa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/actions/AddImportActionTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package com.itsaky.androidide.lsp.kotlin.actions | ||
|
|
||
| import com.itsaky.androidide.lsp.kotlin.compiler.index.findSymbolBySimpleName | ||
| import com.itsaky.androidide.lsp.kotlin.fixtures.KtLspTest | ||
| import kotlinx.coroutines.runBlocking | ||
| import org.appdevforall.codeonthego.indexing.jvm.JvmClassInfo | ||
| import org.appdevforall.codeonthego.indexing.jvm.JvmFunctionInfo | ||
| import org.appdevforall.codeonthego.indexing.jvm.JvmSourceLanguage | ||
| import org.appdevforall.codeonthego.indexing.jvm.JvmSymbol | ||
| import org.appdevforall.codeonthego.indexing.jvm.JvmSymbolKind | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
|
|
||
| class AddImportActionTest : KtLspTest() { | ||
|
|
||
| private val mainPath get() = env.sourceRoots.first().resolve("Main.kt") | ||
|
|
||
| private fun classSymbol( | ||
| pkg: String, | ||
| shortName: String, | ||
| ) = symbol(pkg, shortName, JvmSymbolKind.CLASS, JvmClassInfo()) | ||
|
|
||
| private fun funSymbol( | ||
| pkg: String, | ||
| shortName: String, | ||
| ) = symbol(pkg, shortName, JvmSymbolKind.FUNCTION, JvmFunctionInfo()) | ||
|
|
||
| private fun symbol( | ||
| pkg: String, | ||
| shortName: String, | ||
| kind: JvmSymbolKind, | ||
| data: org.appdevforall.codeonthego.indexing.jvm.JvmSymbolInfo, | ||
| ): JvmSymbol { | ||
| val internalName = "${pkg.replace('.', '/')}/$shortName" | ||
| return JvmSymbol( | ||
| key = "$internalName#${kind.name}", | ||
| sourceId = "test", | ||
| name = internalName, | ||
| shortName = shortName, | ||
| packageName = pkg, | ||
| kind = kind, | ||
| language = JvmSourceLanguage.KOTLIN, | ||
| data = data, | ||
| ) | ||
| } | ||
|
|
||
| private fun index(vararg symbols: JvmSymbol) = | ||
| runBlocking { symbols.forEach { env.ktSymbolIndex.sourceIndex.insert(it) } } | ||
|
|
||
| @Test | ||
| fun `resolves a single classifier candidate by simple name`() { | ||
| index(classSymbol("lib", "Foo")) | ||
| createSourceFile("Main.kt", "package p\nimport lib.Bar\nfun f(x: Foo) {}") | ||
|
|
||
| val candidates = AddImportAction().computeImportCandidates(env, mainPath, "Foo") | ||
|
|
||
| assertEquals(setOf("lib.Foo"), candidates.keys) | ||
| val edit = candidates.getValue("lib.Foo").single() | ||
| assertEquals("import lib.Foo", edit.newText.trim()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `offers every matching classifier for a multi-candidate reference`() { | ||
| index(classSymbol("a", "Foo"), classSymbol("b", "Foo")) | ||
| createSourceFile("Main.kt", "package p\nfun f(x: Foo) {}") | ||
|
|
||
| val candidates = AddImportAction().computeImportCandidates(env, mainPath, "Foo") | ||
|
|
||
| assertEquals(setOf("a.Foo", "b.Foo"), candidates.keys) | ||
| candidates.values.forEach { assertEquals(1, it.size) } | ||
| } | ||
|
|
||
| @Test | ||
| fun `filters out non-classifier symbols`() { | ||
| index(classSymbol("a", "Foo"), funSymbol("b", "Foo")) | ||
| createSourceFile("Main.kt", "package p\nfun f(x: Foo) {}") | ||
|
|
||
| val candidates = AddImportAction().computeImportCandidates(env, mainPath, "Foo") | ||
|
|
||
| assertEquals(setOf("a.Foo"), candidates.keys) | ||
| } | ||
|
|
||
| @Test | ||
| fun `returns no candidates for an unknown reference`() { | ||
| createSourceFile("Main.kt", "package p\nfun f() {}") | ||
|
|
||
| assertTrue(AddImportAction().computeImportCandidates(env, mainPath, "Nope").isEmpty()) | ||
| } | ||
|
|
||
| /** | ||
| * Regression guard: `findSymbolBySimpleName` is called with `limit = 0` (unbounded). A plain | ||
| * `take(0)` would return nothing, silently disabling the whole Add-import action. | ||
| */ | ||
| @Test | ||
| fun `findSymbolBySimpleName treats limit 0 as unbounded and a positive limit as a cap`() { | ||
| index(classSymbol("a", "Foo"), classSymbol("b", "Foo"), classSymbol("c", "Foo")) | ||
|
|
||
| val unbounded = env.ktSymbolIndex.findSymbolBySimpleName("Foo", limit = 0).toList() | ||
| assertEquals(setOf("a.Foo", "b.Foo", "c.Foo"), unbounded.map { it.fqName }.toSet()) | ||
|
|
||
| assertEquals(2, env.ktSymbolIndex.findSymbolBySimpleName("Foo", limit = 2).toList().size) | ||
| } | ||
| } |
102 changes: 102 additions & 0 deletions
102
lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/EditExtsTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package com.itsaky.androidide.lsp.kotlin.utils | ||
|
|
||
| import com.itsaky.androidide.lsp.kotlin.compiler.read | ||
| import com.itsaky.androidide.lsp.kotlin.fixtures.KtLspTest | ||
| import com.itsaky.androidide.lsp.models.TextEdit | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
|
|
||
| /** Tests for [insertImport]: the sorted-insertion + dedup logic the Add-import action relies on. */ | ||
| class EditExtsTest : KtLspTest() { | ||
|
|
||
| private val nl = System.lineSeparator() | ||
|
|
||
| private fun insert( | ||
| source: String, | ||
| fqn: String, | ||
| ): List<TextEdit> { | ||
| val ktFile = createSourceFile("Main.kt", source) | ||
| return env.project.read { insertImport(ktFile, fqn) } | ||
| } | ||
|
|
||
| @Test | ||
| fun `inserts before the first import that sorts after it`() { | ||
| val edit = | ||
| insert( | ||
| """ | ||
| package p | ||
| import a.A | ||
| import c.C | ||
| """.trimIndent(), | ||
| "b.B", | ||
| ).single() | ||
| // Pure insertion at the start of `import c.C` (line 2, col 0). | ||
| assertEquals(edit.range.start, edit.range.end) | ||
| assertEquals(2, edit.range.start.line) | ||
| assertEquals(0, edit.range.start.column) | ||
| assertEquals("import b.B$nl", edit.newText) | ||
| } | ||
|
|
||
| @Test | ||
| fun `inserts before the very first import`() { | ||
| val edit = | ||
| insert( | ||
| """ | ||
| package p | ||
| import b.B | ||
| """.trimIndent(), | ||
| "a.A", | ||
| ).single() | ||
| assertEquals(1, edit.range.start.line) | ||
| assertEquals(0, edit.range.start.column) | ||
| assertEquals("import a.A$nl", edit.newText) | ||
| } | ||
|
|
||
| @Test | ||
| fun `appends after the last import when it sorts last`() { | ||
| val edit = | ||
| insert( | ||
| """ | ||
| package p | ||
| import a.A | ||
| import b.B | ||
| """.trimIndent(), | ||
| "z.Z", | ||
| ).single() | ||
| // Pure insertion at the end of `import b.B` (line 2, col 10). | ||
| assertEquals(edit.range.start, edit.range.end) | ||
| assertEquals(2, edit.range.start.line) | ||
| assertEquals(10, edit.range.start.column) | ||
| assertEquals("${nl}import z.Z", edit.newText) | ||
| } | ||
|
|
||
| @Test | ||
| fun `skips an exact duplicate import`() { | ||
| assertTrue( | ||
| insert( | ||
| """ | ||
| package p | ||
| import a.A | ||
| """.trimIndent(), | ||
| "a.A", | ||
| ).isEmpty(), | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `inserts after the package statement when there are no imports`() { | ||
| val edit = | ||
| insert( | ||
| """ | ||
| package p | ||
|
|
||
| class X | ||
| """.trimIndent(), | ||
| "a.A", | ||
| ).single() | ||
| assertEquals(0, edit.range.start.line) | ||
| assertEquals(9, edit.range.start.column) | ||
| assertEquals("${nl}import a.A", edit.newText) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Replace the broad catch-all with narrow exception handling.
The use of
runCatchinghandles allThrowableinstances, intentionally suppressing crashes for any unexpected errors. Based on learnings, prefer narrow exception handling that catches only the specific exception type reported in crashes (such asIllegalArgumentException) instead of a broad catch-all. This preserves fail-fast behavior during development. Uncaught exceptions should be allowed to crash the app so they can be exposed and addressed.🛠️ Proposed fix to apply narrow exception handling and update KDoc
📝 Committable suggestion
🤖 Prompt for AI Agents
Source: Learnings