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

Method sliceString

lib/lines.ts:553–613  ·  view source on GitHub ↗
(
    start: Pos = this.firstPos(),
    end: Pos = this.lastPos(),
    options?: Options,
  )

Source from the content-addressed store, hash-verified

551 }
552
553 sliceString(
554 start: Pos = this.firstPos(),
555 end: Pos = this.lastPos(),
556 options?: Options,
557 ) {
558 const { tabWidth, useTabs, reuseWhitespace, lineTerminator } =
559 normalizeOptions(options);
560
561 const parts = [];
562
563 for (let line = start.line; line <= end.line; ++line) {
564 let info = this.infos[line - 1];
565
566 if (line === start.line) {
567 if (line === end.line) {
568 info = sliceInfo(info, start.column, end.column);
569 } else {
570 info = sliceInfo(info, start.column);
571 }
572 } else if (line === end.line) {
573 info = sliceInfo(info, 0, end.column);
574 }
575
576 const indent = Math.max(info.indent, 0);
577
578 const before = info.line.slice(0, info.sliceStart);
579 if (
580 reuseWhitespace &&
581 isOnlyWhitespace(before) &&
582 countSpaces(before, tabWidth) === indent
583 ) {
584 // Reuse original spaces if the indentation is correct.
585 parts.push(info.line.slice(0, info.sliceEnd));
586 continue;
587 }
588
589 let tabs = 0;
590 let spaces = indent;
591
592 if (useTabs) {
593 tabs = Math.floor(indent / tabWidth);
594 spaces -= tabs * tabWidth;
595 }
596
597 let result = "";
598
599 if (tabs > 0) {
600 result += new Array(tabs + 1).join("\t");
601 }
602
603 if (spaces > 0) {
604 result += new Array(spaces + 1).join(" ");
605 }
606
607 result += info.line.slice(info.sliceStart, info.sliceEnd);
608
609 parts.push(result);
610 }

Callers 5

toStringMethod · 0.95
parseFunction · 0.80
breakTiesFunction · 0.80
genericPrintNoParensFunction · 0.80
checkAllSlicesFunction · 0.80

Calls 7

firstPosMethod · 0.95
lastPosMethod · 0.95
sliceInfoFunction · 0.85
isOnlyWhitespaceFunction · 0.85
countSpacesFunction · 0.85
joinMethod · 0.80
sliceMethod · 0.45

Tested by

no test coverage detected