Skip to content

Commit 035f8b7

Browse files
committed
feat(devin-lang): add stub support and refactoring to highlighter and parser #101
This commit introduces stub support for the DevInLanguage by creating a new `DevInFileStub` class and its corresponding `Type`, which extends `IStubFileElementType`. The `DevInFile` class has been updated to use the `getOriginalFile` method and to override `toString` and `getStub` methods. The `DevInSyntaxHighlighter` and `DevInCompletionContributor` have been refactored to use the new `DevInTypes.REF_BLOCK` token type in their respective extensions. The `DevInLexer` has been modified to recognize references with the `@` and `$` prefixes, and the `DevInFileType` has been updated to use the extension `.devin` instead of `.autoin`.
1 parent f7990a6 commit 035f8b7

File tree

6 files changed

+42
-8
lines changed

6 files changed

+42
-8
lines changed

exts/devin-lang/src/grammar/DevInLexer.flex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import com.intellij.psi.TokenType;
2626
%s ID_SUFFIX
2727

2828
IDENTIFIER=[a-zA-Z0-9]([_\-a-zA-Z0-9]*)
29-
REF_BLOCK=("@" {IDENTIFIER} )
29+
REF_BLOCK=([$/@] {IDENTIFIER} )
3030
TEXT_SEGMENT=[^$/@]+
3131
NEWLINE=\n|\r\n
3232

exts/devin-lang/src/main/kotlin/cc/unitmesh/language/DevInCompletionContributor.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import cc.unitmesh.language.completion.VariableProvider
88

99
class DevInCompletionContributor : CompletionContributor() {
1010
init {
11-
extend(CompletionType.BASIC, psiElement(DevInTypes.REF_BLOCK), VariableProvider())
11+
extend(
12+
CompletionType.BASIC,
13+
psiElement(DevInTypes.REF_BLOCK),
14+
VariableProvider()
15+
)
1216
}
1317
}

exts/devin-lang/src/main/kotlin/cc/unitmesh/language/DevInParserDefinition.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ internal class DevInParserDefinition : ParserDefinition {
4545

4646
@NotNull
4747
override fun createElement(node: ASTNode?): PsiElement {
48-
// return DevInTypes.Factory.createElement(node)
4948
TODO()
5049
}
5150

exts/devin-lang/src/main/kotlin/cc/unitmesh/language/DevInSyntaxHighlighter.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package cc.unitmesh.language
22

3-
import cc.unitmesh.language.lexer.DevInTokenType
4-
import cc.unitmesh.language.psi.DevInElementType
53
import cc.unitmesh.language.psi.DevInTypes
64
import com.intellij.lexer.Lexer
75
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
86
import com.intellij.openapi.editor.colors.TextAttributesKey
97
import com.intellij.openapi.fileTypes.SyntaxHighlighter
108
import com.intellij.openapi.fileTypes.SyntaxHighlighterBase
11-
import com.intellij.openapi.fileTypes.SyntaxHighlighterBase.fillMap
129
import com.intellij.psi.tree.IElementType
1310

1411
class DevInSyntaxHighlighter : SyntaxHighlighter {
@@ -22,7 +19,7 @@ class DevInSyntaxHighlighter : SyntaxHighlighter {
2219
private val ATTRIBUTES: MutableMap<IElementType, TextAttributesKey> = HashMap()
2320

2421
init {
25-
ATTRIBUTES[DevInTypes.REF_BLOCK] = DefaultLanguageHighlighterColors.IDENTIFIER
22+
ATTRIBUTES[DevInTypes.REF_BLOCK] = DefaultLanguageHighlighterColors.KEYWORD
2623
}
2724
}
2825

exts/devin-lang/src/main/kotlin/cc/unitmesh/language/psi/DevInFile.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,42 @@ import cc.unitmesh.language.DevInLanguage
55
import com.intellij.extapi.psi.PsiFileBase
66
import com.intellij.openapi.fileTypes.FileType
77
import com.intellij.psi.FileViewProvider
8+
import com.intellij.psi.PsiFile
9+
import com.intellij.psi.StubBuilder
10+
import com.intellij.psi.stubs.*
11+
import com.intellij.psi.tree.IStubFileElementType
812

913
class DevInFile(viewProvider: FileViewProvider) : PsiFileBase(viewProvider, DevInLanguage) {
1014
override fun getFileType(): FileType = DevInFileType.INSTANCE
1115

16+
override fun getOriginalFile(): DevInFile = super.getOriginalFile() as DevInFile
17+
18+
override fun toString(): String = "DevInFile"
19+
20+
override fun getStub(): DevInFileStub? = super.getStub() as DevInFileStub?
1221
}
22+
23+
class DevInFileStub(file: DevInFile?, private val flags: Int) : PsiFileStubImpl<DevInFile>(file) {
24+
25+
override fun getType() = Type
26+
27+
object Type : IStubFileElementType<DevInFileStub>(DevInLanguage) {
28+
override fun getStubVersion(): Int = 1
29+
30+
override fun getExternalId(): String = "Feakin.file"
31+
32+
override fun serialize(stub: DevInFileStub, dataStream: StubOutputStream) {
33+
dataStream.writeByte(stub.flags)
34+
}
35+
36+
override fun deserialize(dataStream: StubInputStream, parentStub: StubElement<*>?): DevInFileStub {
37+
return DevInFileStub(null, dataStream.readUnsignedByte())
38+
}
39+
40+
override fun getBuilder(): StubBuilder = object : DefaultStubBuilder() {
41+
override fun createStubForFile(file: PsiFile): StubElement<*> {
42+
return DevInFileStub(file as DevInFile, 0)
43+
}
44+
}
45+
}
46+
}

exts/devin-lang/src/main/resources/cc.unitmesh.language.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<extensions defaultExtensionNs="com.intellij">
33
<!-- refs: https://github.com/JetBrains/intellij-sdk-code-samples/blob/main/simple_language_plugin/src/main/resources/META-INF/plugin.xml-->
44
<fileType name="DevInFile" implementationClass="cc.unitmesh.language.DevInFileType" fieldName="INSTANCE"
5-
language="DevIn" extensions="autoin"/>
5+
language="DevIn" extensions="devin"/>
66

77
<lang.parserDefinition language="DevIn" implementationClass="cc.unitmesh.language.DevInParserDefinition"/>
88
<lang.syntaxHighlighterFactory language="DevIn"

0 commit comments

Comments
 (0)