<commit_before>import { assertNever } from '../../lib/fatal-error'

/**
 * The state of a file's diff selection
 */
export enum DiffSelectionType {
  /** The entire file should be committed */
  All,
  /** A subset of lines in the file have been selected for committing */
  Partial,
  /** The file should be excluded from committing */
  None,
}

/**
 * Utility function which determines whether a boolean selection state
 * matches the given DiffSelectionType. A true selection state matches
 * DiffSelectionType.All, a false selection state matches
 * DiffSelectionType.None and if the selection type is partial there's
 * never a match.
 */
function typeMatchesSelection(
  selectionType: DiffSelectionType,
  selected: boolean
): boolean {
  switch (selectionType) {
    case DiffSelectionType.All:
      return selected
    case DiffSelectionType.None:
      return !selected
    case DiffSelectionType.Partial:
      return false
    default:
      return assertNever(
        selectionType,
        `Unknown selection type ${selectionType}`
      )
  }
}

/**
 * An immutable, efficient, storage object for tracking selections of indexable
 * lines. While general purpose by design this is currently used exclusively for
 * tracking selected lines in modified files in the working directory.
 *
 * This class starts out with an initial (or default) selection state, ie
 * either all lines are selected by default or no lines are selected by default.
 *
 * The selection can then be transformed by marking a line or a range of lines
 * as selected or not selected. Internally the class maintains a list of lines
 * whose selection state has diverged from the default selection state.
 */
export class DiffSelection {
  private readonly defaultSelectionType:
    | DiffSelectionType.All
    | DiffSelectionType.None

  /* Any line numbers where the selection differs from the default state. */
  private readonly divergingLines: Set<number> | null

  /* Optional set of line numbers which can be selected. */
  private readonly selectableLines: Set<number> | null

  /**
   * Initialize a new selection instance where either all lines are selected by default
   * or not lines are selected by default.
   */
  public static fromInitialSelection(
    initialSelection: DiffSelectionType.All | DiffSelectionType.None
  ): DiffSelection {
    if (
      initialSelection !== DiffSelectionType.All &&
      initialSelection !== DiffSelectionType.None
    ) {
      return assertNever(
        initialSelection,
        'Can only instantiate a DiffSelection with All or None as the initial selection'
      )
    }

    return new DiffSelection(initialSelection, null, null)
  }

  private constructor(
    defaultSelectionType: DiffSelectionType.All | DiffSelectionType.None,
    divergingLines: Set<number> | null,
    selectableLines: Set<number> | null
  ) {
    this.defaultSelectionType = defaultSelectionType
    this.divergingLines = divergingLines || null
    this.selectableLines = selectableLines || null
  }

  /** Returns a value indicating the computed overall state of the selection */
  public getSelectionType(): DiffSelectionType {
    const divergingLines = this.divergingLines
    const selectableLines = this.selectableLines

    // No diverging lines, happy path. Either all lines are selected or none are.
    if (!divergingLines) {
      return this.defaultSelectionType
    }
    if (divergingLines.size === 0) {
      return this.defaultSelectionType
    }

    // If we know which lines are selectable we need to check that
    // all lines are divergent and return the inverse of default selection.
    // To avoid loopting through the set that often our happy path is
    // if there's a size mismatch.
    if (selectableLines && selectableLines.size === divergingLines.size) {
      const allSelectableLinesAreDivergent = [...selectableLines].every(i =>
        divergingLines.has(i)
      )

      if (allSelectableLinesAreDivergent) {
        return this.defaultSelectionType === DiffSelectionType.All
          ? DiffSelectionType.None
          : DiffSelectionType.All
      }
    }
<commit_msg>Refactored models/diff/diff-selection constructors with parameter properties<commit_after>import { assertNever } from '../../lib/fatal-error'

/**
 * The state of a file's diff selection
 */
export enum DiffSelectionType {
  /** The entire file should be committed */
  All,
  /** A subset of lines in the file have been selected for committing */
  Partial,
  /** The file should be excluded from committing */
  None,
}

/**
 * Utility function which determines whether a boolean selection state
 * matches the given DiffSelectionType. A true selection state matches
 * DiffSelectionType.All, a false selection state matches
 * DiffSelectionType.None and if the selection type is partial there's
 * never a match.
 */
function typeMatchesSelection(
  selectionType: DiffSelectionType,
  selected: boolean
): boolean {
  switch (selectionType) {
    case DiffSelectionType.All:
      return selected
    case DiffSelectionType.None:
      return !selected
    case DiffSelectionType.Partial:
      return false
    default:
      return assertNever(
        selectionType,
        `Unknown selection type ${selectionType}`
      )
  }
}

/**
 * An immutable, efficient, storage object for tracking selections of indexable
 * lines. While general purpose by design this is currently used exclusively for
 * tracking selected lines in modified files in the working directory.
 *
 * This class starts out with an initial (or default) selection state, ie
 * either all lines are selected by default or no lines are selected by default.
 *
 * The selection can then be transformed by marking a line or a range of lines
 * as selected or not selected. Internally the class maintains a list of lines
 * whose selection state has diverged from the default selection state.
 */
export class DiffSelection {
  /* Any line numbers where the selection differs from the default state. */
  //private readonly divergingLines: Set<number> | null

  /* Optional set of line numbers which can be selected. */
  //private readonly selectableLines: Set<number> | null

  /**
   * Initialize a new selection instance where either all lines are selected by default
   * or not lines are selected by default.
   */
  public static fromInitialSelection(
    initialSelection: DiffSelectionType.All | DiffSelectionType.None
  ): DiffSelection {
    if (
      initialSelection !== DiffSelectionType.All &&
      initialSelection !== DiffSelectionType.None
    ) {
      return assertNever(
        initialSelection,
        'Can only instantiate a DiffSelection with All or None as the initial selection'
      )
    }

    return new DiffSelection(initialSelection, null, null)
  }

  private constructor(
    private readonly defaultSelectionType:
      | DiffSelectionType.All
      | DiffSelectionType.None,
    private readonly divergingLines: Set<number> | null = null,
    private readonly selectableLines: Set<number> | null = null
  ) {}

  /** Returns a value indicating the computed overall state of the selection */
  public getSelectionType(): DiffSelectionType {
    const divergingLines = this.divergingLines
    const selectableLines = this.selectableLines

    // No diverging lines, happy path. Either all lines are selected or none are.
    if (!divergingLines) {
      return this.defaultSelectionType
    }
    if (divergingLines.size === 0) {
      return this.defaultSelectionType
    }

    // If we know which lines are selectable we need to check that
    // all lines are divergent and return the inverse of default selection.
    // To avoid loopting through the set that often our happy path is
    // if there's a size mismatch.
    if (selectableLines && selectableLines.size === divergingLines.size) {
      const allSelectableLinesAreDivergent = [...selectableLines].every(i =>
        divergingLines.has(i)
      )

      if (allSelectableLinesAreDivergent) {
        return this.defaultSelectionType === DiffSelectionType.All
          ? DiffSelectionType.None
          : DiffSelectionType.All
      }
    }

{'fraction_non_alphanumeric': 0.06931664826401669, 'fraction_numerical': 0.000245368666421298, 'mean_word_length': 3.2747771368641847, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1350404', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1987, 'n_words': 820}