Skip to content

Commit

Permalink
Option to provide custom diff
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeche committed Aug 23, 2022
1 parent 25e4282 commit b8f8e16
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,17 @@ function suppressWhitespace(value: string, pos: number, state: DiffState): boole
}

function getDiff(from: ParsedModel, to: ParsedModel, options: Options): Diff[] {
const dmp = new diff_match_patch();
if (options.dmp) {
Object.assign(dmp, options.dmp);
let diffs: Diff[];
if (options.diff) {
diffs = options.diff(from.content, to.content, options.dmp);
} else {
const dmp = new diff_match_patch();
if (options.dmp) {
Object.assign(dmp, options.dmp);
}
diffs = dmp.diff_main(from.content, to.content);
dmp.diff_cleanupSemantic(diffs);
}
let diffs = dmp.diff_main(from.content, to.content);
dmp.diff_cleanupSemantic(diffs);

if (options.replaceThreshold && getChangeThreshold(diffs) > options.replaceThreshold) {
// Text is too different, mark it as replaced
Expand Down
6 changes: 6 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Diff } from 'diff-match-patch';
import { ScannerOptions, createOptions as scannerOptions } from '@emmetio/html-matcher';

export interface DMPOptions {
Expand Down Expand Up @@ -108,6 +109,11 @@ export interface Options extends ScannerOptions {
* Values vary from 0 to 1, but actual threshold might be larger than 1
*/
replaceThreshold?: number;

/**
* Custom function to perform diff on given content
*/
diff?: (from: string, to: string, opt?: Partial<DMPOptions>) => Diff[];
}

const defaultOptions: Partial<Options> = {
Expand Down

0 comments on commit b8f8e16

Please sign in to comment.