pddl-workspace
Version:
128 lines • 5.61 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.SimpleDocumentPositionResolver = exports.DocumentPositionResolver = exports.PddlRange = exports.PddlPosition = void 0;
class PddlPosition {
constructor(line, character) {
this.line = line;
this.character = character;
}
atOrBefore(other) {
if (this.line === other.line) {
return this.character <= other.character;
}
else {
return this.line < other.line;
}
}
toString() {
return `${this.line}:${this.character}`;
}
}
exports.PddlPosition = PddlPosition;
/**
* This is a local version of the vscode Range class, but because the parser is used in both the extension (client)
* and the language server, where the Range class is defined separately, we need a single proprietary implementation,
* which is converted to the VS Code class specific to the two distinct client/server environment.
*/
class PddlRange {
constructor(details) {
this._start = details.start;
this._end = details.end;
}
static createRange(details) {
return new PddlRange({
start: new PddlPosition(details.startLine, details.startCharacter),
end: new PddlPosition(details.endLine, details.endCharacter)
});
}
static createSingleCharacterRange(details) {
const position = new PddlPosition(details.line, details.character);
return new PddlRange({ start: position, end: position });
}
static createSingleLineRange(details) {
var _a, _b, _c;
if (((_a = details.end) !== null && _a !== void 0 ? _a : details.length) === undefined) {
throw new Error("Either 'end' or 'length' must be specified.");
}
const start = new PddlPosition(details.line, details.start);
const end = new PddlPosition(details.line, (_b = details.end) !== null && _b !== void 0 ? _b : details.start + ((_c = details.length) !== null && _c !== void 0 ? _c : 0));
return new PddlRange({ start, end });
}
static createFullLineRange(line) {
return new PddlRange({ start: new PddlPosition(line, 0), end: new PddlPosition(line, Number.MAX_VALUE) });
}
static createUnknown() {
return this.createFullLineRange(Number.NaN);
}
includes(positionAtOffset) {
return this.start.atOrBefore(positionAtOffset) && positionAtOffset.atOrBefore(this.end);
}
get start() {
return this._start;
}
get end() {
return this._end;
}
toString() {
return `${this.start.toString()}~${this.end.toString()}`;
}
}
exports.PddlRange = PddlRange;
/**
* Abstract document position resolve. It translates document text offsets to Position or Range.
*/
class DocumentPositionResolver {
resolveToRange(start, end) {
return new PddlRange({ start: this.resolveToPosition(start), end: this.resolveToPosition(end) });
}
rangeIncludesOffset(range, offset) {
const positionAtOffset = this.resolveToPosition(offset);
return range.includes(positionAtOffset);
}
nodeToRange(node) {
return this.resolveToRange(node.getStart(), node.getEnd());
}
}
exports.DocumentPositionResolver = DocumentPositionResolver;
class SimpleDocumentPositionResolver extends DocumentPositionResolver {
constructor(documentText) {
super();
this.documentText = documentText;
this.lineLengths = this.documentText.split('\n')
.map(line => line.length + 1);
}
resolveToOffset(position) {
if (position.line >= this.lineLengths.length) {
throw new Error(`Position line ${position.line} exceeds the document length (${this.lineLengths.length - 1} lines)`);
}
if (position.character >= this.lineLengths[position.line]) {
throw new Error(`Position character ${position.character} exceeds the line ${position.line} length (${this.lineLengths[position.line]} characters)`);
}
let documentLengthAtCurrentLineEnd = 0;
for (let lineIndex = 0; lineIndex < position.line; lineIndex++) {
const currentLineLength = this.lineLengths[lineIndex];
documentLengthAtCurrentLineEnd += currentLineLength;
}
return documentLengthAtCurrentLineEnd + position.character;
}
resolveToPosition(offset) {
let documentLengthAtCurrentLineStart = 0;
let documentLengthAtCurrentLineEnd = 0;
for (let lineIndex = 0; lineIndex < this.lineLengths.length; lineIndex++) {
const currentLineLength = this.lineLengths[lineIndex];
documentLengthAtCurrentLineEnd += currentLineLength;
if (offset >= documentLengthAtCurrentLineStart && offset < documentLengthAtCurrentLineEnd) {
const character = offset - documentLengthAtCurrentLineStart;
return new PddlPosition(lineIndex, character);
}
documentLengthAtCurrentLineStart = documentLengthAtCurrentLineEnd;
}
throw new Error(`Offset ${offset} is outside the document.`);
}
}
exports.SimpleDocumentPositionResolver = SimpleDocumentPositionResolver;
//# sourceMappingURL=DocumentPositionResolver.js.map