All files / utils FormatUtils.ts

100% Statements 14/14
100% Branches 7/7
100% Functions 9/9
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91                14x                   2544x                     5x 5x   8x                       206x 206x   209x                       14x                       5x 2x   3x                     112x          
/**
 * Code formatting utilities for C code generation.
 * Pure functions for text formatting and indentation.
 *
 * Extracted from CodeGenerator.ts as part of ADR-109 decomposition.
 */
class FormatUtils {
  /** Default indentation string (4 spaces) */
  static readonly INDENT = "    ";
 
  /**
   * Generate indentation string for the given level.
   * Uses 4 spaces per level, matching C-Next coding style.
   *
   * @param level - The indentation level (0 = no indent)
   * @returns String of spaces for indentation
   */
  static indent(level: number): string {
    return FormatUtils.INDENT.repeat(level);
  }
 
  /**
   * Indent each line of a multi-line string (skips empty lines).
   *
   * @param text - The text to indent (may contain newlines)
   * @param level - The indentation level
   * @returns Text with each non-empty line indented
   */
  static indentLines(text: string, level: number): string {
    const prefix = FormatUtils.indent(level);
    return text
      .split("\n")
      .map((line) => (line.length > 0 ? prefix + line : line))
      .join("\n");
  }
 
  /**
   * Indent ALL lines of a multi-line string (including empty lines).
   *
   * @param text - The text to indent (may contain newlines)
   * @param level - The indentation level
   * @returns Text with every line indented
   */
  static indentAllLines(text: string, level: number): string {
    const prefix = FormatUtils.indent(level);
    return text
      .split("\n")
      .map((line) => prefix + line)
      .join("\n");
  }
 
  /**
   * Join strings with separator, filtering out empty strings.
   *
   * @param parts - Array of strings to join
   * @param separator - Separator between parts
   * @returns Joined string with empty parts removed
   */
  static joinNonEmpty(parts: string[], separator: string): string {
    return parts.filter((p) => p.length > 0).join(separator);
  }
 
  /**
   * Wrap text in braces with proper formatting.
   * Used for generating C blocks like: { content }
   *
   * @param content - The content to wrap
   * @param inline - If true, format as "{ content }" on one line
   * @returns Formatted block string
   */
  static wrapInBraces(content: string, inline: boolean = false): string {
    if (inline) {
      return `{ ${content} }`;
    }
    return `{\n${content}\n}`;
  }
 
  /**
   * Get the appropriate scope separator for C++ vs C/C-Next.
   * C++ uses :: for scope resolution, C/C-Next uses _ (underscore).
   *
   * @param isCppContext - Whether generating C++ code
   * @returns "::" for C++ or "_" for C/C-Next
   */
  static getScopeSeparator(isCppContext: boolean): string {
    return isCppContext ? "::" : "_";
  }
}
 
export default FormatUtils;