From 7d1c0876b73427b681754d943a820dcc5a9f805d Mon Sep 17 00:00:00 2001 From: ice1000 Date: Sat, 18 May 2019 12:19:05 -0700 Subject: [PATCH] [ cubical ] Contextual completion --- res/META-INF/plugin.xml | 1 + .../tt/editing/cubicaltt/completion.kt | 48 +++++++++++++++++++ src/org/ice1000/tt/psi/cubicaltt/stubs.kt | 19 ++++++-- 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/org/ice1000/tt/editing/cubicaltt/completion.kt diff --git a/res/META-INF/plugin.xml b/res/META-INF/plugin.xml index 07974ef..3a7f92e 100644 --- a/res/META-INF/plugin.xml +++ b/res/META-INF/plugin.xml @@ -74,6 +74,7 @@ + diff --git a/src/org/ice1000/tt/editing/cubicaltt/completion.kt b/src/org/ice1000/tt/editing/cubicaltt/completion.kt new file mode 100644 index 0000000..7ebf4ad --- /dev/null +++ b/src/org/ice1000/tt/editing/cubicaltt/completion.kt @@ -0,0 +1,48 @@ +package org.ice1000.tt.editing.cubicaltt + +import com.intellij.codeInsight.completion.* +import com.intellij.codeInsight.lookup.LookupElementBuilder +import com.intellij.openapi.project.DumbAware +import com.intellij.patterns.PlatformPatterns.psiElement +import com.intellij.util.ProcessingContext +import icons.TTIcons +import org.ice1000.tt.editing.SimpleProvider +import org.ice1000.tt.psi.childrenWithLeaves +import org.ice1000.tt.psi.cubicaltt.* + +class CubicalTTCompletionContributor : CompletionContributor(), DumbAware { + private val keywords = listOf( + "transparentAll", "transparent", + "transport", "undefined", "import", "module", "mutual", "opaque", + "split@", "unglue", "hComp", "hdata", "PathP", "split", "where", "comp", + "data", "fill", "glue", "Glue", "with", "id", "id", "let", "Id", "in", "U" + ).map { + LookupElementBuilder + .create(it) + .withTypeText("Keyword") + .withIcon(TTIcons.CUBICAL_TT) + .bold() + } + + init { + extend(CompletionType.BASIC, psiElement(), SimpleProvider(keywords)) + extend(CompletionType.BASIC, psiElement(CubicalTTTypes.NAME_EXP), object : CompletionProvider() { + override fun addCompletions( + parameters: CompletionParameters, + context: ProcessingContext, + result: CompletionResultSet + ) { + val file = parameters.originalFile as? CubicalTTFileImpl ?: return + file.childrenWithLeaves + .filterIsInstance() + .mapNotNull { it.moduleUsage } + .mapNotNull { it.reference?.resolve() } + .filterIsInstance() + .mapNotNull { it.stub } + .flatMap { + it.childrenStubs.asSequence().filterIsInstance() + }.forEach { result.addElement(it.lookupElement) } + } + }) + } +} diff --git a/src/org/ice1000/tt/psi/cubicaltt/stubs.kt b/src/org/ice1000/tt/psi/cubicaltt/stubs.kt index 2382058..2b300ea 100644 --- a/src/org/ice1000/tt/psi/cubicaltt/stubs.kt +++ b/src/org/ice1000/tt/psi/cubicaltt/stubs.kt @@ -1,10 +1,13 @@ package org.ice1000.tt.psi.cubicaltt +import com.intellij.codeInsight.lookup.LookupElement +import com.intellij.codeInsight.lookup.LookupElementBuilder import com.intellij.lang.ASTNode import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import com.intellij.psi.stubs.* import com.intellij.psi.tree.IStubFileElementType +import icons.SemanticIcons import org.ice1000.tt.CubicalTTLanguage import org.ice1000.tt.psi.childrenWithLeaves import org.ice1000.tt.psi.cubicaltt.impl.CubicalTTDataImpl @@ -40,20 +43,30 @@ abstract class CubicalTTStubType, Psi: PsiElement>( override fun getExternalId() = "cubicaltt.${super.toString()}" } +interface CubicalCompletionElement { + val lookupElement: LookupElement +} + class CubicalTTDefStub( parent: StubElement<*>, val declName: String -) : StubBase(parent, CubicalTTDefStubType), StubElement +) : StubBase(parent, CubicalTTDefStubType), StubElement, CubicalCompletionElement { + override val lookupElement get() = LookupElementBuilder.create(declName).withIcon(SemanticIcons.PINK_LAMBDA) +} class CubicalTTLabelStub( parent: StubElement<*>, val labelName: String -) : StubBase(parent, CubicalTTLabelStubType), StubElement +) : StubBase(parent, CubicalTTLabelStubType), StubElement, CubicalCompletionElement { + override val lookupElement get() = LookupElementBuilder.create(labelName).withIcon(SemanticIcons.BLUE_C) +} class CubicalTTDataStub( parent: StubElement<*>, val dataName: String -) : StubBase(parent, CubicalTTDataStubType), StubElement +) : StubBase(parent, CubicalTTDataStubType), StubElement, CubicalCompletionElement { + override val lookupElement get() = LookupElementBuilder.create(dataName).withIcon(SemanticIcons.BLUE_HOLE) +} class CubicalTTModuleStub( parent: StubElement<*>,