fun computeDiff()

in src/main/kotlin/org/jetbrains/qodana/DiffLocator.kt [81:136]


        fun computeDiff(original: List<String>, revised: List<String>, lines: List<Int>): List<DiffResult> {
            val patch = DiffUtils.diff(original.map { it.replace(" ","") }, revised.map { it.replace(" ","") })

            val mapping = mutableListOf<Int?>()
            for (i in original.indices) {
                mapping.add(i, i)
            }

            val deltas = patch.deltas
            // Process the deltas starting from the last one
            deltas.reverse()

            for (delta in deltas) {
                val oldLinePosition: Int = delta.source.position
                val oldLineCount: Int = delta.source.size()
                when (delta.type!!) {
                    DeltaType.DELETE -> {
                        // If lines were deleted, we remove them from the mapping
                        for (i in 0..<oldLineCount) {
                            mapping.removeAt(oldLinePosition)
                        }
                    }

                    DeltaType.INSERT -> {
                        // If lines were inserted, we add placeholders in the mapping
                        val lineCountInRevised: Int = delta.target.size()
                        for (i in 0..<lineCountInRevised) {
                            mapping.add(oldLinePosition + i, null)
                        }
                    }

                    DeltaType.CHANGE -> {
                        // If lines were changed, we need to consider the number of new lines added
                        val deltaCount = delta.target.size() - oldLineCount
                        // If lines have been added in the change
                        if (deltaCount > 0) {
                            for (i in 0..<deltaCount) {
                                mapping.add(oldLinePosition + i, null)
                            }
                        }
                    }
                    DeltaType.EQUAL -> { }
                }
            }

            val ret = mutableListOf<DiffResult>()
            for (line in lines) {
                val idx = mapping.indexOf(line)
                if (idx == -1) {
                    ret.add(DiffResult(line, -1, DiffStatus.DELETE))
                } else {
                    ret.add(DiffResult(line, idx, DiffStatus.CHANGE))
                }
            }
            return ret
        }