diffcontext

package module
v0.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2024 License: GPL-3.0 Imports: 5 Imported by: 0

README

go-diffcontext

Based on sergi/go-diff

Transform from

// Diff represents one diff operation
type Diff struct {
	Type Operation
	Text string
}

To

type DiffLine struct {
	Before []byte
	After  []byte
	State  diffmatchpatch.Operation
}

State

There were 3 so called Operations in sergi/go-diff which are:

const (
// DiffDelete item represents a delete diff.
DiffDelete Operation = -1
// DiffInsert item represents an insert diff.
DiffInsert Operation = 1
// DiffEqual item represents an equal diff.
DiffEqual Operation = 0

But in here we added one more state to clarify line change and slice of content change inside a line

  • if a line is removed or inserted, the DiffLine will be marked as diffmatchpatch.DiffDelete or diffmatchpatch.DiffInsert (package diffmatchpatch is from sergi/go-diff)
  • if slice of string in one line changed, the DiffLine will be marked as diffline.DiffChanged (package diffline is from this repo), and both line content before change and line content after change are in DiffLine.Before and DiffLine.After

Available funcs

Able to get both content before and after change by GetBefore and GetAfter

Also able to get mixed diff content with GetMixed

dc.GetMixed():
 package main

 import (
 	"fmt"

 	"github.com/sergi/go-diff/diffmatchpatch"
 )

 const (
 	text1 = "Lorem ipsum dolor."
-	text2 = "Lorem dolor sit amet."
+	text2 = "Lorem sit amet."
 )

 func main() {
-	dmp := diffmatchpatch.New()
-
 	diffs := dmp.DiffMain(text1, text2, false)

+	dmp := diffmatchpatch.New()
+
 	fmt.Println(dmp.DiffPrettyText(diffs))
 }

Example

import (
	"fmt"
	"time"

	"github.com/ogios/go-diffcontext/diffline"
	"github.com/sergi/go-diff/diffmatchpatch"
)

const code1 = `
const (
	text1 = "Lorem ipsum dolor."
	text2 = "Lorem dolor sit amet."
)
`

const code2 = `
const (
	text2 = "Lorem sit amet."
)
`
package main


func main() {
	dmp := diffmatchpatch.New()

	diffs := dmp.DiffMain(code1, code2, true)
	diffs = dmp.DiffCleanupSemantic(diffs)
	diffs = dmp.DiffCleanupSemantic(diffs)

	dc := diffline.New()
	dc.AddDiffs(diffs)
	fmt.Printf("dc.Lines: %v\n", dc.Lines)
	fmt.Printf("dc.GetBefore(): %v\n", dc.GetBefore())
	fmt.Printf("dc.GetAfter(): %v\n", dc.GetAfter())
	fmt.Printf("dc.GetMixed(): %v\n", dc.GetMixed())
}

Documentation

Index

Constants

View Source
const DiffChanged diffmatchpatch.Operation = 2

Variables

This section is empty.

Functions

func JoinMixedLines added in v0.3.2

func JoinMixedLines(mixedLines []*MixedLine) string

Types

type DiffConstractor

type DiffConstractor struct {
	Lines []*DiffLine
}

func New

func New() *DiffConstractor

func (*DiffConstractor) AddDiffs

func (d *DiffConstractor) AddDiffs(ds []diffmatchpatch.Diff)

func (*DiffConstractor) GetAfter

func (d *DiffConstractor) GetAfter() string

func (*DiffConstractor) GetBefore

func (d *DiffConstractor) GetBefore() string

func (*DiffConstractor) GetMixed

func (d *DiffConstractor) GetMixed() string

func (*DiffConstractor) GetMixedLines

func (d *DiffConstractor) GetMixedLines() []*MixedLine

func (*DiffConstractor) GetMixedLinesAndStateRecord added in v0.3.2

func (d *DiffConstractor) GetMixedLinesAndStateRecord() ([]*MixedLine, [][3]int)

type DiffLine

type DiffLine struct {
	Before []byte
	After  []byte
	State  diffmatchpatch.Operation
}

type MixedLine

type MixedLine struct {
	Data  string
	State diffmatchpatch.Operation
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL