diff --git a/src/main/kotlin/me/mattco/serenityos/gml/GMLParserDefinition.kt b/src/main/kotlin/me/mattco/serenityos/gml/GMLParserDefinition.kt index 9afcf8b..3040906 100644 --- a/src/main/kotlin/me/mattco/serenityos/gml/GMLParserDefinition.kt +++ b/src/main/kotlin/me/mattco/serenityos/gml/GMLParserDefinition.kt @@ -5,6 +5,7 @@ import com.intellij.lang.ParserDefinition import com.intellij.openapi.project.Project import com.intellij.psi.FileViewProvider import com.intellij.psi.PsiElement +import com.intellij.psi.TokenType import com.intellij.psi.tree.TokenSet class GMLParserDefinition : ParserDefinition { @@ -21,7 +22,7 @@ class GMLParserDefinition : ParserDefinition { override fun getStringLiteralElements(): TokenSet = TokenSet.EMPTY companion object { - private val WHITE_SPACES = TokenSet.create(GMLLexerBase.SPACE) + private val WHITE_SPACES = TokenSet.create(TokenType.WHITE_SPACE) private val COMMENTS = TokenSet.create(GMLTypes.COMMENT) } } diff --git a/src/main/kotlin/me/mattco/serenityos/gml/GMLSyntaxHighlighterFactory.kt b/src/main/kotlin/me/mattco/serenityos/gml/GMLSyntaxHighlighterFactory.kt index fffcfe8..1f3e1c3 100644 --- a/src/main/kotlin/me/mattco/serenityos/gml/GMLSyntaxHighlighterFactory.kt +++ b/src/main/kotlin/me/mattco/serenityos/gml/GMLSyntaxHighlighterFactory.kt @@ -39,6 +39,7 @@ class GMLSyntaxHighlighter : SyntaxHighlighterBase() { COMMENT -> Highlights.COMMENT NUMBER -> Highlights.NUMBER STRING -> Highlights.STRING + BOOLEAN -> Highlights.BOOLEAN OPEN_CURLY, CLOSE_CURLY -> Highlights.BRACES OPEN_BRACKET, CLOSE_BRACKET -> Highlights.BRACKETS diff --git a/src/main/kotlin/me/mattco/serenityos/gml/annotators/GMLSyntaxAnnotator.kt b/src/main/kotlin/me/mattco/serenityos/gml/annotators/GMLSyntaxAnnotator.kt index 059c68d..c81b1d8 100644 --- a/src/main/kotlin/me/mattco/serenityos/gml/annotators/GMLSyntaxAnnotator.kt +++ b/src/main/kotlin/me/mattco/serenityos/gml/annotators/GMLSyntaxAnnotator.kt @@ -14,8 +14,6 @@ class GMLSyntaxAnnotator : DSLAnnotator(), DumbAware { override fun annotate(element: PsiElement, holder: Holder) = with(holder) { when (element) { is GMLValue -> { - if (element.boolean != null) - element.highlight(Highlights.BOOLEAN) if (element.string != null) { val lastChar = element.string!!.text.lastOrNull() if (lastChar != null && lastChar != '"') diff --git a/src/main/kotlin/me/mattco/serenityos/gml/formatting/GMLBraceMatcher.kt b/src/main/kotlin/me/mattco/serenityos/gml/formatting/GMLBraceMatcher.kt index 1f6877e..5d1fdfa 100644 --- a/src/main/kotlin/me/mattco/serenityos/gml/formatting/GMLBraceMatcher.kt +++ b/src/main/kotlin/me/mattco/serenityos/gml/formatting/GMLBraceMatcher.kt @@ -3,6 +3,7 @@ package me.mattco.serenityos.gml.formatting import com.intellij.lang.BracePair import com.intellij.lang.PairedBraceMatcher import com.intellij.psi.PsiFile +import com.intellij.psi.TokenType import com.intellij.psi.tree.IElementType import me.mattco.serenityos.gml.GMLLexerBase import me.mattco.serenityos.gml.GMLTypes @@ -24,7 +25,7 @@ class GMLBraceMatcher : PairedBraceMatcher { private val ALLOWED_CONTEXT_TOKENS = setOf( GMLTypes.CLOSE_CURLY, GMLTypes.COMMENT, - GMLLexerBase.SPACE, + TokenType.WHITE_SPACE, ) } } diff --git a/src/main/kotlin/me/mattco/serenityos/gml/formatting/GMLFormattingBlock.kt b/src/main/kotlin/me/mattco/serenityos/gml/formatting/GMLFormattingBlock.kt index 911f7cf..101280c 100644 --- a/src/main/kotlin/me/mattco/serenityos/gml/formatting/GMLFormattingBlock.kt +++ b/src/main/kotlin/me/mattco/serenityos/gml/formatting/GMLFormattingBlock.kt @@ -23,7 +23,7 @@ class GMLFormattingBlock( GMLFormattingBlock( it, null, - findAlignmentForGMLNode(it), + null, findIndentForGMLNode(it), spacingBuilder, ) diff --git a/src/main/kotlin/me/mattco/serenityos/gml/formatting/GMLLineIndentProvider.kt b/src/main/kotlin/me/mattco/serenityos/gml/formatting/GMLLineIndentProvider.kt new file mode 100644 index 0000000..9f3ab2a --- /dev/null +++ b/src/main/kotlin/me/mattco/serenityos/gml/formatting/GMLLineIndentProvider.kt @@ -0,0 +1,49 @@ +package me.mattco.serenityos.gml.formatting + +import com.intellij.formatting.Indent +import com.intellij.lang.Language +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import com.intellij.psi.impl.source.codeStyle.SemanticEditorPosition.SyntaxElement +import com.intellij.psi.impl.source.codeStyle.lineIndent.IndentCalculator +import com.intellij.psi.impl.source.codeStyle.lineIndent.JavaLikeLangLineIndentProvider +import com.intellij.psi.tree.IElementType +import me.mattco.serenityos.gml.GMLLanguage +import me.mattco.serenityos.gml.GMLTypes.* + +class GMLLineIndentProvider : JavaLikeLangLineIndentProvider() { + override fun mapType(tokenType: IElementType) = when (tokenType) { + OPEN_CURLY -> GMLSyntaxElement.OpenCurly + CLOSE_CURLY -> GMLSyntaxElement.CloseCurly + CLOSE_BRACKET, NUMBER, STRING, BOOLEAN -> GMLSyntaxElement.Value + else -> null + } + + override fun getIndent(project: Project, editor: Editor, language: Language?, offset: Int): IndentCalculator? { + if (getPosition(editor, offset).matchesRule { it.isAfterOnSameLine(GMLSyntaxElement.Value) }) { + return IndentCalculatorFactory(project, editor).createIndentCalculator( + Indent.Type.NONE, + IndentCalculator.LINE_BEFORE + ) + } else if (getPosition(editor, offset).matchesRule { it.isAfterOnSameLine(GMLSyntaxElement.OpenCurly) } + && !getPosition(editor, offset).matchesRule { it.isAfterOnSameLine(GMLSyntaxElement.CloseCurly) }) { + return IndentCalculatorFactory(project, editor).createIndentCalculator( + Indent.Type.NORMAL, + IndentCalculator.LINE_BEFORE + ) + } + + return IndentCalculatorFactory(project, editor).createIndentCalculator( + Indent.Type.NONE, + IndentCalculator.LINE_AFTER + ) + } + + override fun isSuitableForLanguage(language: Language) = language == GMLLanguage + + enum class GMLSyntaxElement : SyntaxElement { + OpenCurly, + CloseCurly, + Value, + } +} diff --git a/src/main/kotlin/me/mattco/serenityos/gml/formatting/rules.kt b/src/main/kotlin/me/mattco/serenityos/gml/formatting/rules.kt index 527f602..1abf5ab 100644 --- a/src/main/kotlin/me/mattco/serenityos/gml/formatting/rules.kt +++ b/src/main/kotlin/me/mattco/serenityos/gml/formatting/rules.kt @@ -1,16 +1,19 @@ package me.mattco.serenityos.gml.formatting -import com.intellij.formatting.Alignment import com.intellij.formatting.Indent import com.intellij.lang.ASTNode import com.intellij.psi.codeStyle.CommonCodeStyleSettings import me.mattco.serenityos.common.formatting.SpacingBuilder import me.mattco.serenityos.common.formatting.elementType -import me.mattco.serenityos.gml.GMLFileStub import me.mattco.serenityos.gml.GMLTypes.* fun buildGMLSpacingRules(settings: CommonCodeStyleSettings) = SpacingBuilder(settings).apply { simple { + between(OPEN_CURLY, CLOSE_CURLY).spacing(0, 0, 0, false, 0) + between(PROPERTY, CLOSE_CURLY).spacing(0, 0, 0, false, 0) + between(OPEN_CURLY, COMPONENT_BODY).spacing(0, 0, 1, false, 0) + between(COMPONENT_BODY, CLOSE_CURLY).spacing(0, 0, 1, false, 0) + after(AT).spaces(0) before(OPEN_CURLY).spaces(1) after(OPEN_CURLY).spaces(0) @@ -25,6 +28,9 @@ fun buildGMLSpacingRules(settings: CommonCodeStyleSettings) = SpacingBuilder(set around(type).spaces(0) between(COLON, COMPONENT).spacing(1, 1, 0, false, 0) + between(PROPERTY, PROPERTY).spacing(0, 0, 1, false, 0) + between(PROPERTY, COMPONENT).spacing(0, 0, 2, false, 0) + between(COMPONENT, PROPERTY).spacing(0, 0, 2, false, 0) } contextual(parent = COMPONENT_BODY, right = COMPONENT) { _, left, _ -> @@ -36,24 +42,9 @@ fun buildGMLSpacingRules(settings: CommonCodeStyleSettings) = SpacingBuilder(set } } -fun findAlignmentForGMLNode(node: ASTNode): Alignment? { - val parentType = node.treeParent?.elementType ?: return null - if (parentType == COMPONENT_BODY) - return Alignment.createAlignment() - return null -} - fun findIndentForGMLNode(node: ASTNode): Indent? { - if (node.elementType == PROPERTY) + if (node.elementType == COMPONENT_BODY) return Indent.getNormalIndent() - if (node.elementType == COMPONENT) { - // Only indent if this isn't a property value - val parent = node.treeParent ?: return Indent.getNoneIndent() - val parentType = parent.elementType - if (parentType != PROPERTY && parentType != GMLFileStub.Type) - return Indent.getNormalIndent() - } - return Indent.getNoneIndent() } diff --git a/src/main/kotlin/me/mattco/serenityos/gml/lexer.kt b/src/main/kotlin/me/mattco/serenityos/gml/lexer.kt index ec2f620..95ec328 100644 --- a/src/main/kotlin/me/mattco/serenityos/gml/lexer.kt +++ b/src/main/kotlin/me/mattco/serenityos/gml/lexer.kt @@ -12,9 +12,4 @@ class GMLToken(debugName: String) : IElementType(debugName, GMLLanguage) { class GMLElementType(debugName: String) : IElementType(debugName, GMLLanguage) -abstract class GMLLexerBase : FlexLexer { - companion object { - @JvmField - val SPACE = GMLElementType("SPACE") - } -} +abstract class GMLLexerBase : FlexLexer diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index e5987ea..c7876c8 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -48,6 +48,9 @@ language="SerenityOS GML" implementationClass="me.mattco.serenityos.gml.formatting.GMLBraceMatcher" /> + { - {WHITE_SPACE} { return SPACE; } + {WHITE_SPACE} { return WHITE_SPACE; } {COMMENT} { return COMMENT; } "{" { return OPEN_CURLY; } @@ -45,6 +47,7 @@ COMMENT=\/\/.*|\/\*[^]*?\* "\"" { yybegin(YYSTRING); } + {BOOLEAN} { return BOOLEAN; } {IDENTIFIER} { return IDENTIFIER; } {NUMBER} { return NUMBER; } }