-
Notifications
You must be signed in to change notification settings - Fork 6
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 #71 from anboralabs/fix-formatting
Fix formatting
- Loading branch information
Showing
21 changed files
with
451 additions
and
157 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
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
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
86 changes: 86 additions & 0 deletions
86
src/main/kotlin/co/anbora/labs/firebase/ide/formatter/FirebaseAlignmentStrategy.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,86 @@ | ||
/* | ||
* Use of this source code is governed by the MIT license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
package co.anbora.labs.firebase.ide.formatter | ||
|
||
import com.intellij.formatting.Alignment | ||
import com.intellij.lang.ASTNode | ||
import com.intellij.psi.tree.IElementType | ||
import com.intellij.psi.tree.TokenSet | ||
|
||
interface FirebaseAlignmentStrategy { | ||
/** | ||
* Requests current strategy for alignment to use for given child. | ||
*/ | ||
fun getAlignment(child: ASTNode, parent: ASTNode?, childCtx: FirebaseFmtContext): Alignment? | ||
|
||
/** | ||
* Always returns `null`. | ||
*/ | ||
object NullStrategy : FirebaseAlignmentStrategy { | ||
override fun getAlignment(child: ASTNode, parent: ASTNode?, childCtx: FirebaseFmtContext): Alignment? = null | ||
} | ||
|
||
/** | ||
* Apply this strategy only when child element is in [tt]. | ||
*/ | ||
fun alignIf(vararg tt: IElementType): FirebaseAlignmentStrategy = alignIf(TokenSet.create(*tt)) | ||
|
||
/** | ||
* Apply this strategy only when child element type matches [filterSet]. | ||
*/ | ||
fun alignIf(filterSet: TokenSet): FirebaseAlignmentStrategy = | ||
object : FirebaseAlignmentStrategy { | ||
override fun getAlignment(child: ASTNode, parent: ASTNode?, childCtx: FirebaseFmtContext): Alignment? = | ||
if (child.elementType in filterSet) { | ||
this@FirebaseAlignmentStrategy.getAlignment(child, parent, childCtx) | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
/** | ||
* Apply this strategy only when [predicate] passes. | ||
*/ | ||
fun alignIf(predicate: (child: ASTNode, parent: ASTNode?, ctx: FirebaseFmtContext) -> Boolean): FirebaseAlignmentStrategy = | ||
object : FirebaseAlignmentStrategy { | ||
override fun getAlignment(child: ASTNode, parent: ASTNode?, childCtx: FirebaseFmtContext): Alignment? = | ||
if (predicate(child, parent, childCtx)) { | ||
this@FirebaseAlignmentStrategy.getAlignment(child, parent, childCtx) | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
/** | ||
* Returns [NullStrategy] if [condition] is `false`. Useful for making strategies configurable. | ||
*/ | ||
fun alignIf(condition: Boolean): FirebaseAlignmentStrategy = | ||
if (condition) { | ||
this | ||
} else { | ||
NullStrategy | ||
} | ||
|
||
companion object { | ||
/** | ||
* Always returns [alignment]. | ||
*/ | ||
fun wrap(alignment: Alignment = Alignment.createAlignment()): FirebaseAlignmentStrategy = | ||
object : FirebaseAlignmentStrategy { | ||
override fun getAlignment(child: ASTNode, parent: ASTNode?, childCtx: FirebaseFmtContext): Alignment? = | ||
alignment | ||
} | ||
|
||
/** | ||
* Always returns [FirebaseFmtContext.sharedAlignment] | ||
*/ | ||
fun shared(): FirebaseAlignmentStrategy = | ||
object : FirebaseAlignmentStrategy { | ||
override fun getAlignment(child: ASTNode, parent: ASTNode?, childCtx: FirebaseFmtContext): Alignment? = | ||
childCtx.sharedAlignment | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/co/anbora/labs/firebase/ide/formatter/FirebaseFmtContext.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 co.anbora.labs.firebase.ide.formatter | ||
|
||
import co.anbora.labs.firebase.ide.formatter.impl.createSpacingBuilder | ||
import com.intellij.formatting.Alignment | ||
import com.intellij.formatting.SpacingBuilder | ||
import com.intellij.psi.codeStyle.CodeStyleSettings | ||
|
||
data class FirebaseFmtContext private constructor( | ||
val commonSettings: CodeStyleSettings, | ||
val spacingBuilder: SpacingBuilder, | ||
val sharedAlignment: Alignment? = null | ||
) { | ||
companion object { | ||
fun create(settings: CodeStyleSettings): FirebaseFmtContext { | ||
return FirebaseFmtContext(settings, createSpacingBuilder(settings)) | ||
} | ||
} | ||
|
||
} |
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
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/co/anbora/labs/firebase/ide/formatter/impl/alignment.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 co.anbora.labs.firebase.ide.formatter.impl | ||
|
||
import co.anbora.labs.firebase.ide.formatter.FirebaseAlignmentStrategy | ||
import co.anbora.labs.firebase.ide.formatter.FirebaseFormatterBlock | ||
import co.anbora.labs.firebase.lang.core.psi.FirebaseRulesTypes.CALL_ARGUMENTS | ||
import co.anbora.labs.firebase.lang.core.psi.FirebaseRulesTypes.FUNCTION_PARAMETER_LIST | ||
|
||
fun FirebaseFormatterBlock.getAlignmentStrategy(): FirebaseAlignmentStrategy = when (node.elementType) { | ||
FUNCTION_PARAMETER_LIST, CALL_ARGUMENTS -> | ||
FirebaseAlignmentStrategy | ||
.shared() | ||
.alignUnlessBlockDelim() | ||
.alignIf(ctx.commonSettings.ALIGN_MULTILINE_PARAMETERS) | ||
else -> FirebaseAlignmentStrategy.NullStrategy | ||
|
||
} | ||
|
||
fun FirebaseAlignmentStrategy.alignUnlessBlockDelim(): FirebaseAlignmentStrategy = | ||
alignIf { c, p, _ -> !c.isDelimiterOfCurrentBlock(p) } |
61 changes: 61 additions & 0 deletions
61
src/main/kotlin/co/anbora/labs/firebase/ide/formatter/impl/indent.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,61 @@ | ||
package co.anbora.labs.firebase.ide.formatter.impl | ||
|
||
import co.anbora.labs.firebase.ide.formatter.FirebaseFormatterBlock | ||
import co.anbora.labs.firebase.lang.core.psi.* | ||
import co.anbora.labs.firebase.lang.core.psi.FirebaseRulesTypes.CONDITIONAL_BLOCK | ||
import com.intellij.formatting.Indent | ||
import com.intellij.lang.ASTNode | ||
|
||
fun FirebaseFormatterBlock.computeIndent(child: ASTNode): Indent? { | ||
// val parentType = node.elementType | ||
val parentPsi = node.psi | ||
// val childType = child.elementType | ||
val childPsi = child.psi | ||
return when { | ||
node.isDelimitedBlock -> getNormalIndentIfNotCurrentBlockDelimiter(child, node) | ||
// do not indent statements | ||
childPsi.prevSibling == null -> Indent.getNoneIndent() | ||
// let a = | ||
// 92; | ||
// => | ||
// let a = | ||
// 92; | ||
// except if RefExpr as lhs of assignment expr | ||
// childPsi is MoveExpr | ||
// && (parentType == LET_EXPR || parentType == ASSIGNMENT_EXPR || parentType == CONST_DEF) -> Indent.getNormalIndent() | ||
childPsi is FirebaseRulesExpression | ||
&& parentPsi is FirebaseRulesVariableStatement -> Indent.getNormalIndent() | ||
|
||
childPsi is FirebaseRulesConditionalBlock | ||
&& parentPsi is FirebaseRulesConditionalStatement -> Indent.getNormalIndent() | ||
|
||
childPsi is FirebaseRulesConditionalStatement -> Indent.getNormalIndent() | ||
|
||
childPsi is FirebaseRulesConditionalBlock -> Indent.getNormalIndent() | ||
// if (true) | ||
// create() | ||
// else | ||
// delete() | ||
parentPsi is FirebaseRulesServiceBlock | ||
|| parentPsi is FirebaseRulesMatchBlock | ||
|| parentPsi is FirebaseRulesFunctionBlock | ||
|| parentPsi is FirebaseRulesReturnBlock -> Indent.getNormalIndent() | ||
|
||
// binary expressions, chain calls | ||
// no indent on it's own, use parent indent | ||
parentPsi is FirebaseRulesExpression -> Indent.getIndent(Indent.Type.NONE, true, true) | ||
|
||
else -> Indent.getNoneIndent() | ||
} | ||
} | ||
|
||
fun getNormalIndentIfNotCurrentBlockDelimiter(child: ASTNode, parent: ASTNode): Indent = | ||
if (child.isDelimiterOfCurrentBlock(parent)) { | ||
Indent.getNoneIndent() | ||
} else { | ||
if (parent.elementType == CONDITIONAL_BLOCK) { | ||
Indent.getNoneIndent() | ||
} else { | ||
Indent.getNormalIndent() | ||
} | ||
} |
Oops, something went wrong.