-
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.
Remove deprecated calculateLineColByOffset methods from Ktlint (#394)
* Remove deprecated calculateLineColByOffset methods from Ktlint ### What's done: Copy-paste methods from ktlint
- Loading branch information
Showing
3 changed files
with
83 additions
and
9 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
71 changes: 71 additions & 0 deletions
71
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/PositionInTextLocator.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,71 @@ | ||
package org.cqfn.diktat.ruleset.utils | ||
|
||
//fixme this code is copy-pasted from ktlint. Change it | ||
internal typealias LineAndColumn = Pair<Int, Int> | ||
|
||
/** | ||
* Calculate position in text - line and column based on offset from the text start. | ||
*/ | ||
internal fun buildPositionInTextLocator(text: String): (offset: Int) -> LineAndColumn { | ||
val textLength = text.length | ||
val identifierArray = ArrayList<Int>() | ||
var endOfLineIndex = -1 | ||
|
||
do { | ||
identifierArray.add(endOfLineIndex + 1) | ||
endOfLineIndex = text.indexOf('\n', endOfLineIndex + 1) | ||
} while (endOfLineIndex != -1) | ||
|
||
identifierArray.add(textLength + if (identifierArray.last() == textLength) 1 else 0) | ||
|
||
val segmentTree = SegmentTree(identifierArray.toIntArray()) | ||
|
||
return { offset -> | ||
val line = segmentTree.indexOf(offset) | ||
if (line != -1) { | ||
val column = offset - segmentTree.get(line).left | ||
line + 1 to column + 1 | ||
} else { | ||
1 to 1 | ||
} | ||
} | ||
} | ||
|
||
private class SegmentTree(sortedArray: IntArray) { | ||
|
||
init { | ||
require(sortedArray.size > 1) { "At least two data points are required" } | ||
sortedArray.reduce { current, next -> | ||
require(current <= next) { "Data points are not sorted (ASC)" } | ||
next | ||
} | ||
} | ||
|
||
private val segments: List<Segment> = sortedArray | ||
.dropLast(1) | ||
.mapIndexed { index: Int, element: Int -> | ||
Segment(element, sortedArray[index + 1] - 1) | ||
} | ||
|
||
fun get(index: Int): Segment = segments[index] | ||
|
||
fun indexOf(index: Int): Int = binarySearch(index, 0, segments.size - 1) | ||
|
||
private fun binarySearch(compareElement: Int, left: Int, right: Int): Int = when { | ||
left > right -> -1 | ||
else -> { | ||
val index = left + (right - left) / 2 | ||
val midElement = segments[index] | ||
if (compareElement < midElement.left) { | ||
binarySearch(compareElement, left, index - 1) | ||
} else { | ||
if (midElement.right < compareElement) binarySearch(compareElement, index + 1, right) else index | ||
} | ||
} | ||
} | ||
} | ||
|
||
private data class Segment( | ||
val left: Int, | ||
val right: Int | ||
) |
2175cb9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't able to retrieve PDD puzzles from the code base and submit them to GitHub. If you think that it's a bug on our side, please submit it to yegor256/0pdd:
Please, copy and paste this stack trace to GitHub: