-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and clean up
IndentationRule
### What's done: * See #1448.
- Loading branch information
1 parent
a50c713
commit 378cf81
Showing
7 changed files
with
356 additions
and
134 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
...t-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationAmount.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,59 @@ | ||
package org.cqfn.diktat.ruleset.rules.chapter3.files | ||
|
||
import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig | ||
|
||
/** | ||
* Encapsulates the change in the indentation level. | ||
*/ | ||
@Suppress("WRONG_DECLARATIONS_ORDER") | ||
internal enum class IndentationAmount { | ||
/** | ||
* The indent should be preserved at the current level. | ||
*/ | ||
NONE, | ||
|
||
/** | ||
* The indent should be increased or decreased by 1 (regular single indent). | ||
*/ | ||
SINGLE, | ||
|
||
/** | ||
* Extended, or _continuation_ indent. Applicable when any of | ||
* [`extendedIndent*`][IndentationConfig] flags is **on**. | ||
*/ | ||
EXTENDED, | ||
; | ||
|
||
/** | ||
* @return the indentation level. To get the actual indentation (the amount | ||
* of space characters), the value needs to be multiplied by | ||
* [IndentationConfig.indentationSize]. | ||
* @see IndentationConfig.indentationSize | ||
*/ | ||
fun level(): Int = | ||
ordinal | ||
|
||
/** | ||
* @return whether this amount represents the change in the indentation | ||
* level, i.e. whether the element should be indented or un-indented. | ||
*/ | ||
fun isNonZero(): Boolean = | ||
level() > 0 | ||
|
||
companion object { | ||
/** | ||
* A convenience factory method. | ||
* | ||
* @param extendedIndent the actual value of ony of the `extendedIndent*` | ||
* flags. | ||
* @return the corresponding indentation amount, either [SINGLE] or | ||
* [EXTENDED]. | ||
*/ | ||
@JvmStatic | ||
fun valueOf(extendedIndent: Boolean): IndentationAmount = | ||
when { | ||
extendedIndent -> EXTENDED | ||
else -> SINGLE | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...at-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationAware.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,11 @@ | ||
package org.cqfn.diktat.ruleset.rules.chapter3.files | ||
|
||
/** | ||
* A contract for types which encapsulate the indentation level. | ||
*/ | ||
internal interface IndentationAware { | ||
/** | ||
* @return the indentation (the amount of space characters) of this element. | ||
*/ | ||
val indentation: Int | ||
} |
76 changes: 76 additions & 0 deletions
76
...es/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationConfigAware.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,76 @@ | ||
package org.cqfn.diktat.ruleset.rules.chapter3.files | ||
|
||
import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig | ||
|
||
/** | ||
* Higher-level abstractions on top of the [indentation size][IndentationConfig.indentationSize]. | ||
*/ | ||
internal interface IndentationConfigAware { | ||
/** | ||
* The configuration this instance encapsulates. | ||
*/ | ||
val configuration: IndentationConfig | ||
|
||
/** | ||
* Increases the indentation level by [level] * [IndentationConfig.indentationSize]. | ||
* | ||
* This extension doesn't modify the receiver. | ||
* | ||
* @receiver the previous indentation level (in space characters), not | ||
* modified by the function call. | ||
* @param level the indentation level, 1 by default. | ||
* @return the new indentation level. | ||
* @see unindent | ||
* @see IndentationConfig.indentationSize | ||
*/ | ||
fun Int.indent(level: Int = 1): Int = | ||
this + level * configuration.indentationSize | ||
|
||
/** | ||
* Decreases the indentation level by [level] * [IndentationConfig.indentationSize]. | ||
* | ||
* This extension doesn't modify the receiver. | ||
* | ||
* @receiver the previous indentation level (in space characters), not | ||
* modified by the function call. | ||
* @param level the indentation level, 1 by default. | ||
* @return the new indentation level. | ||
* @see indent | ||
* @see IndentationConfig.indentationSize | ||
*/ | ||
fun Int.unindent(level: Int = 1): Int = | ||
indent(-level) | ||
|
||
/** | ||
* @receiver the previous indentation level (in space characters), not | ||
* modified by the function call. | ||
* @param amount the indentation amount. | ||
* @return the new (increased) indentation level. | ||
* @see minus | ||
*/ | ||
operator fun Int.plus(amount: IndentationAmount): Int = | ||
indent(level = amount.level()) | ||
|
||
/** | ||
* @receiver the previous indentation level (in space characters), not | ||
* modified by the function call. | ||
* @param amount the indentation amount. | ||
* @return the new (decreased) indentation level. | ||
* @see plus | ||
*/ | ||
operator fun Int.minus(amount: IndentationAmount): Int = | ||
unindent(level = amount.level()) | ||
|
||
companion object Factory { | ||
/** | ||
* Creates a new instance. | ||
* | ||
* @param configuration the configuration this instance will wrap. | ||
* @return the newly created instance. | ||
*/ | ||
operator fun invoke(configuration: IndentationConfig): IndentationConfigAware = | ||
object : IndentationConfigAware { | ||
override val configuration = configuration | ||
} | ||
} | ||
} |
Oops, something went wrong.