Remove overlaps before applying replacements

This commit is contained in:
Ng Zhi An 2018-11-07 22:06:43 -08:00
parent bc111141f8
commit 408a3b99d8
1 changed files with 16 additions and 2 deletions

View File

@ -6,12 +6,26 @@ import Data.List
applyFix fix fileLines =
-- apply replacements in sorted order by end position
-- assert no overlaps, or maybe remove overlaps?
let sorted = (reverse . sort) (fixReplacements fix) in
let sorted = (removeOverlap . reverse . sort) (fixReplacements fix) in
applyReplacement sorted fileLines
where
applyReplacement [] s = s
applyReplacement (rep:xs) s = applyReplacement xs $ replaceMultiLines rep s
-- prereq: list is already sorted by start position
removeOverlap [] = []
removeOverlap (x:xs) = checkoverlap x xs
checkoverlap :: Replacement -> [Replacement] -> [Replacement]
checkoverlap x [] = x:[]
checkoverlap x (y:ys) =
if overlap x y then x:(removeOverlap ys) else x:y:(removeOverlap ys)
-- two position overlaps when
overlap x y =
(yStart >= xStart && yStart <= xEnd) || (yStart < xStart && yStart > xStart)
where
yStart = repStartPos y
xStart = repStartPos x
xEnd = repEndPos x
-- A replacement that spans multiple line is applied by:
-- 1. merging the affected lines into a single string using `unlines`