UNPKG

@bscotch/stitch

Version:

Stitch: The GameMaker Studio 2 Asset Pipeline Development Kit.

72 lines 2.54 kB
import { assert } from '../../utility/errors.js'; import path from '../../utility/paths.js'; export class GmlTokenLocation { position; line; column; subresource; _resource; constructor(position, line, column, subresource) { this.position = position; this.line = line; this.column = column; this.subresource = subresource; } set resource(resource) { this._resource = resource; } get resource() { return this._resource; } get filename() { assert(this.resource, 'Location is not associated witha resource'); return `${this.subresource || this.resource.name}.gml`; } get filepathRelative() { return path.join(this.resource.yyDirRelative, this.filename); } get filepathAbsolute() { return path.join(this.resource.yyDirAbsolute, this.filename); } /** * Whether this token exactly matches another token * by location, including associated resources. * Will return false if either token does not have an associated resource. */ isSameLocation(otherLocation) { return (this.isSamePosition(otherLocation) && this.isFromSameResource(otherLocation)); } /** * Whether or not the token's position data (position, column, line) * are the same as another token *ignoring whether they come from the same resource*. * This is useful for tokens created externally that do not have an associated * Gms2Resource. */ isSamePosition(otherLocation) { return ['position', 'column', 'line'].every((posField) => { return this[posField] == otherLocation[posField]; }); } isFromSameResource(otherLocation) { return (this.resource && otherLocation.resource && this.resource.name == otherLocation.resource.name && this.subresource == otherLocation.subresource); } toJSON() { return { position: this.position, line: this.line, column: this.column, resource: this.resource, subresource: this.subresource, }; } static createFromMatch(source, match, options) { const position = match.index + (options?.offsetPosition || 0); const lines = source.slice(0, position).split(/\r?\n/g); return new GmlTokenLocation(position, lines.length - 1, lines[lines.length - 1].length, options?.sublocation); } } //# sourceMappingURL=GmlTokenLocation.js.map