1
+ package io.runescript.plugin.ide.formatter.lineIndent
2
+
3
+ import com.intellij.formatting.Indent
4
+ import com.intellij.lang.Language
5
+ import com.intellij.openapi.editor.Editor
6
+ import com.intellij.openapi.project.Project
7
+ import com.intellij.psi.TokenType
8
+ import com.intellij.psi.impl.source.codeStyle.SemanticEditorPosition
9
+ import com.intellij.psi.impl.source.codeStyle.lineIndent.IndentCalculator
10
+ import com.intellij.psi.impl.source.codeStyle.lineIndent.JavaLikeLangLineIndentProvider
11
+ import com.intellij.psi.impl.source.codeStyle.lineIndent.JavaLikeLangLineIndentProvider.JavaLikeElement.*
12
+ import com.intellij.psi.tree.IElementType
13
+ import io.runescript.plugin.lang.RuneScript
14
+ import io.runescript.plugin.lang.psi.RsElementTypes
15
+ import io.runescript.plugin.lang.psi.RsTokenTypes
16
+
17
+ class RsLineIndentProvider : JavaLikeLangLineIndentProvider () {
18
+ override fun mapType (tokenType : IElementType ): SemanticEditorPosition .SyntaxElement ? {
19
+ return elementsMap[tokenType]
20
+ }
21
+
22
+ override fun getIndent (project : Project , editor : Editor , language : Language ? , offset : Int ): IndentCalculator ? {
23
+ val currentPosition = getPosition(editor, offset)
24
+ val before = currentPosition.beforeOptionalMix(Whitespace , LineComment , BlockComment )
25
+ val factory = IndentCalculatorFactory (project, editor)
26
+ if (getPosition(editor, offset).before().isAt(LeftParenthesis )) {
27
+ return null
28
+ }
29
+ return when {
30
+ before.isAt(Semicolon ) -> factory.createIndentCalculator(Indent .getNoneIndent(), IndentCalculator .LINE_BEFORE )
31
+ before.isAt(BlockOpeningBrace ) -> factory.createIndentCalculator(Indent .getNormalIndent()) { before.startOffset }
32
+ else -> super .getIndent(project, editor, language, offset)
33
+ }
34
+ }
35
+
36
+ override fun isSuitableForLanguage (language : Language ) = language.isKindOf(RuneScript )
37
+
38
+ companion object {
39
+ private val elementsMap: Map <IElementType , SemanticEditorPosition .SyntaxElement > = hashMapOf(
40
+ TokenType .WHITE_SPACE to Whitespace ,
41
+ RsElementTypes .SEMICOLON to Semicolon ,
42
+ RsElementTypes .LBRACE to BlockOpeningBrace ,
43
+ RsElementTypes .RBRACE to BlockClosingBrace ,
44
+ RsElementTypes .LPAREN to RightParenthesis ,
45
+ RsElementTypes .RPAREN to LeftParenthesis ,
46
+ RsElementTypes .COLON to Colon ,
47
+ RsElementTypes .CASE to SwitchCase ,
48
+ RsElementTypes .DEFAULT to SwitchDefault ,
49
+ RsElementTypes .ELSE to ElseKeyword ,
50
+ RsElementTypes .IF to IfKeyword ,
51
+ RsTokenTypes .MULTI_LINE_COMMENT to BlockComment ,
52
+ RsTokenTypes .SINGLE_LINE_COMMENT to LineComment ,
53
+ RsElementTypes .COMMA to Comma
54
+ )
55
+ }
56
+ }
0 commit comments