Skip to content

Commit

Permalink
feat: make folding works
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Sep 27, 2022
1 parent de5450e commit 0ec55c7
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 74 deletions.
8 changes: 4 additions & 4 deletions src/main/kotlin/com/feakin/intellij/FeakinFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import com.intellij.openapi.fileTypes.FileType
import com.intellij.psi.FileViewProvider

class FeakinFile(viewProvider: FileViewProvider) : PsiFileBase(viewProvider, FeakinLanguage) {
override fun getFileType(): FileType {
return FeakinFileType.INSTANCE
}
override fun getFileType(): FileType = FeakinFileType;

override fun getOriginalFile(): FeakinFile = super.getOriginalFile() as FeakinFile

override fun toString(): String {
return "Feakin File"
return "Feakin"
}
}
24 changes: 8 additions & 16 deletions src/main/kotlin/com/feakin/intellij/FeakinFileType.kt
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
package com.feakin.intellij

import com.intellij.openapi.fileTypes.LanguageFileType
import com.intellij.openapi.vfs.VirtualFile
import javax.swing.Icon

class FeakinFileType : LanguageFileType(FeakinLanguage) {
override fun getName(): String {
return "Feakin File"
}
object FeakinFileType : LanguageFileType(FeakinLanguage) {

override fun getDescription(): String {
return "Feakin language file"
}
override fun getName(): String = "Feakin File"

override fun getDefaultExtension(): String {
return "fkl"
}
override fun getIcon(): Icon = FeakinIcons.FILE

override fun getIcon(): Icon {
return FeakinIcons.FILE
}
override fun getDefaultExtension(): String = "fkl"

companion object {
val INSTANCE = FeakinFileType()
}
override fun getCharset(file: VirtualFile, content: ByteArray): String = "UTF-8"

override fun getDescription(): String = "Feakin DSL file"
}
1 change: 0 additions & 1 deletion src/main/kotlin/com/feakin/intellij/FeakinLanguage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ import com.intellij.lang.Language

object FeakinLanguage : Language("Feakin", "text/feakin", "text/x-feakin", "application/x-feakin") {
override fun isCaseSensitive() = true

override fun getDisplayName() = "Feakin"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.feakin.intellij.completion

import com.feakin.intellij.FeakinLanguage
import com.feakin.intellij.lexer.FeakinTypes.BLOCK_COMMENT
import com.intellij.application.options.CodeStyle
import com.intellij.codeInsight.generation.CommenterDataHolder
import com.intellij.codeInsight.generation.SelfManagingCommenter
Expand Down
67 changes: 43 additions & 24 deletions src/main/kotlin/com/feakin/intellij/edit/FeakinFoldingBuilder.kt
Original file line number Diff line number Diff line change
@@ -1,42 +1,61 @@
package com.feakin.intellij.edit

import com.feakin.intellij.FeakinFile
import com.feakin.intellij.psi.FeakinContextMapDeclaration
import com.feakin.intellij.lexer.FeakinElementTypes.LBRACE
import com.feakin.intellij.lexer.FeakinElementTypes.RBRACE
import com.feakin.intellij.psi.FeakinContextBody
import com.feakin.intellij.psi.FeakinContextMapBody
import com.feakin.intellij.psi.FeakinVisitor
import com.intellij.lang.ASTNode
import com.intellij.lang.folding.FoldingBuilderEx
import com.intellij.lang.folding.CustomFoldingBuilder
import com.intellij.lang.folding.FoldingDescriptor
import com.intellij.openapi.editor.Document
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement
import com.intellij.psi.tree.TokenSet
import com.intellij.psi.util.PsiTreeUtil
import java.util.*

class FeakinFoldingBuilder : FoldingBuilderEx() {
override fun getPlaceholderText(node: ASTNode): String {
return "..."
class FeakinFoldingBuilder : CustomFoldingBuilder(), DumbAware {

override fun buildLanguageFoldRegions(
descriptors: MutableList<FoldingDescriptor>,
root: PsiElement,
document: Document,
quick: Boolean
) {
if (root !is FeakinFile) return

val visitor = FoldingVisitor(descriptors)
PsiTreeUtil.processElements(root) { it.accept(visitor); true }
}

override fun buildFoldRegions(element: PsiElement, document: Document, quick: Boolean): Array<FoldingDescriptor> {
val descriptors: MutableList<FoldingDescriptor> = ArrayList()
if (element !is FeakinFile) {
return FoldingDescriptor.EMPTY
override fun getLanguagePlaceholderText(node: ASTNode, range: TextRange): String {
when (node.elementType) {
LBRACE -> return " { "
RBRACE -> return " }"
}
val structs = PsiTreeUtil.getChildrenOfType(element, FeakinContextMapDeclaration::class.java)
?: return descriptors.toTypedArray()
for (struct in structs) {
val structNameDeclaration = struct.contextMapName
val nameEnd = structNameDeclaration.node.textRange.startOffset
val structEnd = struct.node.textRange.endOffset
val foldingDescriptor = FoldingDescriptor(
Objects.requireNonNull(struct),
TextRange(nameEnd, structEnd)
)
descriptors.add(foldingDescriptor)
return when (node.psi) {
is PsiComment -> "/* ... */"
else -> "{...}"
}
return descriptors.toTypedArray()
}

override fun isCollapsedByDefault(node: ASTNode): Boolean {
return true
override fun isRegionCollapsedByDefault(node: ASTNode): Boolean {
return node.elementType in COLLAPSED_BY_DEFAULT
}

private companion object {
val COLLAPSED_BY_DEFAULT = TokenSet.create(LBRACE, RBRACE)
}

private class FoldingVisitor(private val descriptors: MutableList<FoldingDescriptor>) : FeakinVisitor() {
override fun visitContextBody(o: FeakinContextBody) = fold(o)

override fun visitContextMapBody(o: FeakinContextMapBody) = fold(o)
private fun fold(element: PsiElement) {
descriptors += FoldingDescriptor(element.node, element.textRange)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.feakin.intellij.highlight

import com.feakin.intellij.FeakinLexerAdapter
import com.feakin.intellij.lexer.FeakinTypes
import com.feakin.intellij.lexer.FeakinLexerAdapter
import com.feakin.intellij.lexer.FeakinElementTypes
import com.intellij.lexer.Lexer
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
import com.intellij.openapi.editor.colors.TextAttributesKey
Expand All @@ -22,13 +22,24 @@ class FeakinSyntaxHighlighter : SyntaxHighlighterBase() {

init {
fillMap(ATTRIBUTES, FeakinTokenTypeSets.KEY_WORDS, DefaultLanguageHighlighterColors.KEYWORD)
ATTRIBUTES[FeakinTypes.IDENTIFIER] = DefaultLanguageHighlighterColors.IDENTIFIER
ATTRIBUTES[FeakinElementTypes.IDENTIFIER] = DefaultLanguageHighlighterColors.IDENTIFIER

ATTRIBUTES[FeakinTypes.COMMENT] = DefaultLanguageHighlighterColors.LINE_COMMENT;
ATTRIBUTES[FeakinTypes.CLOSE_BRACE] = DefaultLanguageHighlighterColors.BRACES;
ATTRIBUTES[FeakinTypes.OPEN_BRACE] = DefaultLanguageHighlighterColors.BRACES;
ATTRIBUTES[FeakinElementTypes.COMMENT] = DefaultLanguageHighlighterColors.LINE_COMMENT;
ATTRIBUTES[FeakinElementTypes.BLOCK_COMMENT] = DefaultLanguageHighlighterColors.BLOCK_COMMENT;

ATTRIBUTES[FeakinTypes.STRING_LITERAL] = DefaultLanguageHighlighterColors.STRING;
ATTRIBUTES[FeakinElementTypes.LBRACE] = DefaultLanguageHighlighterColors.BRACES;
ATTRIBUTES[FeakinElementTypes.RBRACE] = DefaultLanguageHighlighterColors.BRACES;

ATTRIBUTES[FeakinElementTypes.COMMA] = DefaultLanguageHighlighterColors.COMMA;
ATTRIBUTES[FeakinElementTypes.SEMICOLON] = DefaultLanguageHighlighterColors.SEMICOLON;

ATTRIBUTES[FeakinElementTypes.COLON] = DefaultLanguageHighlighterColors.COMMA;

ATTRIBUTES[FeakinElementTypes.RARROW] = DefaultLanguageHighlighterColors.CONSTANT;
ATTRIBUTES[FeakinElementTypes.LARROW] = DefaultLanguageHighlighterColors.CONSTANT;
ATTRIBUTES[FeakinElementTypes.DARROW] = DefaultLanguageHighlighterColors.CONSTANT;

ATTRIBUTES[FeakinElementTypes.STRING_LITERAL] = DefaultLanguageHighlighterColors.STRING;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.feakin.intellij.highlight

import com.feakin.intellij.lexer.FeakinTypes
import com.feakin.intellij.lexer.FeakinElementTypes
import com.intellij.psi.tree.TokenSet

object FeakinTokenTypeSets {
var KEY_WORDS = TokenSet.create(
FeakinTypes.CONTEXT_KEYWORD,
FeakinTypes.CONTEXT_MAP_KEYWORD,
FeakinTypes.ENTITY_KEYWORD,
FeakinTypes.AGGREGATION_KEYWORD,
FeakinTypes.VALUE_OBJECT_KEYWORD,
FeakinElementTypes.CONTEXT_KEYWORD,
FeakinElementTypes.CONTEXT_MAP_KEYWORD,
FeakinElementTypes.ENTITY_KEYWORD,
FeakinElementTypes.AGGREGATION_KEYWORD,
FeakinElementTypes.VALUE_OBJECT_KEYWORD,
FeakinElementTypes.STRUCT_KEYWORD,
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.feakin.intellij
package com.feakin.intellij.lexer

import com.feakin.intellij.lexer._FeakinLexer
import com.intellij.lexer.FlexAdapter

class FeakinLexerAdapter : FlexAdapter(_FeakinLexer(null))
class FeakinLexerAdapter : FlexAdapter(com.feakin.intellij.lexer._FeakinLexer(null))
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ import com.intellij.psi.tree.IFileElementType
import com.intellij.psi.tree.TokenSet

class FeakinParserDefinition : ParserDefinition {
companion object {
val WHITE_SPACES = TokenSet.create(TokenType.WHITE_SPACE)
val COMMENTS = TokenSet.create(FeakinElementTypes.COMMENT)
val FILE = IFileElementType(FeakinLanguage)
}

override fun createLexer(project: Project): Lexer {
return FeakinLexerAdapter()
}

override fun createParser(project: Project): PsiParser {
return FeakinParser()
return com.feakin.intellij.parser.FeakinParser()
}

override fun getFileNodeType(): IFileElementType {
Expand All @@ -44,10 +50,4 @@ class FeakinParserDefinition : ParserDefinition {
override fun createFile(viewProvider: FileViewProvider): PsiFile {
return FeakinFile(viewProvider)
}

companion object {
val WHITE_SPACES = TokenSet.create(TokenType.WHITE_SPACE)
val COMMENTS = TokenSet.create(FeakinElementTypes.COMMENT)
val FILE = IFileElementType(FeakinLanguage)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.feakin.intellij.parser

import com.feakin.intellij.FeakinIcons
import com.feakin.intellij.lexer.FeakinTypes
import com.feakin.intellij.lexer.FeakinElementTypes
import com.feakin.intellij.psi.FeakinContextMapDeclaration
import com.feakin.intellij.psi.FeakinNameComponent
import com.intellij.lang.PsiBuilder
Expand All @@ -18,7 +18,7 @@ object FeakinPsiImplUtil {
}

fun getKey(element: FeakinContextMapDeclaration): String? {
val keyNode = element.node.findChildByType(FeakinTypes.CONTEXT_MAP_DECLARATION)
val keyNode = element.node.findChildByType(FeakinElementTypes.CONTEXT_MAP_DECLARATION)
return keyNode?.text?.replace("\\\\ ".toRegex(), " ")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class FeakinContextDeclarationNameIndex : StringStubIndexExtension<FeakinContext
override fun getKey(): StubIndexKey<String, FeakinContextDeclaration> {
return KEY
}

companion object {
val KEY = StubIndexKey.createIndexKey<String, FeakinContextDeclaration>("feakin.declaration.shortName")
fun allKeys(project: Project?): Collection<String> {
Expand Down
14 changes: 12 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@
<extensions defaultExtensionNs="com.intellij">
<stubIndex implementation="com.feakin.intellij.psi.stubs.FeakinContextDeclarationNameIndex"/>

<fileType name="Feakin File" implementationClass="com.feakin.intellij.FeakinFileType" fieldName="INSTANCE"
language="Feakin" extensions="fkl"/>
<!-- File-type Factory -->
<fileType name="Feakin File"
language="Feakin"
implementationClass="com.feakin.intellij.FeakinFileType"
fieldName="INSTANCE"
extensions="fkl"/>
<internalFileTemplate name="Feakin File"/>

<!-- Parser -->

<lang.parserDefinition language="Feakin" implementationClass="com.feakin.intellij.parser.FeakinParserDefinition"/>
<stubElementTypeHolder class="com.feakin.intellij.lexer.FeakinElementTypes"/>

<lang.syntaxHighlighter language="Feakin"
implementationClass="com.feakin.intellij.highlight.FeakinSyntaxHighlighter"/>
Expand Down

0 comments on commit 0ec55c7

Please sign in to comment.