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

Method indent

src/MagicString.ts:320–443  ·  view source on GitHub ↗
(indentStr?: string | IndentOptions, options?: IndentOptions)

Source from the content-addressed store, hash-verified

318 */
319 indent(indentStr?: string, options?: IndentOptions): this
320 indent(indentStr?: string | IndentOptions, options?: IndentOptions): this {
321 const pattern = /^[^\r\n]/gm
322
323 if (isObject(indentStr)) {
324 options = indentStr
325 indentStr = undefined
326 }
327
328 if (indentStr === undefined) {
329 this._ensureindentStr()
330 indentStr = this.indentStr || '\t'
331 }
332
333 if (indentStr === '')
334 return this // noop
335 const resolvedIndentStr = indentStr as string
336
337 options = options || {}
338
339 // Process exclusion ranges
340 const isExcluded: Record<number, boolean> = {}
341
342 if (options.exclude) {
343 const exclusions
344 = typeof options.exclude[0] === 'number'
345 ? [options.exclude as ExclusionRange]
346 : (options.exclude as ExclusionRange[])
347 exclusions.forEach((exclusion) => {
348 for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
349 isExcluded[i] = true
350 }
351 })
352 }
353
354 let shouldIndentNextCharacter = options.indentStart !== false
355 const replacer = (match: string) => {
356 if (shouldIndentNextCharacter)
357 return `${resolvedIndentStr}${match}`
358 shouldIndentNextCharacter = true
359 return match
360 }
361
362 this.intro = this.intro.replace(pattern, replacer)
363
364 let charIndex = 0
365 let chunk = this.firstChunk
366
367 const indentAt = (index: number) => {
368 shouldIndentNextCharacter = false
369
370 if (index === chunk!.start) {
371 chunk!.prependRight(resolvedIndentStr)
372 }
373 else {
374 this._splitChunk(chunk!, index)
375 chunk = chunk!.next
376 chunk!.prependRight(resolvedIndentStr)
377 }

Callers

nothing calls this directly

Calls 3

_ensureindentStrMethod · 0.95
isObjectFunction · 0.50
replaceMethod · 0.45

Tested by

no test coverage detected