Skip to content

api7/gotextdiff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gotextdiff - unified text diffing in Go

This is a copy of the Go text diffing packages that the official Go language server gopls uses internally to generate unified diffs.

If you've previously tried to generate unified text diffs in Go (like the ones you see in Git and on GitHub), you may have found github.com/sergi/go-diff which is a Go port of Neil Fraser's google-diff-match-patch code - however it does not support unified diffs.

This is arguably one of the best (and most maintained) unified text diffing packages in Go as of at least 2020.

(All credit goes to the Go authors, We are merely re-publishing their work so others can use it.)

Example usage

Assuming you want to diff a.txt and b.txt, whose contents are stored in aString and bString then:

import (
    "fmt"

    "github.com/api7/gotextdiff"
    "github.com/api7/gotextdiff/myers"
)

aString := `foo`
bString := `bar`
edits := myers.ComputeEdits(span.URIFromPath("a.txt"), aString, bString)

fmt.Println(gotextdiff.ToUnified("a.txt", "b.txt", aString, edits))

the output will be a string like:

--- a.txt
+++ b.txt
@@ -1,13 +1,28 @@
-foo
+bar

About

Unified text diffing in Go (copy of the internal diffing packages the officlal Go language server uses)

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages