MyNixOS website logo
Description

Map file locations across diffs.

See DiffLoc.

diff-loc: Map file locations across diffs Hackage

Example

You have a diff between two versions of a file. Given a source span in one version, find the corresponding span in the other version.

For example, here is a diff d between a source string "abcdefgh" and a target string "appcfgzzh", with deletions and insertions in the middle:

  ab cdefg  h
-  b  de
+  pp     zz
  appc  fgzzh

Diffs are represented by the type Diff. Only locations and lengths are recorded, not the actual characters.

import DiffLoc
import DiffLoc.Unsafe (offset)

d :: Diff N
d = addDiff (Replace 1 (offset 1) (offset 2))  -- at location 1, replace "b" (length 1) with "pp" (length 2)
  $ addDiff (Replace 3 (offset 2) (offset 0))  -- at location 3, replace "de" with ""
  $ addDiff (Replace 7 (offset 0) (offset 2))  -- at location 7, replace "" with "zz"
  $ emptyDiff
-- N.B.: replacements should be inserted right to left, starting from 'emptyDiff'.

The span s of "fg" in the first string is an interval that starts at location 5 and has length 2.

s :: Interval N
s = 5 :.. offset 2

Illustration of the span:

 a b c d e f g h
0 1 2 3 4 5 6 7 8
          ^f+g+ length 2
          ^
          start 5

After applying the diff, the span has been shifted to location 4.

>>> mapDiff d (5 :.. offset 2)
Just (4 :.. offset 2)
 a p p c f g q q h
0 1 2 3 4 5 6 7 8 9
        ^f+g+ length 2
        ^
        start 4

Conversely, we can map spans from the target string to the source string of the diff:

>>> comapDiff d (4 :.. offset 2)
Just (5 :.. offset 2)

If part of the input span is modified by the diff, there is no corresponding output span.

>>> mapDiff d (1 :.. offset 2)  -- "bc" contains "b" which is edited by the diff
Nothing

See the API documentation in DiffLoc.

Metadata

Version

0.1.0.0

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows