Skip to content

Commit

Permalink
Changed the way how space-only diffs are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeche committed Nov 25, 2021
1 parent c32f5de commit c9c3feb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 45 deletions.
46 changes: 7 additions & 39 deletions src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default function diff(from: ParsedModel, to: ParsedModel, options: Option
}

if (!shouldSkipIns(value, toOffset, state, options)) {
moveSlice(to, toOffset, toOffset + value.length, ins, state);
moveSlice(to, toOffset, toOffset + value.length, options.skipSpace ? '' : ins, state);
}

toOffset += value.length;
Expand Down Expand Up @@ -216,7 +216,12 @@ function shouldSkipIns(value: string, pos: number, state: DiffState, options: Op
* Check if given DELETE patch should be omitted
*/
function shouldSkipDel(content: string, value: string, pos: number, options: Options): boolean {
if (options.compact && isWhitespace(value)) {
const isSpaceDel = isWhitespace(value);
if (options.skipSpace && isSpaceDel) {
return true;
}

if (options.compact && isSpaceDel) {
if (isWhitespace(content.charAt(pos - 1)) || isWhitespace(content.charAt(pos))) {
// There’s whitespace before or after deleted whitespace token
return true;
Expand Down Expand Up @@ -276,10 +281,6 @@ function getDiff(from: ParsedModel, to: ParsedModel, options: Options): Diff[] {
];
}

if (options.skipSpace) {
diffs = skipSpace(diffs);
}

if (options.wordPatches) {
diffs = wordBounds(diffs);
}
Expand Down Expand Up @@ -330,36 +331,3 @@ function getChangeThreshold(diffs: Diff[]): number {

return changed / unchanged;
}

function skipSpace(diffs: Diff[]): Diff[] {
const result: Diff[] = [];
let prev: Diff;

diffs.forEach(chunk => {
let canJoin = false;
if (chunk[0] === DIFF_EQUAL) {
// If previous chunk is also a text (whitespace), join it with previous
canJoin = true;
} else if (isSpace(chunk[1])) {
if (chunk[0] === DIFF_DELETE) {
// Skip deleted space chunk
return;
}

chunk[0] = DIFF_EQUAL;
canJoin = true;
}

if (canJoin && prev && prev[0] === DIFF_EQUAL) {
prev[1] += chunk[1];
} else {
result.push(prev = chunk);
}
});

return result;
}

function isSpace(text: string): boolean {
return /^\s+$/.test(text);
}
15 changes: 9 additions & 6 deletions src/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ export class SliceResult {
for (const t of this.tokens) {
if (typeof t !== 'string') {
if (isSliceMark(t)) {
result.push({
type: ElementTypeAddon.Diff,
location: t.location,
name: opTag,
value: t.close ? `</${opTag}>` : `<${opTag}>`
});
if (opTag) {
// NB: allow empty name to skip operation tag
result.push({
type: ElementTypeAddon.Diff,
location: t.location,
name: opTag,
value: t.close ? `</${opTag}>` : `<${opTag}>`
});
}
} else {
result.push(t);
}
Expand Down
2 changes: 2 additions & 0 deletions test/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ describe('Diff documents', () => {

equal(diff('<p>foobar</p>', '<p>foo bar</p>'), '<p>foo<ins> </ins>bar</p>');
equal(diff('<p>foobar</p>', '<p>foo bar</p>', opt), '<p>foo bar</p>');

equal(diff('<p>foo</p><p>bar</p><p>baz</p>', '<p>foo</p> <p>bar</p> <p>baz</p>', opt), '<p>foo</p> <p>bar</p> <p>baz</p>');
});

it.skip('debug', () => {
Expand Down

0 comments on commit c9c3feb

Please sign in to comment.