Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add detekt static analyzer #18

Merged
merged 3 commits into from
Dec 19, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: Add detekt static analyzer
* Enable detekt-formatting
petertrr committed Dec 19, 2021
commit 7d21957379a492e0ff033736e4249095009d2d2c
2 changes: 2 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -23,6 +23,8 @@ jobs:
with:
arguments: build
gradle-version: wrapper
properties: |
detektAutoCorrect=false
- name: Upload test reports
if: ${{ failure() }} # runs only if previous step has failed, the entire workflow will still be marked as failed
uses: actions/upload-artifact@v2
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -56,6 +56,10 @@ tasks.withType<KotlinJvmTest> {
detekt {
buildUponDefaultConfig = true
config = files("detekt.yml")
autoCorrect = (findProperty("detektAutoCorrect") as String?)?.toBoolean() ?: true
}
dependencies {
detektPlugins(libs.detekt.formatting)
}

// configure Jacoco-based code coverage reports for JVM tests executions
6 changes: 5 additions & 1 deletion detekt.yml
Original file line number Diff line number Diff line change
@@ -2,4 +2,8 @@ style:
active: true
MaxLineLength:
active: true
maxLineLength: 180
maxLineLength: 180
formatting:
active: true
MaximumLineLength:
active: false
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[versions]
kotlin = "1.6.0"
junit = "5.8.1"
detekt = "1.19.0"

[plugins]
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
detekt = { id = "io.gitlab.arturbosch.detekt", version = "1.19.0" }
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }

[libraries]
junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "junit" }
junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit" }
detekt-formatting = { module = "io.gitlab.arturbosch.detekt:detekt-formatting", version.ref = "detekt" }
25 changes: 17 additions & 8 deletions src/commonMain/kotlin/io/github/petertrr/diffutils/DiffUtils.kt
Original file line number Diff line number Diff line change
@@ -55,9 +55,13 @@ public fun <T> diff(original: List<T>, revised: List<T>, includeEqualParts: Bool
/**
* Computes the difference between the original and revised text.
*/
public fun diff(sourceText: String, targetText: String,
progress: DiffAlgorithmListener?): Patch<String> {
return diff(sourceText.split("\n"),
public fun diff(
sourceText: String,
targetText: String,
progress: DiffAlgorithmListener?
): Patch<String> {
return diff(
sourceText.split("\n"),
targetText.split("\n"),
progress
)
@@ -75,7 +79,8 @@ public fun diff(sourceText: String, targetText: String,
* @return The patch describing the difference between the original and revised sequences.
*/
public fun <T> diff(
source: List<T>, target: List<T>,
source: List<T>,
target: List<T>,
equalizer: ((T, T) -> Boolean)?
): Patch<T> {
return if (equalizer != null) {
@@ -87,8 +92,10 @@ public fun <T> diff(
}

public fun <T> diff(
original: List<T>, revised: List<T>,
algorithm: DiffAlgorithm<T>, progress: DiffAlgorithmListener?
original: List<T>,
revised: List<T>,
algorithm: DiffAlgorithm<T>,
progress: DiffAlgorithmListener?
): Patch<T> {
return diff(original, revised, algorithm, progress, false)
}
@@ -106,8 +113,10 @@ public fun <T> diff(
* `null`.
*/
public fun <T> diff(
original: List<T>, revised: List<T>,
algorithm: DiffAlgorithm<T>, progress: DiffAlgorithmListener?,
original: List<T>,
revised: List<T>,
algorithm: DiffAlgorithm<T>,
progress: DiffAlgorithmListener?,
includeEqualParts: Boolean
): Patch<T> {
return Patch.generate(original, revised, algorithm.computeDiff(original, revised, progress), includeEqualParts)
Original file line number Diff line number Diff line change
@@ -20,9 +20,10 @@ package io.github.petertrr.diffutils.algorithm

import io.github.petertrr.diffutils.patch.DeltaType

public data class Change(val deltaType: DeltaType,
val startOriginal: Int,
val endOriginal: Int,
val startRevised: Int,
val endRevised: Int
public data class Change(
val deltaType: DeltaType,
val startOriginal: Int,
val endOriginal: Int,
val startRevised: Int,
val endRevised: Int
)
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ public data class Chunk<T>(
* the positions of changed lines of chunk in the text
*/
val changePosition: List<Int>? = null
) {
) {
/**
* Verifies that this chunk's saved text matches the corresponding text in the given sequence.
*
Original file line number Diff line number Diff line change
@@ -164,7 +164,12 @@ public class DiffRowGenerator(
*
* @param endPos line number after previous delta end
*/
private fun transformDeltaIntoDiffRow(original: List<String>, endPos: Int, diffRows: MutableList<DiffRow>, delta: Delta<String>): Int {
private fun transformDeltaIntoDiffRow(
original: List<String>,
endPos: Int,
diffRows: MutableList<DiffRow>,
delta: Delta<String>
): Int {
val orig: Chunk<String> = delta.source
val rev: Chunk<String> = delta.target
for (line in original.subList(endPos, orig.position)) {
@@ -289,7 +294,7 @@ public class DiffRowGenerator(
val inlineOrig: Chunk<String> = inlineDelta.source
val inlineRev: Chunk<String> = inlineDelta.target
when (inlineDelta.type) {
DeltaType.DELETE -> {
DeltaType.DELETE -> {
wrapInTag(
origList, inlineOrig.position, inlineOrig.position + inlineOrig.size(),
DiffRow.Tag.DELETE, oldTag, processDiffs, replaceOriginalLinefeedInChangesWithSpaces && mergeOriginalRevised
@@ -382,33 +387,33 @@ public class DiffRowGenerator(
public companion object {
internal val DEFAULT_EQUALIZER: (Any?, Any?) -> Boolean = { o1: Any?, o2: Any? -> o1 == o2 }
internal val IGNORE_WHITESPACE_EQUALIZER: (String, String) -> Boolean = { original: String, revised: String ->
adjustWhitespace(
original
) == adjustWhitespace(revised)
}
adjustWhitespace(
original
) == adjustWhitespace(revised)
}
internal val LINE_NORMALIZER_FOR_HTML: (String) -> String = { normalize(it) }

/**
* Splitting lines by character to achieve char by char diff checking.
*/
internal val SPLITTER_BY_CHARACTER = { line: String ->
val list: MutableList<String> = ArrayList(line.length)
for (character in line.toCharArray()) {
list.add(character.toString())
}
list.toList()
val list: MutableList<String> = ArrayList(line.length)
for (character in line.toCharArray()) {
list.add(character.toString())
}
list.toList()
}
internal val SPLIT_BY_WORD_PATTERN = Regex("\\s+|[,.\\[\\](){}/\\\\*+\\-#]")

/**
* Splitting lines by word to achieve word by word diff checking.
*/
internal val SPLITTER_BY_WORD = { line: String ->
splitStringPreserveDelimiter(
line,
SPLIT_BY_WORD_PATTERN
)
}
splitStringPreserveDelimiter(
line,
SPLIT_BY_WORD_PATTERN
)
}
internal val WHITESPACE_PATTERN = Regex("\\s+")

private fun adjustWhitespace(raw: String): String {
@@ -473,7 +478,7 @@ public class DiffRowGenerator(
}
endPos--

//search position for end tag
// search position for end tag
while (endPos > startPosition) {
if ("\n" == sequence[endPos - 1]) {
if (replaceLinefeedWithSpace) {
Original file line number Diff line number Diff line change
@@ -52,7 +52,8 @@ internal fun wrapText(line: String, columnWidth: Int): String {
while (length > widthIndex) {
var breakPoint = widthIndex + delimiter * count
if (b[breakPoint - 1].isHighSurrogate() &&
b[breakPoint].isLowSurrogate()) {
b[breakPoint].isLowSurrogate()
) {
// Shift a breakpoint that would split a supplemental code-point.
breakPoint += 1
if (breakPoint == b.length) {