generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add storefront snippet autocompletion
- Loading branch information
Showing
7 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/de/shyim/shopware6/completion/TwigCompletionProvider.kt
This file contains 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,32 @@ | ||
package de.shyim.shopware6.completion | ||
|
||
import com.intellij.codeInsight.completion.* | ||
import com.intellij.codeInsight.lookup.LookupElementBuilder | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.util.ProcessingContext | ||
import com.intellij.util.indexing.FileBasedIndex | ||
import de.shyim.shopware6.index.FrontendSnippetIndex | ||
import de.shyim.shopware6.util.TwigPattern | ||
import icons.ShopwareToolBoxIcons | ||
|
||
|
||
class TwigCompletionProvider() : CompletionContributor() { | ||
init { | ||
extend( | ||
CompletionType.BASIC, | ||
TwigPattern.getTranslationKeyPattern("trans", "transchoice"), | ||
object : CompletionProvider<CompletionParameters>() { | ||
override fun addCompletions( | ||
parameters: CompletionParameters, | ||
context: ProcessingContext, | ||
result: CompletionResultSet | ||
) { | ||
val project: Project = parameters.position.project | ||
for (key in FileBasedIndex.getInstance().getAllKeys(FrontendSnippetIndex.key, project)) { | ||
result.addElement(LookupElementBuilder.create(key!!).withIcon(ShopwareToolBoxIcons.SHOPWARE)) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/de/shyim/shopware6/index/FrontendSnippetIndex.kt
This file contains 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,50 @@ | ||
package de.shyim.shopware6.index | ||
|
||
import com.intellij.json.JsonFileType | ||
import com.intellij.util.indexing.* | ||
import com.intellij.util.io.DataExternalizer | ||
import com.intellij.util.io.EnumeratorStringDescriptor | ||
import com.intellij.util.io.KeyDescriptor | ||
import com.jetbrains.php.lang.psi.stubs.indexes.StringSetDataExternalizer | ||
import de.shyim.shopware6.util.SnippetUtil | ||
|
||
open class FrontendSnippetIndex : FileBasedIndexExtension<String, Set<String>>() { | ||
override fun getName(): ID<String, Set<String>> { | ||
return key | ||
} | ||
|
||
override fun getVersion(): Int { | ||
return 1 | ||
} | ||
|
||
override fun dependsOnFileContent(): Boolean { | ||
return true | ||
} | ||
|
||
override fun getIndexer(): DataIndexer<String, Set<String>, FileContent> { | ||
return DataIndexer { inputData -> | ||
if (!inputData.getFile().getName().equals("storefront.en-GB.json")) { | ||
return@DataIndexer mapOf() | ||
} | ||
|
||
return@DataIndexer SnippetUtil.flatten(inputData.contentAsText.toString()) | ||
} | ||
} | ||
|
||
override fun getKeyDescriptor(): KeyDescriptor<String> { | ||
return EnumeratorStringDescriptor.INSTANCE | ||
} | ||
|
||
override fun getValueExternalizer(): DataExternalizer<Set<String>> { | ||
return StringSetDataExternalizer.INSTANCE | ||
} | ||
|
||
companion object { | ||
val key = ID.create<String, Set<String>>("de.shyim.shopware6.frontend.snippet") | ||
} | ||
|
||
override fun getInputFilter(): FileBasedIndex.InputFilter { | ||
return object : DefaultFileTypeSpecificInputFilter(JsonFileType.INSTANCE) { | ||
} | ||
} | ||
} |
This file contains 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,40 @@ | ||
package de.shyim.shopware6.util | ||
|
||
import gnu.trove.THashMap | ||
import org.codehaus.jettison.json.JSONException | ||
import org.codehaus.jettison.json.JSONObject | ||
|
||
object SnippetUtil { | ||
fun flatten(fileContent: String?): THashMap<String, Set<String>> { | ||
val content = THashMap<String, Set<String>>() | ||
return try { | ||
val jsonObject = JSONObject(fileContent) | ||
val it = jsonObject.keys() | ||
while (it.hasNext()) { | ||
val key = it.next().toString() | ||
flatten(content, jsonObject, key, key) | ||
} | ||
content | ||
} catch (e: JSONException) { | ||
e.printStackTrace() | ||
content | ||
} | ||
} | ||
|
||
private fun flatten(content: THashMap<String, Set<String>>, json: JSONObject, key: String, prefix: String) { | ||
try { | ||
val jsonObject = json.getJSONObject(key) | ||
val it = jsonObject.keys() | ||
while (it.hasNext()) { | ||
val innerKey = it.next().toString() | ||
flatten(content, jsonObject, innerKey, "$prefix.$innerKey") | ||
} | ||
return | ||
} catch (ignored: JSONException) { | ||
} | ||
try { | ||
content[prefix] = setOf(json.getString(key)) | ||
} catch (ignored: JSONException) { | ||
} | ||
} | ||
} |
This file contains 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,33 @@ | ||
package de.shyim.shopware6.util | ||
|
||
import com.intellij.patterns.ElementPattern | ||
import com.intellij.patterns.PlatformPatterns | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiWhiteSpace | ||
import com.jetbrains.twig.TwigLanguage | ||
import com.jetbrains.twig.TwigTokenTypes | ||
|
||
object TwigPattern { | ||
fun getTranslationKeyPattern(vararg type: String?): ElementPattern<PsiElement?> { | ||
return PlatformPatterns | ||
.psiElement(TwigTokenTypes.STRING_TEXT) | ||
.beforeLeafSkipping( | ||
PlatformPatterns.or( | ||
PlatformPatterns.psiElement(PsiWhiteSpace::class.java), | ||
PlatformPatterns.psiElement(TwigTokenTypes.WHITE_SPACE), | ||
PlatformPatterns.psiElement(TwigTokenTypes.SINGLE_QUOTE), | ||
PlatformPatterns.psiElement(TwigTokenTypes.DOUBLE_QUOTE) | ||
), | ||
PlatformPatterns.psiElement(TwigTokenTypes.FILTER).beforeLeafSkipping( | ||
PlatformPatterns.or( | ||
PlatformPatterns.psiElement(TwigTokenTypes.WHITE_SPACE), | ||
PlatformPatterns.psiElement(PsiWhiteSpace::class.java) | ||
), | ||
PlatformPatterns.psiElement(TwigTokenTypes.IDENTIFIER).withText( | ||
PlatformPatterns.string().oneOf(*type) | ||
) | ||
) | ||
) | ||
.withLanguage(TwigLanguage.INSTANCE) | ||
} | ||
} |
This file contains 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