-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b56a40d
commit eaa7e87
Showing
8 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/main/java/dev/blachut/svelte/lang/format/SvelteExpressionBlock.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,75 @@ | ||
package dev.blachut.svelte.lang.format | ||
|
||
import com.intellij.formatting.* | ||
import com.intellij.lang.ASTNode | ||
import com.intellij.lang.LanguageFormatting | ||
import com.intellij.lang.javascript.JSTokenTypes | ||
import com.intellij.psi.formatter.common.AbstractBlock | ||
import com.intellij.psi.formatter.xml.AbstractXmlBlock | ||
import com.intellij.psi.formatter.xml.XmlFormattingPolicy | ||
import dev.blachut.svelte.lang.psi.SvelteTagElementTypes | ||
import dev.blachut.svelte.lang.psi.SvelteTokenTypes | ||
|
||
class SvelteExpressionBlock( | ||
node: ASTNode, | ||
private val indent: Indent?, | ||
wrap: Wrap?, | ||
private val policy: XmlFormattingPolicy | ||
) : | ||
AbstractBlock(node, wrap, null) { | ||
override fun isLeaf(): Boolean = false | ||
|
||
override fun buildChildren(): MutableList<Block> { | ||
val results = ArrayList<Block>(4) | ||
|
||
// borrowed from com.intellij.psi.formatter.common.InjectedLanguageBlockBuilder.addInjectedLanguageBlockWrapper | ||
val nodePsi = myNode.psi | ||
val builder = LanguageFormatting.INSTANCE.forContext(nodePsi.language, nodePsi) | ||
|
||
var child = myNode.firstChildNode | ||
while (child != null) { | ||
if (child.textLength > 0 && !AbstractXmlBlock.containsWhiteSpacesOnly(child)) { | ||
if (child.elementType === JSTokenTypes.LBRACE) { | ||
val startTag = SvelteTagElementTypes.START_TAGS.contains(myNode.elementType) | ||
val wrap = if (startTag) Wrap.createWrap(WrapType.ALWAYS, true) else null | ||
results.add(SvelteLeafBlock(child, wrap = wrap)) | ||
} else if (child.elementType === JSTokenTypes.RBRACE) { | ||
results.add(SvelteLeafBlock(child, indent = Indent.getNoneIndent())) | ||
} else { | ||
if (builder != null) { | ||
val childModel = builder.createModel(child.psi, policy.settings) | ||
results.add(childModel.rootBlock) | ||
} else { | ||
results.add(SvelteLeafBlock(child)) | ||
} | ||
} | ||
} | ||
|
||
child = child.treeNext | ||
} | ||
|
||
return results | ||
} | ||
|
||
override fun getSpacing(child1: Block?, child2: Block): Spacing? { | ||
if (child1 !is ASTBlock || child2 !is ASTBlock) { | ||
return null | ||
} | ||
|
||
val node1 = child1.node ?: return null | ||
val node2 = child2.node ?: return null | ||
|
||
val type1 = node1.elementType | ||
val type2 = node2.elementType | ||
|
||
if (SvelteTokenTypes.KEYWORDS.contains(type1) && type2 !== JSTokenTypes.RBRACE) { | ||
return Spacing.createSpacing(1, 1, 0, true, 0) | ||
} | ||
|
||
return null | ||
} | ||
|
||
override fun getIndent(): Indent? { | ||
return indent | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/dev/blachut/svelte/lang/format/SvelteLeafBlock.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,19 @@ | ||
package dev.blachut.svelte.lang.format | ||
|
||
import com.intellij.formatting.Block | ||
import com.intellij.formatting.Indent | ||
import com.intellij.formatting.Spacing | ||
import com.intellij.formatting.Wrap | ||
import com.intellij.lang.ASTNode | ||
import com.intellij.psi.formatter.common.AbstractBlock | ||
|
||
class SvelteLeafBlock(node: ASTNode, private val indent: Indent? = null, wrap: Wrap? = null) : | ||
AbstractBlock(node, wrap, null) { | ||
override fun isLeaf(): Boolean = true | ||
|
||
override fun buildChildren(): MutableList<Block> = EMPTY | ||
|
||
override fun getSpacing(child1: Block?, child2: Block): Spacing? = null | ||
|
||
override fun getIndent(): Indent? = indent | ||
} |
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
4 changes: 4 additions & 0 deletions
4
src/test/resources/dev/blachut/svelte/lang/format/MultilineExpression.svelte
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,4 @@ | ||
{@debug { | ||
field: true, | ||
another: "hello" | ||
}} |
4 changes: 4 additions & 0 deletions
4
src/test/resources/dev/blachut/svelte/lang/format/MultilineExpression_after.svelte
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,4 @@ | ||
{@debug { | ||
field: true, | ||
another: "hello" | ||
}} |
6 changes: 6 additions & 0 deletions
6
src/test/resources/dev/blachut/svelte/lang/format/MultilineProp.svelte
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,6 @@ | ||
<Box options={{ | ||
gap: 10, | ||
nested: { | ||
option: true | ||
} | ||
}}/> |
6 changes: 6 additions & 0 deletions
6
src/test/resources/dev/blachut/svelte/lang/format/MultilineProp_after.svelte
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,6 @@ | ||
<Box options={{ | ||
gap: 10, | ||
nested: { | ||
option: true | ||
} | ||
}}/> |