From 8d2dcd4f506885d37c3744567db9a93591521a32 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 22 Nov 2026 21:53:11 -0800 Subject: [PATCH] ADFA-7267 AI-Code-Suggestions: cancel the pending suggestion when the word is gone Typing "fun" and then a space showed a suggestion for "fun" after the space. An edit that left no word of 2+ letters before the cursor returned before cancelling the request started for the previous word, so that request ran 0.8 s later and showed its suggestion. Every edit now cancels the pending request first. Adds the plugin's first unit tests: a word still gets a suggestion, and neither a space nor a one-letter word after "fun" sends "fun" to the model. Both of those fail without the fix. Also raises plugin.max_ide_version from 26.30 to 26.99, like AI-Core and the other first-party plugins: the plugin review failed rubric 6.1 because the declared range excluded the current release (26.47). Version 1.1.0 -> 1.1.1 (versionCode 2 -> 3). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- plugins/AI-Code-Suggestions/build.gradle.kts | 12 ++- .../src/main/AndroidManifest.xml | 2 +- .../CodeSuggestionsPlugin.kt | 7 +- .../CodeSuggestionsPluginTest.kt | 96 +++++++++++++++++++ 4 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 plugins/AI-Code-Suggestions/src/test/kotlin/com/itsaky/androidide/plugins/aicodesuggestions/CodeSuggestionsPluginTest.kt diff --git a/plugins/AI-Code-Suggestions/build.gradle.kts b/plugins/AI-Code-Suggestions/build.gradle.kts index 3e83b739d..c6b25f30a 100644 --- a/plugins/AI-Code-Suggestions/build.gradle.kts +++ b/plugins/AI-Code-Suggestions/build.gradle.kts @@ -15,8 +15,8 @@ android { applicationId = "com.itsaky.androidide.plugins.aicodesuggestions" minSdk = 33 targetSdk = 36 - versionCode = 2 - versionName = "1.1.0" + versionCode = 3 + versionName = "1.1.1" } buildTypes { @@ -25,6 +25,10 @@ android { } } + testOptions { + unitTests.isReturnDefaultValues = true + } + compileOptions { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 @@ -48,6 +52,10 @@ dependencies { implementation("com.google.android.material:material:1.10.0") implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.7") implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.7") + + testImplementation(files("../../libs/plugin-api.jar")) + testImplementation("junit:junit:4.13.2") + testImplementation("io.mockk:mockk:1.13.8") } tasks.matching { diff --git a/plugins/AI-Code-Suggestions/src/main/AndroidManifest.xml b/plugins/AI-Code-Suggestions/src/main/AndroidManifest.xml index 528491ebd..afc85fc1f 100644 --- a/plugins/AI-Code-Suggestions/src/main/AndroidManifest.xml +++ b/plugins/AI-Code-Suggestions/src/main/AndroidManifest.xml @@ -43,7 +43,7 @@ + android:value="26.99" /> () + private val shown = LinkedBlockingQueue() + private val plugin = CodeSuggestionsPlugin() + private lateinit var listener: EditorContentChangeListener + + @Before + fun setUp() { + val captured = slot() + val editor = mockk(relaxed = true) + every { editor.addContentChangeListener(capture(captured)) } just Runs + every { editor.showInlineSuggestion(any()) } answers { shown.add(firstArg()) } + + val llm = mockk() + every { llm.generateCompletion(any(), any()) } answers { + prompts.add(firstArg()) + CompletableFuture.completedFuture(LlmInferenceService.LlmResponse.success(COMPLETION, 1, 1L)) + } + + val services = mockk() + every { services.get(IdeEditorService::class.java) } returns editor + every { services.get(LlmInferenceService::class.java) } returns llm + val context = mockk() + every { context.services } returns services + + assertTrue(plugin.initialize(context)) + assertTrue(plugin.activate()) + listener = captured.captured + } + + @After + fun tearDown() { + plugin.deactivate() + plugin.dispose() + } + + @Test + fun aWordOfTwoLettersOrMoreGetsASuggestion() { + type("fun") + + assertEquals(COMPLETION, shown.poll(5, TimeUnit.SECONDS)) + } + + @Test + fun aSpaceAfterAWordCancelsThatWordsPendingSuggestion() { + type("fun") + type("fun ") + + assertNull("the LLM was asked to complete the word left behind", prompts.poll(2, TimeUnit.SECONDS)) + assertTrue("a suggestion appeared after the space: $shown", shown.isEmpty()) + } + + @Test + fun aOneLetterWordCancelsThePreviousWordsPendingSuggestion() { + type("fun") + type("fun x") + + assertNull("the LLM was asked to complete the word left behind", prompts.poll(2, TimeUnit.SECONDS)) + assertTrue("a suggestion appeared after the one-letter word: $shown", shown.isEmpty()) + } + + /** Reports [text] as a one-line Kotlin file with the cursor at its end. */ + private fun type(text: String) = listener.onContentChanged(text, 0, text.length, "kotlin") + + private companion object { + const val COMPLETION = "ction main() {" + } +}