-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from dirkgroot/add-auto-formatting
Add support for IntelliJ's "Reformat Code" command
- Loading branch information
Showing
18 changed files
with
430 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,8 @@ indent_size = 4 | |
indent_style = space | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
indent_size = 2 | ||
|
||
[*.yml] | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/kotlin/nl/dirkgroot/structurizr/dsl/lang/SDBlock.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package nl.dirkgroot.structurizr.dsl.lang | ||
|
||
import com.intellij.formatting.* | ||
import com.intellij.lang.ASTNode | ||
import com.intellij.psi.TokenType.WHITE_SPACE | ||
import com.intellij.psi.formatter.common.AbstractBlock | ||
import com.intellij.psi.tree.TokenSet | ||
import nl.dirkgroot.structurizr.dsl.psi.SDTypes.* | ||
|
||
class SDBlock( | ||
node: ASTNode, | ||
private val spacingBuilder: SpacingBuilder, | ||
private val indent: Indent = Indent.getNoneIndent(), | ||
wrap: Wrap = Wrap.createWrap(WrapType.NONE, false), | ||
alignment: Alignment = Alignment.createAlignment() | ||
) : AbstractBlock(node, wrap, alignment) { | ||
override fun buildChildren(): MutableList<Block> { | ||
val result = mutableListOf<Block>() | ||
var child = myNode.firstChildNode | ||
while (child != null) { | ||
if (child.elementType != WHITE_SPACE && child.elementType != CRLF) { | ||
val indent = if (child.elementType in INDENT_TOKEN_TYPES && myNode.treeParent != null) | ||
Indent.getNormalIndent(true) | ||
else | ||
Indent.getNoneIndent() | ||
val block = SDBlock(child, spacingBuilder, indent) | ||
result.add(block) | ||
} | ||
child = child.treeNext | ||
} | ||
return result | ||
} | ||
|
||
override fun getIndent() = indent | ||
|
||
override fun getSpacing(child1: Block?, child2: Block) = spacingBuilder.getSpacing(this, child1, child2) | ||
|
||
override fun isLeaf() = myNode.firstChildNode == null | ||
|
||
companion object { | ||
private val INDENT_TOKEN_TYPES = TokenSet.create( | ||
BLOCK_COMMENT, | ||
BLOCK_STATEMENT, | ||
EXPLICIT_RELATIONSHIP, | ||
IDENTIFIER_REFERENCES, | ||
IMPLICIT_RELATIONSHIP, | ||
KEY_VALUE_PAIR, | ||
LINE_COMMENT, | ||
PROPERTY_BLOCK_STATEMENT, | ||
SCRIPT_BLOCK, | ||
SINGLE_LINE_STATEMENT, | ||
) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/nl/dirkgroot/structurizr/dsl/lang/SDFormattingModelBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package nl.dirkgroot.structurizr.dsl.lang | ||
|
||
import com.intellij.formatting.* | ||
import com.intellij.psi.codeStyle.CodeStyleSettings | ||
import nl.dirkgroot.structurizr.dsl.StructurizrDSLLanguage | ||
import nl.dirkgroot.structurizr.dsl.psi.SDTypes | ||
|
||
class SDFormattingModelBuilder : FormattingModelBuilder { | ||
override fun createModel(formattingContext: FormattingContext): FormattingModel = | ||
FormattingModelProvider.createFormattingModelForPsiFile( | ||
formattingContext.containingFile, | ||
SDBlock(formattingContext.node, createSpacingBuilder(formattingContext.codeStyleSettings)), | ||
formattingContext.codeStyleSettings | ||
) | ||
|
||
private fun createSpacingBuilder(codeStyleSettings: CodeStyleSettings) = | ||
SpacingBuilder(codeStyleSettings, StructurizrDSLLanguage) | ||
.around(SDTypes.EQUALS).spaces(1) | ||
.around(SDTypes.IDENTIFIER_NAME).spaces(1) | ||
.before(SDTypes.ARGUMENT).spaces(1) | ||
.before(SDTypes.BRACE1).spaces(1) | ||
.between(SDTypes.KEY, SDTypes.VALUE).spaces(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/test/kotlin/nl/dirkgroot/structurizr/dsl/editing/formatter/FormatterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package nl.dirkgroot.structurizr.dsl.editing.formatter | ||
|
||
import com.intellij.openapi.command.WriteCommandAction | ||
import com.intellij.psi.codeStyle.CodeStyleManager | ||
import nl.dirkgroot.structurizr.dsl.support.StructurizrDSLCodeInsightTest | ||
|
||
abstract class FormatterTest : StructurizrDSLCodeInsightTest() { | ||
protected fun assertFormattingResult(code: String, expectedResult: String) { | ||
val file = fixture.addFileToProject("test.dsl", code) | ||
fixture.openFileInEditor(file.virtualFile) | ||
|
||
WriteCommandAction.writeCommandAction(project).run<Exception> { | ||
CodeStyleManager.getInstance(project) | ||
.reformatText(fixture.file, listOf(fixture.file.textRange)) | ||
} | ||
fixture.checkResult(expectedResult) | ||
} | ||
} |
229 changes: 229 additions & 0 deletions
229
src/test/kotlin/nl/dirkgroot/structurizr/dsl/editing/formatter/IndentationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
package nl.dirkgroot.structurizr.dsl.editing.formatter | ||
|
||
import org.junit.jupiter.api.Test | ||
|
||
class IndentationTest : FormatterTest() { | ||
@Test | ||
fun `block with single line element`() { | ||
assertFormattingResult( | ||
""" | ||
softwareSystem name description { | ||
container Backend | ||
} | ||
""".trimIndent(), | ||
""" | ||
softwareSystem name description { | ||
container Backend | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
|
||
@Test | ||
fun `nested blocks`() { | ||
assertFormattingResult( | ||
""" | ||
softwareSystem name description { | ||
container Backend { | ||
component RestAPI | ||
} | ||
} | ||
""".trimIndent(), | ||
""" | ||
softwareSystem name description { | ||
container Backend { | ||
component RestAPI | ||
} | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
|
||
@Test | ||
fun `property block`() { | ||
assertFormattingResult( | ||
""" | ||
properties { | ||
a b | ||
c d | ||
} | ||
""".trimIndent(), | ||
""" | ||
properties { | ||
a b | ||
c d | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
|
||
@Test | ||
fun `nested property block`() { | ||
assertFormattingResult( | ||
""" | ||
softwareSystem system { | ||
properties { | ||
a b | ||
c d | ||
} | ||
} | ||
""".trimIndent(), | ||
""" | ||
softwareSystem system { | ||
properties { | ||
a b | ||
c d | ||
} | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
|
||
@Test | ||
fun `block with assigned identifier`() { | ||
assertFormattingResult( | ||
""" | ||
systemIdentifier = softwareSystem system { | ||
container backend | ||
} | ||
""".trimIndent(), | ||
""" | ||
systemIdentifier = softwareSystem system { | ||
container backend | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
|
||
@Test | ||
fun `script block`() { | ||
assertFormattingResult( | ||
""" | ||
!script kotlin { | ||
println("Hello World") | ||
} | ||
""".trimIndent(), | ||
""" | ||
!script kotlin { | ||
println("Hello World") | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
|
||
@Test | ||
fun `nested script block`() { | ||
assertFormattingResult( | ||
""" | ||
workspace { | ||
!script kotlin { | ||
println("Hello World") | ||
} | ||
} | ||
""".trimIndent(), | ||
""" | ||
workspace { | ||
!script kotlin { | ||
println("Hello World") | ||
} | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
|
||
@Test | ||
fun `explicit relationships`() { | ||
assertFormattingResult( | ||
""" | ||
model { | ||
a = softwareSystem A | ||
b = softwareSystem B | ||
a -> b Uses | ||
} | ||
""".trimIndent(), | ||
""" | ||
model { | ||
a = softwareSystem A | ||
b = softwareSystem B | ||
a -> b Uses | ||
} | ||
""".trimIndent() | ||
) | ||
} | ||
|
||
@Test | ||
fun `implicit relationships`() { | ||
assertFormattingResult( | ||
""" | ||
model { | ||
a = softwareSystem A | ||
b = softwareSystem B { | ||
-> a Uses | ||
} | ||
} | ||
""".trimIndent(), | ||
""" | ||
model { | ||
a = softwareSystem A | ||
b = softwareSystem B { | ||
-> a Uses | ||
} | ||
} | ||
""".trimIndent() | ||
) | ||
} | ||
|
||
@Test | ||
fun `line comments`() { | ||
assertFormattingResult( | ||
""" | ||
model { | ||
# comment | ||
} | ||
""".trimIndent(), | ||
""" | ||
model { | ||
# comment | ||
} | ||
""".trimIndent() | ||
) | ||
} | ||
|
||
@Test | ||
fun `block comments`() { | ||
assertFormattingResult( | ||
""" | ||
model { | ||
/* | ||
comment | ||
*/ | ||
} | ||
""".trimIndent(), | ||
""" | ||
model { | ||
/* | ||
comment | ||
*/ | ||
} | ||
""".trimIndent() | ||
) | ||
} | ||
|
||
@Test | ||
fun `identifier references`() { | ||
assertFormattingResult( | ||
""" | ||
animation { | ||
a b c | ||
d | ||
} | ||
""".trimIndent(), | ||
""" | ||
animation { | ||
a b c | ||
d | ||
} | ||
""".trimIndent() | ||
) | ||
} | ||
} |
Oops, something went wrong.