Skip to content

Commit 0dee173

Browse files
committed
Improve the formatter a bit for return list/indentation
1 parent ceb59b9 commit 0dee173

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22

33
## [Unreleased]
44

5+
### Added
6+
- Add a formatter option for space around commas in return lists.
7+
- Add a formatter option for space within return list parenthesis.
8+
59
### Changed
610
- The build window will now display internal compiler errors as well.
711

12+
### Fixed
13+
- Fix a bug with script block indentation.
14+
815
## [1.2.0] - 2023-08-18
916

1017
### Added

src/main/kotlin/io/runescript/plugin/ide/formatter/blocks/impl/RsScriptBlock.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import com.intellij.formatting.Indent
44
import com.intellij.lang.ASTNode
55
import io.runescript.plugin.ide.formatter.RsFormatterContext
66
import io.runescript.plugin.ide.formatter.blocks.RsBlock
7+
import io.runescript.plugin.lang.psi.RsElementTypes
78

8-
class RsScriptBlock(context: RsFormatterContext, node: ASTNode)
9-
: RsBlock(context, node, Indent.getNoneIndent(), null, null)
9+
class RsScriptBlock(context: RsFormatterContext, node: ASTNode) :
10+
RsBlock(context, node, Indent.getNoneIndent(), null, null) {
11+
12+
override fun getChildIndent(): Indent? {
13+
return Indent.getNoneIndent()
14+
}
15+
}

src/main/kotlin/io/runescript/plugin/ide/formatter/impl/RsSpacingBuilder.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ class RsSpacingBuilder(private val settings: CommonCodeStyleSettings,
4545
return spaceIf(settings.SPACE_AROUND_ASSIGNMENT_OPERATORS)
4646
}
4747
}
48-
if (type1 == DEFINE_TYPE || type1 == TYPE_NAME || type1 == ARRAY_TYPE_LITERAL) {
49-
return spaceIf(true)
50-
}
5148
if (type2 == SEMICOLON) {
5249
return spaceIf(settings.SPACE_BEFORE_SEMICOLON)
5350
}
@@ -110,6 +107,9 @@ class RsSpacingBuilder(private val settings: CommonCodeStyleSettings,
110107
if (elementType == CALC_EXPRESSION && (type1 == LPAREN || type2 == RPAREN)) {
111108
return spaceIf(rsSettings.SPACE_WITHIN_CALC_PARENTHESES)
112109
}
110+
if (elementType == RETURN_LIST && (type1 == LPAREN || type2 == RPAREN)) {
111+
return spaceIf(rsSettings.SPACE_WITHIN_RETURN_LIST_PARENTHESES)
112+
}
113113
if (elementType == ARGUMENT_LIST && (type1 == LPAREN || type2 == RPAREN)) {
114114
val superType = parent.node.treeParent.elementType
115115
if (superType == GOSUB_EXPRESSION || superType == COMMAND_EXPRESSION) {
@@ -118,16 +118,26 @@ class RsSpacingBuilder(private val settings: CommonCodeStyleSettings,
118118
}
119119
}
120120
if (elementType == ARGUMENT_LIST || elementType == SWITCH_CASE
121-
|| elementType == PARAMETER_LIST || elementType == RETURN_LIST) {
121+
|| elementType == PARAMETER_LIST) {
122122
if (type2 == COMMA) {
123123
return spaceIf(settings.SPACE_BEFORE_COMMA)
124124
} else if (type1 == COMMA) {
125125
return spaceIf(settings.SPACE_AFTER_COMMA)
126126
}
127127
}
128+
if (elementType == RETURN_LIST) {
129+
if (type2 == COMMA) {
130+
return spaceIf(rsSettings.SPACE_BEFORE_COMMA_IN_RETURN_LIST)
131+
} else if (type1 == COMMA) {
132+
return spaceIf(rsSettings.SPACE_AFTER_COMMA_IN_RETURN_LIST)
133+
}
134+
}
128135
if (type2 == ARGUMENT_LIST && (elementType == GOSUB_EXPRESSION || elementType == COMMAND_EXPRESSION)) {
129136
return spaceIf(settings.SPACE_BEFORE_METHOD_CALL_PARENTHESES)
130137
}
138+
if (type1 == DEFINE_TYPE || type1 == TYPE_NAME || type1 == ARRAY_TYPE_LITERAL) {
139+
return spaceIf(true)
140+
}
131141
return null
132142
}
133143

src/main/kotlin/io/runescript/plugin/ide/formatter/style/RsCodeStyleSettings.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,13 @@ class RsCodeStyleSettings(container: CodeStyleSettings) : CustomCodeStyleSetting
1717

1818
@JvmField
1919
var SPACE_WITHIN_CALC_PARENTHESES = false
20+
21+
@JvmField
22+
var SPACE_WITHIN_RETURN_LIST_PARENTHESES = false
23+
24+
@JvmField
25+
var SPACE_BEFORE_COMMA_IN_RETURN_LIST = false
26+
27+
@JvmField
28+
var SPACE_AFTER_COMMA_IN_RETURN_LIST = false
2029
}

src/main/kotlin/io/runescript/plugin/ide/formatter/style/RsLanguageCodeStyleSettingsProvider.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class RsLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvider()
1515
if (settingsType == SettingsType.SPACING_SETTINGS) {
1616
val spacesWithin = CodeStyleSettingsCustomizableOptions.getInstance().SPACES_WITHIN
1717
val spacesBeforeParen = CodeStyleSettingsCustomizableOptions.getInstance().SPACES_BEFORE_PARENTHESES
18+
val spacesOther = CodeStyleSettingsCustomizableOptions.getInstance().SPACES_OTHER
1819
consumer.showStandardOptions(
1920
// Before parentheses
2021
"SPACE_BEFORE_IF_PARENTHESES",
@@ -76,6 +77,11 @@ class RsLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvider()
7677
"'calc' parentheses",
7778
spacesWithin)
7879

80+
consumer.showCustomOption(RsCodeStyleSettings::class.java,
81+
"SPACE_WITHIN_RETURN_LIST_PARENTHESES",
82+
"Return list parentheses",
83+
spacesWithin)
84+
7985
consumer.showCustomOption(RsCodeStyleSettings::class.java,
8086
"SPACE_WITHIN_ARRAY_BOUNDS",
8187
"Array bounds",
@@ -85,6 +91,20 @@ class RsLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvider()
8591
"SPACE_BEFORE_ARRAY_BOUNDS",
8692
"Array bounds",
8793
spacesBeforeParen)
94+
95+
consumer.showCustomOption(RsCodeStyleSettings::class.java,
96+
"SPACE_BEFORE_COMMA_IN_RETURN_LIST",
97+
"Before comma in return list",
98+
spacesOther,
99+
CodeStyleSettingsCustomizable.OptionAnchor.AFTER,
100+
"SPACE_AFTER_COMMA")
101+
102+
consumer.showCustomOption(RsCodeStyleSettings::class.java,
103+
"SPACE_AFTER_COMMA_IN_RETURN_LIST",
104+
"After comma in return list",
105+
spacesOther,
106+
CodeStyleSettingsCustomizable.OptionAnchor.AFTER,
107+
"SPACE_AFTER_COMMA")
88108
}
89109
}
90110

@@ -131,5 +151,7 @@ class RsLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvider()
131151
return(${'$'}odd_count);
132152
133153
[proc,tostring](string ${'$'}text)
154+
155+
[proc,multiple_returns]()(int,int,string)
134156
""".trimIndent()
135157
}

0 commit comments

Comments
 (0)