MCPcopy Create free account
hub / github.com/Rich-Harris/magic-string / indent

Method indent

benchmark/data.js:391–454  ·  view source on GitHub ↗
(indentStr, options)

Source from the content-addressed store, hash-verified

389 return this.indentStr === null ? ' ' : this.indentStr;
390 }
391 indent(indentStr, options) {
392 const pattern = /^[^\r\n]/gm;
393 if (isObject(indentStr)) {
394 options = indentStr;
395 indentStr = void 0;
396 }
397 indentStr = indentStr !== void 0 ? indentStr : this.indentStr || ' ';
398 if (indentStr === '') return this;
399 options = options || {};
400 const isExcluded = {};
401 if (options.exclude) {
402 const exclusions =
403 typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
404 exclusions.forEach((exclusion) => {
405 for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
406 isExcluded[i] = true;
407 }
408 });
409 }
410 let shouldIndentNextCharacter = options.indentStart !== false;
411 const replacer = (match) => {
412 if (shouldIndentNextCharacter) return `${indentStr}${match}`;
413 shouldIndentNextCharacter = true;
414 return match;
415 };
416 this.intro = this.intro.replace(pattern, replacer);
417 let charIndex = 0;
418 let chunk = this.firstChunk;
419 while (chunk) {
420 const end = chunk.end;
421 if (chunk.edited) {
422 if (!isExcluded[charIndex]) {
423 chunk.content = chunk.content.replace(pattern, replacer);
424 if (chunk.content.length) {
425 shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
426 }
427 }
428 } else {
429 charIndex = chunk.start;
430 while (charIndex < end) {
431 if (!isExcluded[charIndex]) {
432 const char = this.original[charIndex];
433 if (char === '\n') {
434 shouldIndentNextCharacter = true;
435 } else if (char !== '\r' && shouldIndentNextCharacter) {
436 shouldIndentNextCharacter = false;
437 if (charIndex === chunk.start) {
438 chunk.prependRight(indentStr);
439 } else {
440 this._splitChunk(chunk, charIndex);
441 chunk = chunk.next;
442 chunk.prependRight(indentStr);
443 }
444 }
445 }
446 charIndex += 1;
447 }
448 }

Callers

nothing calls this directly

Calls 4

_splitChunkMethod · 0.95
isObjectFunction · 0.70
replaceMethod · 0.45
prependRightMethod · 0.45

Tested by

no test coverage detected