Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions plugins/AI-Code-Suggestions/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,6 +25,10 @@ android {
}
}

testOptions {
unitTests.isReturnDefaultValues = true
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion plugins/AI-Code-Suggestions/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

<meta-data
android:name="plugin.max_ide_version"
android:value="26.30" />
android:value="26.99" />

<meta-data
android:name="plugin.main_class"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,17 @@ class CodeSuggestionsPlugin : IPlugin, DocumentationExtension {

// Extract the word/prefix being typed (most recent word)
val prefix = extractPrefix(fileContent, cursorLine, cursorColumn)

// Every edit supersedes the pending request, including one that leaves no word to
// complete: otherwise typing "fun" and a space shows a suggestion for "fun" after the space.
debounceJob?.cancel()
if (prefix.isEmpty() || prefix.length < 2) {
// Too short to suggest
editorService!!.dismissInlineSuggestion()
return
}

// Debounce: cancel previous job and start new one
debounceJob?.cancel()
// Debounce: start a new request once typing pauses
debounceJob = scope.launch {
delay(DEBOUNCE_MS)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.itsaky.androidide.plugins.aicodesuggestions

import com.itsaky.androidide.plugins.PluginContext
import com.itsaky.androidide.plugins.ServiceRegistry
import com.itsaky.androidide.plugins.services.EditorContentChangeListener
import com.itsaky.androidide.plugins.services.IdeEditorService
import com.itsaky.androidide.plugins.services.LlmInferenceService
import io.mockk.Runs
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.slot
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import java.util.concurrent.CompletableFuture
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.TimeUnit

/**
* Drives the plugin through the editor's content-change listener, as the IDE does, with a fake
* editor and a fake LLM. The plugin waits 800 ms before asking the LLM, so a test that expects
* no request waits 2 s for one.
*/
class CodeSuggestionsPluginTest {

private val prompts = LinkedBlockingQueue<String>()
private val shown = LinkedBlockingQueue<String>()
private val plugin = CodeSuggestionsPlugin()
private lateinit var listener: EditorContentChangeListener

@Before
fun setUp() {
val captured = slot<EditorContentChangeListener>()
val editor = mockk<IdeEditorService>(relaxed = true)
every { editor.addContentChangeListener(capture(captured)) } just Runs
every { editor.showInlineSuggestion(any()) } answers { shown.add(firstArg()) }

val llm = mockk<LlmInferenceService>()
every { llm.generateCompletion(any(), any()) } answers {
prompts.add(firstArg())
CompletableFuture.completedFuture(LlmInferenceService.LlmResponse.success(COMPLETION, 1, 1L))
}

val services = mockk<ServiceRegistry>()
every { services.get(IdeEditorService::class.java) } returns editor
every { services.get(LlmInferenceService::class.java) } returns llm
val context = mockk<PluginContext>()
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() {"
}
}
Loading