123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- const diff = require('fast-diff');
- const LINE_ENDING_RE = /\r\n|[\r\n\u2028\u2029]/;
- function showInvisibles(str) {
- let ret = '';
- for (let i = 0; i < str.length; i++) {
- switch (str[i]) {
- case ' ':
- ret += '·';
- break;
- case '\n':
- ret += '⏎';
- break;
- case '\t':
- ret += '↹';
- break;
- case '\r':
- ret += '␍';
- break;
- default:
- ret += str[i];
- break;
- }
- }
- return ret;
- }
- function generateDifferences(source, prettierSource) {
-
-
-
-
-
-
-
-
-
-
-
-
-
- const results = diff(source, prettierSource);
- const differences = [];
- const batch = [];
- let offset = 0;
- while (results.length) {
- const result = results.shift();
- const op = result[0];
- const text = result[1];
- switch (op) {
- case diff.INSERT:
- case diff.DELETE:
- batch.push(result);
- break;
- case diff.EQUAL:
- if (results.length) {
- if (batch.length) {
- if (LINE_ENDING_RE.test(text)) {
- flush();
- offset += text.length;
- } else {
- batch.push(result);
- }
- } else {
- offset += text.length;
- }
- }
- break;
- default:
- throw new Error(`Unexpected fast-diff operation "${op}"`);
- }
- if (batch.length && !results.length) {
- flush();
- }
- }
- return differences;
- function flush() {
- let aheadDeleteText = '';
- let aheadInsertText = '';
- while (batch.length) {
- const next = batch.shift();
- const op = next[0];
- const text = next[1];
- switch (op) {
- case diff.INSERT:
- aheadInsertText += text;
- break;
- case diff.DELETE:
- aheadDeleteText += text;
- break;
- case diff.EQUAL:
- aheadDeleteText += text;
- aheadInsertText += text;
- break;
- }
- }
- if (aheadDeleteText && aheadInsertText) {
- differences.push({
- offset,
- operation: generateDifferences.REPLACE,
- insertText: aheadInsertText,
- deleteText: aheadDeleteText,
- });
- } else if (!aheadDeleteText && aheadInsertText) {
- differences.push({
- offset,
- operation: generateDifferences.INSERT,
- insertText: aheadInsertText,
- });
- } else if (aheadDeleteText && !aheadInsertText) {
- differences.push({
- offset,
- operation: generateDifferences.DELETE,
- deleteText: aheadDeleteText,
- });
- }
- offset += aheadDeleteText.length;
- }
- }
- generateDifferences.INSERT = 'insert';
- generateDifferences.DELETE = 'delete';
- generateDifferences.REPLACE = 'replace';
- module.exports = {
- showInvisibles,
- generateDifferences,
- };
|