Skip to content

Instantly share code, notes, and snippets.

@wz1000
Last active January 6, 2021 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wz1000/cf71f931893a441ca005c148c8ce1afd to your computer and use it in GitHub Desktop.
Save wz1000/cf71f931893a441ca005c148c8ce1afd to your computer and use it in GitHub Desktop.
mappingFromDiff :: BS.ByteString -> BS.ByteString -> PositionMapping
mappingFromDiff (BS.lines -> old) (BS.lines -> new) = PositionMapping (PositionDelta (lookupPos lnew old2new) (lookupPos lold new2old))
where
diff = getDiff old new
(!old2new, !new2old) = go diff 0 0
lnew = lines new
lold = lines old
lookupPos :: [(Int,Maybe Int)] -> Position -> PositionResult Position
lookupPos max xs (Position line col) = go 0 xs
where
go prev [] = PositionRange (Position (prev+1) 0) (Position max 0)
go prev ((a,b):xs) = _
go [] _ _ = ([],[])
go (Both _ _ : xs) lold lnew = bimap ((lold,Just lnew) :) ((lnew,Just lold) :) $ go xs (lold+1) (lnew+1)
go (First _ : xs) lold lnew = first ((lold,Nothing) :) $ go xs (lold+1) lnew
go (Second _ : xs) lold lnew = second ((lnew,Nothing) :) $ go xs lold (lnew+1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment