Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.itsaky.androidide.lsp.java.providers.snippet.JavaSnippetScope
import com.itsaky.androidide.lsp.kotlin.completion.KotlinSnippetScope
import com.itsaky.androidide.lsp.snippets.DefaultSnippet
import com.itsaky.androidide.lsp.snippets.ISnippetScope
import com.itsaky.androidide.lsp.snippets.SnippetLanguage
import com.itsaky.androidide.lsp.snippets.SnippetRegistry
import com.itsaky.androidide.lsp.snippets.UserSnippetLoader
import com.itsaky.androidide.lsp.xml.providers.snippet.XML_SNIPPET_SCOPES
Expand All @@ -15,9 +16,9 @@ object SnippetHandler {
private val log = LoggerFactory.getLogger(SnippetHandler::class.java)

fun loadUserSnippets() {
loadUserSnippetsForLanguage("java", JavaSnippetScope.entries)
loadUserSnippetsForLanguage("kt", KotlinSnippetScope.entries)
loadUserSnippetsForLanguage("xml", XML_SNIPPET_SCOPES)
loadUserSnippetsForLanguage(SnippetLanguage.JAVA.id, JavaSnippetScope.entries)
loadUserSnippetsForLanguage(SnippetLanguage.KOTLIN.id, KotlinSnippetScope.entries)
loadUserSnippetsForLanguage(SnippetLanguage.XML.id, XML_SNIPPET_SCOPES)
}

fun loadPluginSnippets() {
Expand All @@ -40,13 +41,15 @@ object SnippetHandler {
pluginId: String,
contributions: List<SnippetContribution>,
) {
contributions.groupBy { it.language to it.scope }.forEach { (key, group) ->
contributions.groupBy { normalizeLanguage(it.language) to it.scope }.forEach { (key, group) ->
val (language, scope) = key
val snippets = group.map { DefaultSnippet(it.prefix, it.description, it.body.toTypedArray()) }
SnippetRegistry.registerPluginSnippets(pluginId, language, scope, snippets)
}
}

private fun normalizeLanguage(language: String): String = SnippetLanguage.fromId(language)?.id ?: language.lowercase()

fun removePluginSnippets(pluginId: String) {
SnippetRegistry.unregisterPluginSnippets(pluginId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package com.itsaky.androidide.handlers

import com.google.common.truth.Truth.assertThat
import com.itsaky.androidide.lsp.snippets.SnippetRegistry
import com.itsaky.androidide.plugins.PluginContext
import com.itsaky.androidide.plugins.extensions.SnippetContribution
import com.itsaky.androidide.plugins.extensions.SnippetExtension
import com.itsaky.androidide.plugins.manager.snippets.PluginSnippetManager
import com.itsaky.androidide.utils.Environment
import org.junit.After
import org.junit.Before
Expand All @@ -25,10 +29,85 @@ class SnippetHandlerTest {

@After
fun tearDown() {
PluginSnippetManager.getInstance().cleanupPlugin(PLUGIN_ID)
SnippetRegistry.clear()
Environment.SNIPPETS_DIR = originalSnippetsDir
}

@Test
fun `loadPluginSnippets maps the kotlin language id onto the kt registry key`() {
registerPlugin(
SnippetContribution(
language = "kotlin",
scope = "local",
prefix = "ktplugin",
description = "Contributed by a plugin",
body = listOf("println(\"plugin\")"),
),
)

SnippetHandler.loadPluginSnippets()

assertThat(SnippetRegistry.getSnippets("kt", "local").map { it.prefix })
.containsExactly("ktplugin")
assertThat(SnippetRegistry.getSnippets("kotlin", "local")).isEmpty()
}

@Test
fun `loadPluginSnippets leaves other language ids untouched`() {
registerPlugin(
SnippetContribution(
language = "java",
scope = "local",
prefix = "javaplugin",
description = "Contributed by a plugin",
body = listOf("System.out.println();"),
),
)

SnippetHandler.loadPluginSnippets()

assertThat(SnippetRegistry.getSnippets("java", "local").map { it.prefix })
.containsExactly("javaplugin")
assertThat(SnippetRegistry.getSnippets("kt", "local")).isEmpty()
}

@Test
fun `loadPluginSnippets lower-cases an unrecognised language id and keeps it`() {
registerPlugin(
SnippetContribution(
language = "Groovy",
scope = "local",
prefix = "groovyplugin",
description = "Contributed by a plugin",
body = listOf("println 'groovy'"),
),
)

SnippetHandler.loadPluginSnippets()

assertThat(SnippetRegistry.getSnippets("groovy", "local").map { it.prefix })
.containsExactly("groovyplugin")
assertThat(SnippetRegistry.getSnippets("kt", "local")).isEmpty()
}

private fun registerPlugin(vararg contributions: SnippetContribution) {
PluginSnippetManager.getInstance().registerPlugin(
PLUGIN_ID,
object : SnippetExtension {
override fun getSnippetContributions() = contributions.toList()

override fun initialize(context: PluginContext) = true

override fun activate() = true

override fun deactivate() = true

override fun dispose() = Unit
},
)
}

@Test
fun `loadUserSnippets loads Kotlin local snippet with content intact`() {
writeKotlinSnippet(
Expand Down Expand Up @@ -85,4 +164,8 @@ class SnippetHandlerTest {
"""{"$prefix":{"desc":"$description","body":[$bodyJson]}}""",
)
}

private companion object {
const val PLUGIN_ID = "test.snippets.plugin"
}
}
7 changes: 7 additions & 0 deletions docs/PLUGIN_API_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ milestone. **[verified]** = read from the checked-in ABI dump. **[reconstructed]
= diffed from `plugin-api/src` history (predates the dump; symbol-accurate).

### 26.36 — unreleased
- **added — `SnippetContribution.language` accepts `kotlin`** _(ADFA-6189)_
The host keys Kotlin snippets under `kt`, so a contribution declaring `kotlin` registered
under a language nothing looks up and never appeared in a `.kt` file. The id is now
lower-cased and `kotlin` is aliased to `kt`; `java`, `kt` and `xml` are unchanged.
Accepted ids are `java`, `kt` (or `kotlin`) and `xml` — anything else registers but is
never queried.

- **added — Build provenance in every `.cgp`** _(ADFA-5394)_ **[verified]**
A plugin artifact now records the commit it was built from, so a crash report or a
support question can be traced back to source. Nothing in the pipeline carried a git
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.itsaky.androidide.lsp.snippets

enum class SnippetLanguage(
val id: String,
private val aliases: Set<String> = emptySet(),
) {
JAVA("java"),
KOTLIN("kt", setOf("kotlin")),
XML("xml"),
;

companion object {
private val byId =
entries
.flatMap { language -> (language.aliases + language.id).map { it to language } }
.toMap()

fun fromId(value: String): SnippetLanguage? = byId[value.lowercase()]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.itsaky.androidide.lsp.java.providers.snippet

import com.itsaky.androidide.lsp.snippets.ISnippet
import com.itsaky.androidide.lsp.snippets.SnippetLanguage
import com.itsaky.androidide.lsp.snippets.SnippetRegistry

/**
Expand All @@ -26,13 +27,13 @@ import com.itsaky.androidide.lsp.snippets.SnippetRegistry
* @author Akash Yadav
*/
object JavaSnippetRepository {

val snippets: Map<JavaSnippetScope, List<ISnippet>>
get() = JavaSnippetScope.entries.associateWith { scope ->
SnippetRegistry.getSnippets("java", scope.filename)
}
get() =
JavaSnippetScope.entries.associateWith { scope ->
SnippetRegistry.getSnippets(SnippetLanguage.JAVA.id, scope.filename)
}

fun init() {
SnippetRegistry.initBuiltIn("java", JavaSnippetScope.entries)
SnippetRegistry.initBuiltIn(SnippetLanguage.JAVA.id, JavaSnippetScope.entries)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.itsaky.androidide.lsp.kotlin.compiler.Compiler
import com.itsaky.androidide.lsp.kotlin.compiler.KotlinProjectModel
import com.itsaky.androidide.lsp.kotlin.compiler.index.KT_SOURCE_FILE_INDEX_KEY
import com.itsaky.androidide.lsp.kotlin.compiler.index.KT_SOURCE_FILE_META_INDEX_KEY
import com.itsaky.androidide.lsp.kotlin.completion.KotlinSnippetRepository
import com.itsaky.androidide.lsp.kotlin.completion.codeComplete
import com.itsaky.androidide.lsp.kotlin.diagnostic.collectDiagnosticsFor
import com.itsaky.androidide.lsp.kotlin.navigation.findDefinitionAt
Expand Down Expand Up @@ -208,6 +209,8 @@ class KotlinLanguageServer : ILanguageServer {
}
}

KotlinSnippetRepository.init()

initialized = true
logger.info("Kotlin project initialized")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.itsaky.androidide.lsp.kotlin.completion

import com.itsaky.androidide.lsp.snippets.ISnippet
import com.itsaky.androidide.lsp.snippets.SnippetParser
import com.itsaky.androidide.lsp.snippets.SnippetLanguage
import com.itsaky.androidide.lsp.snippets.SnippetRegistry

object KotlinSnippetRepository {
val snippets: Map<KotlinSnippetScope, List<ISnippet>>
get() = KotlinSnippetScope.entries.associateWith { scope ->
SnippetRegistry.getSnippets("kt", scope.filename)
}
get() =
KotlinSnippetScope.entries.associateWith { scope ->
SnippetRegistry.getSnippets(SnippetLanguage.KOTLIN.id, scope.filename)
}

fun init() {
SnippetRegistry.initBuiltIn("kt", KotlinSnippetScope.entries)
SnippetRegistry.initBuiltIn(SnippetLanguage.KOTLIN.id, KotlinSnippetScope.entries)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
package com.itsaky.androidide.lsp.xml.providers.snippet

import com.itsaky.androidide.lsp.snippets.ISnippet
import com.itsaky.androidide.lsp.snippets.SnippetLanguage
import com.itsaky.androidide.lsp.snippets.SnippetRegistry

object XmlSnippetRepository {

val snippets: Map<IXmlSnippetScope, List<ISnippet>>
get() = XML_SNIPPET_SCOPES.associateWith { scope ->
SnippetRegistry.getSnippets("xml", scope.filename)
}
get() =
XML_SNIPPET_SCOPES.associateWith { scope ->
SnippetRegistry.getSnippets(SnippetLanguage.XML.id, scope.filename)
}

fun init() {
SnippetRegistry.initBuiltIn("xml", XML_SNIPPET_SCOPES)
SnippetRegistry.initBuiltIn(SnippetLanguage.XML.id, XML_SNIPPET_SCOPES)
}
}
Loading