pddl-workspace
Version:
116 lines • 4.65 kB
JavaScript
/*
* Copyright (c) Jan Dolejsi 2023. 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.Compilations = exports.CodeReplacement = exports.VariableDeclarationsInjection = exports.PddlCodeInjection = exports.CodeInjection = void 0;
const expression_1 = require("./expression");
const utils_1 = require("./utils");
class CodeInjection {
constructor(options) {
this.offset = options.offset;
this.code = options.code;
this.documentation = options.documentation;
this.reason = options.reason;
this._shouldEnsureWhitespaceSeparation = !options.doesNotRequireWhitespaceSurrounding;
}
get shouldEnsureWhitespaceSeparation() {
return this._shouldEnsureWhitespaceSeparation;
}
}
exports.CodeInjection = CodeInjection;
class PddlCodeInjection extends CodeInjection {
constructor(options) {
super({
offset: options.offset, code: options.code, documentation: options.documentation,
doesNotRequireWhitespaceSurrounding: options.code.startsWith('(') && options.code.endsWith(')'),
reason: options.reason,
});
}
}
exports.PddlCodeInjection = PddlCodeInjection;
class VariableDeclarationsInjection extends CodeInjection {
constructor(options) {
var _a, _b;
super({
offset: options.offset,
code: ((_a = options.prefix) !== null && _a !== void 0 ? _a : '') +
options.variables.map(v => new expression_1.VariableDeclarationNode(v).toPddlString()).join(' ') +
((_b = options.suffix) !== null && _b !== void 0 ? _b : ''),
documentation: options.documentation,
reason: options.reason,
});
this.variables = options.variables;
}
get shouldEnsureWhitespaceSeparation() {
return false;
}
}
exports.VariableDeclarationsInjection = VariableDeclarationsInjection;
class CodeReplacement {
constructor(options) {
this.offset = options.offset;
this.documentation = options.documentation;
this.code = options.newCode;
this.removedCodeLength = options.origCode.length;
this.reason = options.reason;
}
get shouldEnsureWhitespaceSeparation() {
return false;
}
}
exports.CodeReplacement = CodeReplacement;
/** Container for all code compilation. */
class Compilations {
constructor() {
this.compilations = new Map();
}
getAll() {
return this.compilations;
}
add(compilation) {
(0, utils_1.addToMapOfLists)(this.compilations, compilation.offset, compilation);
}
addAll(index, injections) {
(0, utils_1.addAllToMapOfLists)(this.compilations, index, injections);
}
/**
* Applies all code compilations.
* @param input original code
* @returns resulting code with all the compilations
*/
applyAll(input) {
const output = [...this.compilations.keys()]
.sort((a, b) => a - b)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
.reduceRight((input1, offset) => this.applyAt(input1, offset, this.compilations.get(offset)), input);
return output;
}
/**
* Applies all compilations at `offset` and return the output.
*/
applyAt(input, offset, compilations) {
// we should first apply replacements before we apply injections for the consistency
const replacementsFirst = (0, utils_1.split)(compilations, (c) => c instanceof CodeReplacement);
const output = replacementsFirst.reduce((input1, compilations) => compilations.reduceRight((input2, compilation) => this.applyOne(input2, offset, compilation), input1), input);
return output;
}
applyOne(input, offset, compilation) {
var _a;
let code = compilation.code;
// prepend by space, if there is none
if (compilation.shouldEnsureWhitespaceSeparation
&& offset > 0 && (input.charAt(offset - 1) !== ' ')) {
code = ' ' + code;
}
// append space, if there is none
if (compilation.shouldEnsureWhitespaceSeparation
&& offset < input.length && ![' ', ')'].includes(input.charAt(offset))) {
code = code + ' ';
}
return input.substring(0, offset) + code + input.substring(offset + ((_a = compilation.removedCodeLength) !== null && _a !== void 0 ? _a : 0));
}
}
exports.Compilations = Compilations;
//# sourceMappingURL=Compilations.js.map