@unisnips/ultisnips
Version:
Utilities for converting ultisnips in unisnips project
363 lines • 12.1 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var position_1 = require("../util/position");
var util_1 = require("../util/util");
var tokenizer_1 = require("../parse/tokenizer");
/**
* Represents any object in the text that has a span in any ways.
*/
var Marker = /** @class */ (function () {
function Marker(opts) {
this.tabStops = {};
/**
* useful in nested tabstops,
* e.g. '${11:good}', innerContentOffset should be '${12:'.length
*/
this.innerContentOffset = 0;
this.children = [];
var parent = opts.parent, start = opts.start, end = opts.end, token = opts.token, tieBreaker = opts.tieBreaker;
if (token) {
// Initialize from token
this.start = token.start;
this.end = token.end;
this.initialText = token.initialText;
this.token = token;
}
else {
this.start = start;
this.end = end;
this.initialText = opts.initialText;
}
if (tieBreaker) {
this.tieBreaker = tieBreaker;
}
else {
if (this.start) {
if ('line' in this.start) {
// QUESTION: 两个 line 是不是有问题?
this.tieBreaker = new position_1.TextPosition(this.start.line, this.end.line);
}
}
}
if (parent) {
this.parent = parent;
parent.addChild(this);
}
this.init(opts);
}
Marker.prototype.addChild = function (child) {
this.children.push(child);
};
Object.defineProperty(Marker.prototype, "markerType", {
/**
* will be serialized to TokenNode
*/
get: function () {
return 'Marker';
},
enumerable: true,
configurable: true
});
Marker.prototype.getTokenNodeData = function () {
if (this.token) {
return this.token.getTokenNodeData();
}
return {};
};
Marker.prototype.toTokenNode = function () {
return {
type: this.markerType,
position: {
start: this.start.toUnistPosition(),
end: this.end.toUnistPosition(),
},
data: this.getTokenNodeData(),
};
};
/** @abstract */
Marker.prototype.init = function (opts) {
// should be overrided
};
return Marker;
}());
exports.Marker = Marker;
/**
* This base class represents any object in the text that can be changed by
* the user.
*/
var EditableMarker = /** @class */ (function (_super) {
__extends(EditableMarker, _super);
function EditableMarker() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(EditableMarker.prototype, "editableChildren", {
get: function () {
return this.children.filter(function (child) { return child instanceof EditableMarker; });
},
enumerable: true,
configurable: true
});
return EditableMarker;
}(Marker));
var NoneditableMarker = /** @class */ (function (_super) {
__extends(NoneditableMarker, _super);
function NoneditableMarker() {
return _super !== null && _super.apply(this, arguments) || this;
}
return NoneditableMarker;
}(Marker));
var Transform = /** @class */ (function (_super) {
__extends(Transform, _super);
function Transform() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(Transform.prototype, "markerType", {
get: function () {
return 'Transform';
},
enumerable: true,
configurable: true
});
Transform.prototype.getTokenNodeData = function () {
return util_1.pick(this, ['search', 'replace', 'options']);
};
Transform.prototype.initTransformation = function (opts) {
this.options = opts.options;
this.search = opts.search;
if (opts.options) {
}
this.regex = new RegExp(opts.search, opts.options);
// curently more clever replace like '\u' is not supported
this.replace = opts.replace;
};
Transform.prototype.transformText = function (text) {
if (!this.regex) {
return text;
}
text.replace(this.regex, this.replace);
};
return Transform;
}(Marker));
exports.Transform = Transform;
var TransformableMarker = /** @class */ (function (_super) {
__extends(TransformableMarker, _super);
function TransformableMarker() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.options = null;
return _this;
}
TransformableMarker.prototype.getTokenNodeData = function () {
var data = _super.prototype.getTokenNodeData.call(this);
if (this.transform) {
data.transform = this.transform.getTokenNodeData();
}
return data;
};
TransformableMarker.prototype.parseTransform = function (text) {
if (text[0] !== '/')
return;
// console.log('parse transform', text)
var iter = new tokenizer_1.TextIterator(text, new position_1.TextPosition(0, 0, 0));
iter.next();
try {
var search = tokenizer_1.parseTillUnescapedChar(iter, '/')[0];
var replace = tokenizer_1.parseTillUnescapedChar(iter, '/')[0];
var optionChars = [];
if (iter.peek()) {
optionChars.push(iter.next());
}
var options = optionChars.join('');
if (search && replace) {
var transform = new Transform({
parent: this,
});
transform.initTransformation({ search: search, replace: replace, options: options });
this.transform = transform;
}
}
catch (e) {
if (e instanceof tokenizer_1.StopIteration) {
return;
}
throw e;
}
};
return TransformableMarker;
}(EditableMarker));
exports.TransformableMarker = TransformableMarker;
var TabStop = /** @class */ (function (_super) {
__extends(TabStop, _super);
function TabStop() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(TabStop.prototype, "markerType", {
get: function () {
return 'TabStop';
},
enumerable: true,
configurable: true
});
TabStop.prototype.init = function (opts) {
_super.prototype.init.call(this, opts);
var token = opts.token, parent = opts.parent;
if (token) {
this.number = token.number;
}
this.innerContentOffset = ("${" + this.number.toString() + ":").length;
parent.tabStops[this.number] = this;
if (this.initialText && !token.hasColon) {
this.parseTransform(this.initialText);
if (this.transform) {
this.initialText = '';
}
}
};
return TabStop;
}(TransformableMarker));
exports.TabStop = TabStop;
var SnippetInstance = /** @class */ (function (_super) {
__extends(SnippetInstance, _super);
function SnippetInstance() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(SnippetInstance.prototype, "markerType", {
get: function () {
return 'Snippet';
},
enumerable: true,
configurable: true
});
SnippetInstance.prototype.init = function (opts) {
_super.prototype.init.call(this, opts);
this.start = opts.start || new position_1.TextPosition(0, 0);
this.end = opts.end || new position_1.TextPosition(0, 0);
};
return SnippetInstance;
}(EditableMarker));
exports.SnippetInstance = SnippetInstance;
var Mirror = /** @class */ (function (_super) {
__extends(Mirror, _super);
function Mirror() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(Mirror.prototype, "markerType", {
get: function () {
return 'Mirror';
},
enumerable: true,
configurable: true
});
Mirror.prototype.init = function (opts) {
_super.prototype.init.call(this, opts);
};
return Mirror;
}(Marker));
exports.Mirror = Mirror;
var UniSnipsVariable = /** @class */ (function (_super) {
__extends(UniSnipsVariable, _super);
function UniSnipsVariable() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(UniSnipsVariable.prototype, "name", {
get: function () {
if (this.token instanceof tokenizer_1.UniSnipsVariableToken) {
return this.token.name;
}
},
enumerable: true,
configurable: true
});
return UniSnipsVariable;
}(NoneditableMarker));
exports.UniSnipsVariable = UniSnipsVariable;
/**
* A ${VISUAL} placeholder that will use the text that was last visually
* selected and insert it here.
*
* If there was no text visually selected, this will be the empty string.
*/
var Visual = /** @class */ (function (_super) {
__extends(Visual, _super);
function Visual() {
return _super !== null && _super.apply(this, arguments) || this;
}
Visual.prototype.init = function (opts) {
var marker = opts.parent;
while (marker) {
if (marker instanceof SnippetInstance) {
// this.text = marker.
break;
}
else {
marker = marker.parent;
}
if (!this.text) {
var token = opts.token;
this.text = token.alternativeText;
this.mode = 'v';
}
}
// this.
// Transformation
};
Object.defineProperty(Visual.prototype, "markerType", {
get: function () {
return 'Visual';
},
enumerable: true,
configurable: true
});
return Visual;
}(TransformableMarker));
exports.Visual = Visual;
var ScriptCode = /** @class */ (function (_super) {
__extends(ScriptCode, _super);
function ScriptCode() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(ScriptCode.prototype, "code", {
get: function () {
var code;
if (this.token && this.token instanceof tokenizer_1.ScriptCodeToken) {
code = this.token.scriptCode;
}
return code;
},
enumerable: true,
configurable: true
});
Object.defineProperty(ScriptCode.prototype, "scriptType", {
get: function () {
var type;
if (this.token && this.token instanceof tokenizer_1.ScriptCodeToken) {
type = this.token.scriptType;
}
return type;
},
enumerable: true,
configurable: true
});
Object.defineProperty(ScriptCode.prototype, "markerType", {
get: function () {
return 'ScriptCode';
},
enumerable: true,
configurable: true
});
return ScriptCode;
}(NoneditableMarker));
exports.ScriptCode = ScriptCode;
//# sourceMappingURL=index.js.map