pddl-workspace
Version:
91 lines • 4.81 kB
JavaScript
;
/* --------------------------------------------------------------------------------------------
* Copyright (c) Jan Dolejsi. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SyntaxInjectors = exports.RelativePosition = exports.InjectionPosition = exports.BaseSyntaxInjector = void 0;
const DocumentPositionResolver_1 = require("../DocumentPositionResolver");
class BaseSyntaxInjector {
/**
* Resolves the relative position within/around the `referenceNode`
* @param referenceNode syntax tree node around which the code should be injected
* @param positionResolver document position resolver
* @param options relative position around the reference node
* @returns offset in the document
*/
getOffset(referenceNode, positionResolver, options) {
if (referenceNode) {
switch (options.position) {
case InjectionPosition.OutsideStart: {
const range = positionResolver.nodeToRange(referenceNode);
const relativePosition = RelativePosition.Before;
return this.getOffsetRelativeTo(range, relativePosition, positionResolver);
}
case InjectionPosition.InsideStart: {
const range = new DocumentPositionResolver_1.PddlRange({
start: positionResolver.resolveToPosition(referenceNode.getStart()),
end: positionResolver.resolveToPosition(referenceNode.getToken().getEnd()),
});
const relativePosition = RelativePosition.After;
return this.getOffsetRelativeTo(range, relativePosition, positionResolver);
}
case InjectionPosition.InsideEnd: {
const endPosition = positionResolver.resolveToPosition(referenceNode.getEnd() - 1);
const range = DocumentPositionResolver_1.PddlRange.createSingleCharacterRange(endPosition);
const relativePosition = RelativePosition.Before;
return this.getOffsetRelativeTo(range, relativePosition, positionResolver);
}
case InjectionPosition.OutsideEnd: {
const range = positionResolver.nodeToRange(referenceNode);
const relativePosition = RelativePosition.After;
return this.getOffsetRelativeTo(range, relativePosition, positionResolver);
}
default:
throw new Error('Unexpected position: ' + options.position);
}
}
return -1;
}
getOffsetRelativeTo(range, relativePosition, positionResolver) {
switch (relativePosition) {
case RelativePosition.Before:
return positionResolver.resolveToOffset(range.start);
case RelativePosition.After:
return positionResolver.resolveToOffset(range.end);
default:
throw new Error('Unexpected relative position: ' + relativePosition);
}
}
}
exports.BaseSyntaxInjector = BaseSyntaxInjector;
var InjectionPosition;
(function (InjectionPosition) {
/** Insert in front of the reference node */
InjectionPosition[InjectionPosition["OutsideStart"] = 0] = "OutsideStart";
/** Insert inside the reference token - just after the opening token, e.g. after `:types` */
InjectionPosition[InjectionPosition["InsideStart"] = 1] = "InsideStart";
/** Insert in front of the closing bracket. */
InjectionPosition[InjectionPosition["InsideEnd"] = 2] = "InsideEnd";
/** Insert after the closing bracket of the reference ndoe. */
InjectionPosition[InjectionPosition["OutsideEnd"] = 3] = "OutsideEnd";
})(InjectionPosition || (exports.InjectionPosition = InjectionPosition = {}));
var RelativePosition;
(function (RelativePosition) {
RelativePosition[RelativePosition["Before"] = 0] = "Before";
RelativePosition[RelativePosition["After"] = 1] = "After";
})(RelativePosition || (exports.RelativePosition = RelativePosition = {}));
/** Ordered list of syntax injectors. They are processed in the order of insertion into this list. */
class SyntaxInjectors {
constructor(injectors = []) {
this.injectors = injectors;
}
process(domainInfo, positionResolver) {
this.injectors.forEach(i => i.process(domainInfo, positionResolver));
}
add(injector) {
this.injectors.push(injector);
}
}
exports.SyntaxInjectors = SyntaxInjectors;
//# sourceMappingURL=SyntaxInjector.js.map