Skip to content

Commit 9251a4d

Browse files
committed
Add line indentation support to the formatter
1 parent f032aa1 commit 9251a4d

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Inspections that require symbols will no longer run on files outside the project.
1313
- Update the file extension for commands from ".op" to ".cs2".
1414
- Improve the handling and parsing of block comments.
15+
- Add line indentation support to the formatter.
1516

1617
## [1.3.0] - 2023-08-30
1718

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<lang.formatter language="RuneScript" implementationClass="io.runescript.plugin.ide.formatter.RsFormatter"/>
5858
<codeStyleSettingsProvider implementation="io.runescript.plugin.ide.formatter.style.RsCodeStyleSettingsProvider"/>
5959
<langCodeStyleSettingsProvider implementation="io.runescript.plugin.ide.formatter.style.RsLanguageCodeStyleSettingsProvider"/>
60+
<lineIndentProvider implementation="io.runescript.plugin.ide.formatter.lineIndent.RsLineIndentProvider"/>
6061

6162
<!-- Code Insights -->
6263
<codeInsight.lineMarkerProvider language="RuneScript" implementationClass="io.runescript.plugin.ide.lineMarker.RsLineMarkerProvider"/>

0 commit comments

Comments
 (0)