How to use the fast-diff.INSERT function in fast-diff

To help you get started, we’ve selected a few fast-diff examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github neoclide / coc.nvim / src / handler / refactor.ts View on Github external
let diff = diffs[i]
        let pos = orig.positionAt(offset)
        if (diff[0] == fastDiff.EQUAL) {
          offset = offset + diff[1].length
        } else if (diff[0] == fastDiff.DELETE) {
          let end = orig.positionAt(offset + diff[1].length)
          if (diffs[i + 1] && diffs[i + 1][0] == fastDiff.INSERT) {
            let delta = diffs[i + 1][1].split('\n').length - (end.line - pos.line) - 1
            if (delta != 0) lineChanges.push({ delta, lnum: pos.line + startLine })
            i = i + 1
          } else {
            let delta = - (end.line - pos.line)
            if (delta != 0) lineChanges.push({ delta, lnum: pos.line + startLine })
          }
          offset = offset + diff[1].length
        } else if (diff[0] == fastDiff.INSERT) {
          let delta = diff[1].split('\n').length - 1
          if (delta != 0) lineChanges.push({ delta, lnum: pos.line + startLine })
        }
      }
    } else {
      lineChanges = [{ delta: lineChange, lnum: range.start.line }]
    }
    let changed = false
    // adjust LineNr highlights
    for (let item of fileItems) {
      for (let range of item.ranges) {
        let arr = lineChanges.filter(o => o.lnum < range.lnum - 1)
        if (arr.length) {
          let total = arr.reduce((p, c) => p + c.delta, 0)
          range.lnum = range.lnum + total
          changed = true
github neoclide / coc.nvim / src / cursors / index.ts View on Github external
private async applyComposedEdit(original: string, edit: TextEdit): Promise {
    // check complex edit
    let { range, newText } = edit
    let { nvim, ranges } = this
    let doc = TextDocument.create('file:///1', '', 0, original)
    let edits: TextEdit[] = []
    let diffs = fastDiff(original, newText)
    let offset = 0
    for (let i = 0; i < diffs.length; i++) {
      let diff = diffs[i]
      let pos = adjustPosition(range.start, doc.positionAt(offset))
      if (diff[0] == fastDiff.EQUAL) {
        offset = offset + diff[1].length
      } else if (diff[0] == fastDiff.DELETE) {
        let end = adjustPosition(range.start, doc.positionAt(offset + diff[1].length))
        if (diffs[i + 1] && diffs[i + 1][0] == fastDiff.INSERT) {
          // change
          edits.push({ range: Range.create(pos, end), newText: diffs[i + 1][1] })
          i = i + 1
        } else {
          // delete
          edits.push({ range: Range.create(pos, end), newText: '' })
        }
        offset = offset + diff[1].length
      } else if (diff[0] == fastDiff.INSERT) {
        edits.push({ range: Range.create(pos, pos), newText: diff[1] })
      }
    }
    if (edits.some(edit => edit.newText.indexOf('\n') != -1 || edit.range.start.line != edit.range.end.line)) {
      this.cancel()
      return
    }
github neoclide / coc.nvim / src / util / diff.ts View on Github external
export function patchLine(from: string, to: string, fill = ' '): string {
  if (from == to) return to
  let idx = to.indexOf(from)
  if (idx !== -1) return fill.repeat(idx) + from
  let result = fastDiff(from, to)
  let str = ''
  for (let item of result) {
    if (item[0] == fastDiff.DELETE) {
      // not allowed
      return to
    } else if (item[0] == fastDiff.INSERT) {
      str = str + fill.repeat(byteLength(item[1]))
    } else {
      str = str + item[1]
    }
  }
  return str
}
github stylelint / vscode-stylelint / server.js View on Github external
function replaceEdits(document, newText) {
	const text = document.getText();

	const results = diff(text, newText);

	const edits = [];
	let offset = 0;

	for (const result of results) {
		const start = offset;
		const op = result[0];
		const text = result[1];

		switch (op) {
			case diff.INSERT:
				edits.push(TextEdit.insert(document.positionAt(start), text));
				break;
			case diff.DELETE:
				offset += text.length;
				edits.push(
					TextEdit.del(Range.create(document.positionAt(start), document.positionAt(offset))),
				);
				break;
			case diff.EQUAL:
				offset += text.length;
				break;
		}
	}

	return edits;
}
github ottypes / rich-text / lib / delta.js View on Github external
diffResult.forEach(function (component) {
    var length = component[1].length;
    while (length > 0) {
      var opLength = 0;
      switch (component[0]) {
        case diff.INSERT:
          opLength = Math.min(otherIter.peekLength(), length);
          delta.push(otherIter.next(opLength));
          break;
        case diff.DELETE:
          opLength = Math.min(length, thisIter.peekLength());
          thisIter.next(opLength);
          delta['delete'](opLength);
          break;
        case diff.EQUAL:
          opLength = Math.min(thisIter.peekLength(), otherIter.peekLength(), length);
          var thisOp = thisIter.next(opLength);
          var otherOp = otherIter.next(opLength);
          if (equal(thisOp.insert, otherOp.insert)) {
            delta.retain(opLength, op.attributes.diff(thisOp.attributes, otherOp.attributes));
          } else {
            delta.push(otherOp)['delete'](opLength);
github webiny / Webiny / Js / Webiny / Assets / node_modules / quill-delta / lib / delta.js View on Github external
diffResult.forEach(function (component) {
    var length = component[1].length;
    while (length > 0) {
      var opLength = 0;
      switch (component[0]) {
        case diff.INSERT:
          opLength = Math.min(otherIter.peekLength(), length);
          delta.push(otherIter.next(opLength));
          break;
        case diff.DELETE:
          opLength = Math.min(length, thisIter.peekLength());
          thisIter.next(opLength);
          delta['delete'](opLength);
          break;
        case diff.EQUAL:
          opLength = Math.min(thisIter.peekLength(), otherIter.peekLength(), length);
          var thisOp = thisIter.next(opLength);
          var otherOp = otherIter.next(opLength);
          if (equal(thisOp.insert, otherOp.insert)) {
            delta.retain(opLength, op.attributes.diff(thisOp.attributes, otherOp.attributes));
          } else {
            delta.push(otherOp)['delete'](opLength);
github concordancejs / concordance / lib / primitiveValues / string.js View on Github external
outcome[0][1] + outcome[2][1] === actual &&
      outcome[1][1] + outcome[2][1] === expected
    )
  )

  let stringActual = ''
  let stringExpected = ''

  const noopWrap = { open: '', close: '' }
  const deleteWrap = isPartiallyEqual ? theme.string.diff.delete : noopWrap
  const insertWrap = isPartiallyEqual ? theme.string.diff.insert : noopWrap
  const equalWrap = isPartiallyEqual ? theme.string.diff.equal : noopWrap
  for (const diff of outcome) {
    if (diff[0] === fastDiff.DELETE) {
      stringActual += formatUtils.wrap(deleteWrap, diff[1])
    } else if (diff[0] === fastDiff.INSERT) {
      stringExpected += formatUtils.wrap(insertWrap, diff[1])
    } else {
      const string = formatUtils.wrap(equalWrap, themeControlPictures(theme, equalWrap, diff[1]))
      stringActual += string
      stringExpected += string
    }
  }

  if (!isPartiallyEqual) {
    stringActual = formatUtils.wrap(theme.string.diff.deleteLine, stringActual)
    stringExpected = formatUtils.wrap(theme.string.diff.insertLine, stringExpected)
  }

  return [stringActual, stringExpected]
}

fast-diff

Fast Javascript text diff

Apache-2.0
Latest version published 1 year ago

Package Health Score

71 / 100
Full package analysis