atom-nuclide
Version:
A unified developer experience for web and mobile development, built as a suite of features on top of Atom to provide hackability and the support of an active community.
83 lines (68 loc) • 2.44 kB
JavaScript
Object.defineProperty(exports, '__esModule', {
value: true
});
/*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
exports.default = applyTextEdits;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _assert2;
function _assert() {
return _assert2 = _interopRequireDefault(require('assert'));
}
var _commonsAtomTextEditor2;
function _commonsAtomTextEditor() {
return _commonsAtomTextEditor2 = require('../../commons-atom/text-editor');
}
/**
* Attempts to apply the given patches to the given file.
*
* The file must be currently open in Atom, and the changes will be applied to the buffer but not
* saved.
*
* Returns true if the application was successful, otherwise false (e.g. if the oldText did not
* match).
*/
function applyTextEdits(path) {
var editor = (0, (_commonsAtomTextEditor2 || _commonsAtomTextEditor()).existingEditorForUri)(path);
(0, (_assert2 || _assert()).default)(editor != null);
var buffer = editor.getBuffer();
var checkpoint = buffer.createCheckpoint();
for (var _len = arguments.length, edits = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
edits[_key - 1] = arguments[_key];
}
for (var edit of edits) {
var success = applyToBuffer(buffer, edit);
if (!success) {
buffer.revertToCheckpoint(checkpoint);
return false;
}
}
buffer.groupChangesSinceCheckpoint(checkpoint);
return true;
}
function applyToBuffer(buffer, edit) {
if (edit.oldRange.start.row === edit.oldRange.end.row) {
// A little extra validation when the old range spans only one line. In particular, this helps
// when the old range is empty so there is no old text for us to compare against. We can at
// least abort if the line isn't long enough.
var lineLength = buffer.lineLengthForRow(edit.oldRange.start.row);
if (edit.oldRange.end.column > lineLength) {
return false;
}
}
if (edit.oldText != null) {
var currentText = buffer.getTextInRange(edit.oldRange);
if (currentText !== edit.oldText) {
return false;
}
}
buffer.setTextInRange(edit.oldRange, edit.newText);
return true;
}
module.exports = exports.default;
// If included, this will be used to verify that the edit still applies cleanly.