Skip to content

Commit

Permalink
DiffMatchPatch for faster diffing (#158)
Browse files Browse the repository at this point in the history
* DiffMatchPatch for faster diffing
  • Loading branch information
platers authored Jan 13, 2022
1 parent 68f8708 commit 77c4f9d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
19 changes: 17 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@rollup/plugin-node-resolve": "^11.2.1",
"@rollup/plugin-typescript": "^8.2.1",
"@types/diff": "^5.0.1",
"@types/diff-match-patch": "^1.0.32",
"@types/jest": "^26.0.24",
"@types/node": "^14.17.9",
"@typescript-eslint/eslint-plugin": "^4.30.0",
Expand All @@ -39,7 +40,7 @@
},
"dependencies": {
"@types/js-yaml": "^4.0.3",
"diff": "^4.0.2",
"diff-match-patch": "^1.0.5",
"js-yaml": "^3.14.1",
"moment": "^2.29.1",
"remark": "^14.0.1",
Expand Down
23 changes: 13 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {App, Editor, EventRef, MarkdownView, Menu, Modal, Notice, Plugin, PluginSettingTab, Setting, TAbstractFile, TFile} from 'obsidian';
import {LinterSettings, Options, rules, getDisabledRules} from './rules';
import Diff from 'diff';
import DiffMatchPatch from 'diff-match-patch';
import moment from 'moment';
import {BooleanOption, DropdownOption, MomentFormatOption, TextAreaOption, TextOption} from './option';
import dedent from 'ts-dedent';
Expand Down Expand Up @@ -179,30 +179,33 @@ export default class LinterPlugin extends Plugin {
const newText = this.lintText(oldText, file);

// Replace changed lines
const changes = Diff.diffChars(oldText, newText);
const dmp = new DiffMatchPatch.diff_match_patch(); // eslint-disable-line new-cap
const changes = dmp.diff_main(oldText, newText);
let curText = '';
changes.forEach((change) => {
function endOfDocument(doc: string) {
const lines = doc.split('\n');
return {line: lines.length - 1, ch: lines[lines.length - 1].length};
}

if (change.added) {
editor.replaceRange(change.value, endOfDocument(curText));
curText += change.value;
} else if (change.removed) {
const [type, value] = change;

if (type == DiffMatchPatch.DIFF_INSERT) {
editor.replaceRange(value, endOfDocument(curText));
curText += value;
} else if (type == DiffMatchPatch.DIFF_DELETE) {
const start = endOfDocument(curText);
let tempText = curText;
tempText += change.value;
tempText += value;
const end = endOfDocument(tempText);
editor.replaceRange('', start, end);
} else {
curText += change.value;
curText += value;
}
});

const charsAdded = changes.map((change) => change.added ? change.value.length : 0).reduce((a, b) => a + b, 0);
const charsRemoved = changes.map((change) => change.removed ? change.value.length : 0).reduce((a, b) => a + b, 0);
const charsAdded = changes.map((change) => change[0] == DiffMatchPatch.DIFF_INSERT ? change[1].length : 0).reduce((a, b) => a + b, 0);
const charsRemoved = changes.map((change) => change[0] == DiffMatchPatch.DIFF_DELETE ? change[1].length : 0).reduce((a, b) => a + b, 0);
this.displayChangedMessage(charsAdded, charsRemoved);
}

Expand Down

0 comments on commit 77c4f9d

Please sign in to comment.