-
Notifications
You must be signed in to change notification settings - Fork 18
ADFA-3546: Add code snippet support for plugin api #1162
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
Open
Daniel-ADFA
wants to merge
3
commits into
stage
Choose a base branch
from
ADFA-3546
base: stage
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
67 changes: 67 additions & 0 deletions
67
app/src/main/java/com/itsaky/androidide/handlers/SnippetHandler.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,67 @@ | ||
| package com.itsaky.androidide.handlers | ||
|
|
||
| import com.itsaky.androidide.lsp.java.providers.snippet.JavaSnippetScope | ||
| import com.itsaky.androidide.lsp.snippets.DefaultSnippet | ||
| import com.itsaky.androidide.lsp.snippets.ISnippetScope | ||
| import com.itsaky.androidide.lsp.snippets.SnippetRegistry | ||
| import com.itsaky.androidide.plugins.extensions.SnippetContribution | ||
| import com.itsaky.androidide.lsp.snippets.UserSnippetLoader | ||
| import com.itsaky.androidide.lsp.xml.providers.snippet.XML_SNIPPET_SCOPES | ||
| import com.itsaky.androidide.plugins.manager.snippets.PluginSnippetManager | ||
| import org.slf4j.LoggerFactory | ||
|
|
||
| object SnippetHandler { | ||
|
|
||
| private val log = LoggerFactory.getLogger(SnippetHandler::class.java) | ||
|
|
||
| fun loadUserSnippets() { | ||
| loadUserSnippetsForLanguage("java", JavaSnippetScope.entries.toTypedArray()) | ||
| loadUserSnippetsForLanguage("xml", XML_SNIPPET_SCOPES) | ||
| } | ||
|
|
||
|
|
||
| fun loadPluginSnippets() { | ||
| val allSnippets = PluginSnippetManager.getInstance().getAllSnippets() | ||
| allSnippets.forEach { (pluginId, contributions) -> | ||
| registerContributions(pluginId, contributions) | ||
| } | ||
| if (allSnippets.isNotEmpty()) { | ||
| log.info("Loaded plugin snippets from {} plugins", allSnippets.size) | ||
| } | ||
| } | ||
|
|
||
| fun refreshPluginSnippets(pluginId: String) { | ||
| SnippetRegistry.unregisterPluginSnippets(pluginId) | ||
| val contributions = PluginSnippetManager.getInstance().refreshPlugin(pluginId) | ||
| registerContributions(pluginId, contributions) | ||
| } | ||
|
|
||
| private fun registerContributions(pluginId: String, contributions: List<SnippetContribution>) { | ||
| contributions.groupBy { 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) | ||
| } | ||
| } | ||
|
|
||
| fun removePluginSnippets(pluginId: String) { | ||
| SnippetRegistry.unregisterPluginSnippets(pluginId) | ||
| } | ||
|
|
||
| private fun <S : ISnippetScope> loadUserSnippetsForLanguage( | ||
| language: String, | ||
| scopes: Array<S>, | ||
| ) { | ||
| SnippetRegistry.clearUserSnippets(language) | ||
| val userSnippets = UserSnippetLoader.loadUserSnippets(language, scopes) | ||
| userSnippets.forEach { (scope, snippets) -> | ||
| if (snippets.isNotEmpty()) { | ||
| SnippetRegistry.registerUserSnippets(language, scope, snippets) | ||
| } | ||
| } | ||
| val total = userSnippets.values.sumOf { it.size } | ||
| if (total > 0) { | ||
| log.info("Loaded {} user snippets for {}", total, language) | ||
| } | ||
| } | ||
| } | ||
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
86 changes: 86 additions & 0 deletions
86
lsp/api/src/main/java/com/itsaky/androidide/lsp/snippets/SnippetRegistry.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,86 @@ | ||
| package com.itsaky.androidide.lsp.snippets | ||
|
|
||
| import java.util.concurrent.locks.ReentrantReadWriteLock | ||
| import kotlin.concurrent.read | ||
| import kotlin.concurrent.write | ||
|
|
||
| object SnippetRegistry { | ||
|
|
||
| private val lock = ReentrantReadWriteLock() | ||
|
|
||
| private val builtIn = mutableMapOf<String, MutableMap<String, MutableList<ISnippet>>>() | ||
| private val user = mutableMapOf<String, MutableMap<String, MutableList<ISnippet>>>() | ||
| private val plugin = mutableMapOf<String, MutableMap<String, MutableMap<String, MutableList<ISnippet>>>>() | ||
|
|
||
| fun registerBuiltIn(language: String, scope: String, snippets: List<ISnippet>) { | ||
| lock.write { | ||
| builtIn.getOrPut(language) { mutableMapOf() }[scope] = snippets.toMutableList() | ||
| } | ||
| } | ||
|
|
||
| fun registerUserSnippets(language: String, scope: String, snippets: List<ISnippet>) { | ||
| lock.write { | ||
| user.getOrPut(language) { mutableMapOf() }[scope] = snippets.toMutableList() | ||
| } | ||
| } | ||
|
|
||
| fun registerPluginSnippets( | ||
| pluginId: String, | ||
| language: String, | ||
| scope: String, | ||
| snippets: List<ISnippet>, | ||
| ) { | ||
| lock.write { | ||
| plugin.getOrPut(pluginId) { mutableMapOf() } | ||
| .getOrPut(language) { mutableMapOf() }[scope] = snippets.toMutableList() | ||
| } | ||
| } | ||
|
|
||
| fun unregisterPluginSnippets(pluginId: String) { | ||
| lock.write { | ||
| plugin.remove(pluginId) | ||
| } | ||
| } | ||
|
|
||
| fun clearUserSnippets(language: String) { | ||
| lock.write { | ||
| user.remove(language) | ||
| } | ||
| } | ||
|
|
||
| fun getSnippets(language: String, scope: String): List<ISnippet> = lock.read { | ||
| val builtInSnippets = builtIn[language]?.get(scope).orEmpty() | ||
| val userSnippets = user[language]?.get(scope).orEmpty() | ||
|
|
||
| val userPrefixes = userSnippets.map { it.prefix }.toSet() | ||
| val merged = builtInSnippets.filter { it.prefix !in userPrefixes }.toMutableList() | ||
| merged.addAll(userSnippets) | ||
|
|
||
| plugin.values.forEach { langMap -> | ||
| langMap[language]?.get(scope)?.let { merged.addAll(it) } | ||
| } | ||
|
|
||
| merged | ||
| } | ||
|
|
||
| fun getSnippets(language: String, scopes: List<String>): List<ISnippet> { | ||
| return scopes.flatMap { getSnippets(language, it) } | ||
| } | ||
|
|
||
| fun <S : ISnippetScope> initBuiltIn(language: String, scopes: Array<S>) { | ||
| val parsed = SnippetParser.parse(language, scopes) | ||
| lock.write { | ||
| parsed.forEach { (scope, snippets) -> | ||
| builtIn.getOrPut(language) { mutableMapOf() }[scope.filename] = snippets as MutableList<ISnippet> | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| fun clear() { | ||
| lock.write { | ||
| builtIn.clear() | ||
| user.clear() | ||
| plugin.clear() | ||
| } | ||
| } | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
lsp/api/src/main/java/com/itsaky/androidide/lsp/snippets/UserSnippetLoader.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,42 @@ | ||
| package com.itsaky.androidide.lsp.snippets | ||
|
|
||
| import com.google.gson.JsonParseException | ||
| import com.itsaky.androidide.utils.Environment | ||
| import org.slf4j.LoggerFactory | ||
| import java.io.File | ||
| import java.io.IOException | ||
|
|
||
| object UserSnippetLoader { | ||
|
|
||
| private val log = LoggerFactory.getLogger(UserSnippetLoader::class.java) | ||
|
|
||
| fun <S : ISnippetScope> loadUserSnippets( | ||
| language: String, | ||
| scopes: Array<S>, | ||
| ): Map<String, List<ISnippet>> { | ||
| val langDir = getUserSnippetsDir(language) | ||
| if (!langDir.isDirectory) return emptyMap() | ||
|
|
||
| return scopes.associate { scope -> | ||
| val file = File(langDir, "snippets.${scope.filename}.json") | ||
| val snippets = if (file.isFile) { | ||
| try { | ||
| SnippetParser.parseFromReader(file.reader()) | ||
| } catch (e: IOException) { | ||
| log.error("Failed to read user snippets from {}", file.absolutePath, e) | ||
| emptyList() | ||
| } catch (e: JsonParseException) { | ||
| log.error("Failed to parse user snippets from {}", file.absolutePath, e) | ||
| emptyList() | ||
| } | ||
| } else { | ||
| emptyList() | ||
| } | ||
| scope.filename to snippets | ||
| } | ||
| } | ||
|
|
||
| fun getUserSnippetsDir(language: String): File { | ||
| return File(Environment.SNIPPETS_DIR, language) | ||
| } | ||
| } |
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
15 changes: 15 additions & 0 deletions
15
plugin-api/src/main/kotlin/com/itsaky/androidide/plugins/extensions/SnippetExtension.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,15 @@ | ||
| package com.itsaky.androidide.plugins.extensions | ||
|
|
||
| import com.itsaky.androidide.plugins.IPlugin | ||
|
|
||
| interface SnippetExtension : IPlugin { | ||
| fun getSnippetContributions(): List<SnippetContribution> | ||
| } | ||
|
|
||
| data class SnippetContribution( | ||
| val language: String, | ||
| val scope: String, | ||
| val prefix: String, | ||
| val description: String, | ||
| val body: List<String>, | ||
| ) |
5 changes: 5 additions & 0 deletions
5
plugin-api/src/main/kotlin/com/itsaky/androidide/plugins/services/IdeSnippetService.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,5 @@ | ||
| package com.itsaky.androidide.plugins.services | ||
|
|
||
| interface IdeSnippetService { | ||
| fun refreshSnippets(pluginId: String) | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.