MCPcopy Create free account
hub / github.com/Effect-TS/effect / fitsSmartLoop

Function fitsSmartLoop

packages/printer/src/internal/layout.ts:328–385  ·  view source on GitHub ↗
(
  self: DocStream.DocStream<A>,
  comparator: LazyArg<DocStream.DocStream<A>>,
  pageWidth: number,
  currentColumn: number,
  availableWidth: number
)

Source from the content-addressed store, hash-verified

326}
327
328const fitsSmartLoop = <A>(
329 self: DocStream.DocStream<A>,
330 comparator: LazyArg<DocStream.DocStream<A>>,
331 pageWidth: number,
332 currentColumn: number,
333 availableWidth: number
334): boolean => {
335 let minNestingLevel: number | undefined
336 let stream = self
337 let w = availableWidth
338 while (w >= 0) {
339 switch (stream._tag) {
340 case "FailedStream": {
341 return false
342 }
343 case "EmptyStream": {
344 return true
345 }
346 case "CharStream": {
347 w = w - 1
348 stream = stream.stream
349 break
350 }
351 case "TextStream": {
352 w = w - stream.text.length
353 stream = stream.stream
354 break
355 }
356 case "LineStream": {
357 if (!minNestingLevel) {
358 minNestingLevel = Option.match(getInitialIndentation(comparator()), {
359 // Definitely not a hanging layout. Return the same `minNestingLevel` that
360 // subsequent lines with the same indentation use
361 onNone: () => currentColumn,
362 // Could be a (less wide) hanging layout, so take the minimum of the indent
363 // and the current column
364 onSome: (value) => Math.min(value, currentColumn)
365 })
366 }
367 if (minNestingLevel < stream.indentation) {
368 return false
369 }
370 w = pageWidth - stream.indentation
371 stream = stream.stream
372 break
373 }
374 case "PushAnnotationStream": {
375 stream = stream.stream
376 break
377 }
378 case "PopAnnotationStream": {
379 stream = stream.stream
380 break
381 }
382 }
383 }
384 return false
385}

Callers 1

fitsSmartFunction · 0.85

Calls 1

getInitialIndentationFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…