Skip to content

Commit

Permalink
[ cubical ] Contextual completion
Browse files Browse the repository at this point in the history
  • Loading branch information
ice1000 committed May 18, 2019
1 parent 291af10 commit 7d1c087
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
1 change: 1 addition & 0 deletions res/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<gotoSymbolContributor implementation="org.ice1000.tt.editing.cubicaltt.CubicalTTDeclNavigationContributor"/>
<gotoSymbolContributor implementation="org.ice1000.tt.editing.cubicaltt.CubicalTTDataNavigationContributor"/>
<gotoSymbolContributor implementation="org.ice1000.tt.editing.cubicaltt.CubicalTTLabelNavigationContributor"/>
<completion.contributor language="CubicalTT" implementationClass="org.ice1000.tt.editing.cubicaltt.CubicalTTCompletionContributor"/>

<!--MLPolyR-->
<configurationType implementation="org.ice1000.tt.execution.MLPolyRRunConfigurationType"/>
Expand Down
48 changes: 48 additions & 0 deletions src/org/ice1000/tt/editing/cubicaltt/completion.kt
Original file line number Diff line number Diff line change
@@ -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<CompletionParameters>() {
override fun addCompletions(
parameters: CompletionParameters,
context: ProcessingContext,
result: CompletionResultSet
) {
val file = parameters.originalFile as? CubicalTTFileImpl ?: return
file.childrenWithLeaves
.filterIsInstance<CubicalTTImportMixin>()
.mapNotNull { it.moduleUsage }
.mapNotNull { it.reference?.resolve() }
.filterIsInstance<CubicalTTModuleMixin>()
.mapNotNull { it.stub }
.flatMap {
it.childrenStubs.asSequence().filterIsInstance<CubicalCompletionElement>()
}.forEach { result.addElement(it.lookupElement) }
}
})
}
}
19 changes: 16 additions & 3 deletions src/org/ice1000/tt/psi/cubicaltt/stubs.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -40,20 +43,30 @@ abstract class CubicalTTStubType<Stub: StubElement<*>, Psi: PsiElement>(
override fun getExternalId() = "cubicaltt.${super.toString()}"
}

interface CubicalCompletionElement {
val lookupElement: LookupElement
}

class CubicalTTDefStub(
parent: StubElement<*>,
val declName: String
) : StubBase<CubicalTTDef>(parent, CubicalTTDefStubType), StubElement<CubicalTTDef>
) : StubBase<CubicalTTDef>(parent, CubicalTTDefStubType), StubElement<CubicalTTDef>, CubicalCompletionElement {
override val lookupElement get() = LookupElementBuilder.create(declName).withIcon(SemanticIcons.PINK_LAMBDA)
}

class CubicalTTLabelStub(
parent: StubElement<*>,
val labelName: String
) : StubBase<CubicalTTLabel>(parent, CubicalTTLabelStubType), StubElement<CubicalTTLabel>
) : StubBase<CubicalTTLabel>(parent, CubicalTTLabelStubType), StubElement<CubicalTTLabel>, CubicalCompletionElement {
override val lookupElement get() = LookupElementBuilder.create(labelName).withIcon(SemanticIcons.BLUE_C)
}

class CubicalTTDataStub(
parent: StubElement<*>,
val dataName: String
) : StubBase<CubicalTTData>(parent, CubicalTTDataStubType), StubElement<CubicalTTData>
) : StubBase<CubicalTTData>(parent, CubicalTTDataStubType), StubElement<CubicalTTData>, CubicalCompletionElement {
override val lookupElement get() = LookupElementBuilder.create(dataName).withIcon(SemanticIcons.BLUE_HOLE)
}

class CubicalTTModuleStub(
parent: StubElement<*>,
Expand Down

0 comments on commit 7d1c087

Please sign in to comment.