MCPcopy
hub / github.com/benjamn/recast / slice

Method slice

lib/lines.ts:506–547  ·  view source on GitHub ↗
(start?: Pos, end?: Pos)

Source from the content-addressed store, hash-verified

504 }
505
506 slice(start?: Pos, end?: Pos) {
507 if (!end) {
508 if (!start) {
509 // The client seems to want a copy of this Lines object, but
510 // Lines objects are immutable, so it's perfectly adequate to
511 // return the same object.
512 return this;
513 }
514
515 // Slice to the end if no end position was provided.
516 end = this.lastPos();
517 }
518
519 if (!start) {
520 throw new Error("cannot slice with end but not start");
521 }
522
523 const sliced = this.infos.slice(start.line - 1, end.line);
524
525 if (start.line === end.line) {
526 sliced[0] = sliceInfo(sliced[0], start.column, end.column);
527 } else {
528 assert.ok(start.line < end.line);
529 sliced[0] = sliceInfo(sliced[0], start.column);
530 sliced.push(sliceInfo(sliced.pop(), 0, end.column));
531 }
532
533 const lines = new Lines(sliced);
534
535 if (this.mappings.length > 0) {
536 const newMappings = lines.mappings;
537 assert.strictEqual(newMappings.length, 0);
538 this.mappings.forEach(function (this: any, mapping: any) {
539 const sliced = mapping.slice(this, start, end);
540 if (sliced) {
541 newMappings.push(sliced);
542 }
543 }, this);
544 }
545
546 return lines;
547 }
548
549 bootstrapSliceString(start: Pos, end: Pos, options?: Options) {
550 return this.slice(start, end).toString(options);

Callers 15

trimLeftMethod · 0.95
trimRightMethod · 0.95
trimMethod · 0.95
bootstrapSliceStringMethod · 0.95
printLeadingCommentFunction · 0.45
printTrailingCommentFunction · 0.45
genericPrintNoParensFunction · 0.45
fast-path.tsFile · 0.45
pushSliceFunction · 0.45
guessTabWidthMethod · 0.45
startsWithCommentMethod · 0.45

Calls 2

lastPosMethod · 0.95
sliceInfoFunction · 0.85

Tested by

no test coverage detected