Class SectionedBuffers

java.lang.Object
org.ek9lang.cli.SectionedBuffers

public final class SectionedBuffers extends Object
Manages sectioned source buffers for the REPL. Each EK9 section type (type, function, record, trait, class, program) has its own buffer. Provides functional operations for iterating across all buffers with a unified line numbering scheme.

This class eliminates duplication by providing a single iteration mechanism that can be used for delete, edit, insert, and line number translation operations.

  • Constructor Details

    • SectionedBuffers

      public SectionedBuffers()
  • Method Details

    • getAllSections

      public List<SectionedBuffers.Section> getAllSections()
      Get all sections in the canonical order for source assembly.
    • getBufferForMode

      public List<String> getBufferForMode(ReplMode mode)
      Get the buffer list for a specific mode.
    • findByGlobalLine

      public Optional<SectionedBuffers.BufferLocation> findByGlobalLine(int globalLineNum)
      Find a buffer location by global line number (1-based). Iterates through all sections in order until the line is found.
      Parameters:
      globalLineNum - The 1-based global line number
      Returns:
      The location if found, empty otherwise
    • applyAtLine

      public <T> Optional<T> applyAtLine(int globalLineNum, BiFunction<SectionedBuffers.Section, Integer, T> operation)
      Apply an operation at a specific global line number. The operation receives the section and index, and returns a result.
      Type Parameters:
      T - The result type
      Parameters:
      globalLineNum - The 1-based global line number
      operation - The operation to apply (section, index) -> result
      Returns:
      The operation result if line was found, empty otherwise
    • deleteLine

      public boolean deleteLine(int globalLineNum)
      Delete a line by its global line number.
      Parameters:
      globalLineNum - The 1-based global line number to delete
      Returns:
      true if deleted, false if line not found
    • editLine

      public boolean editLine(int globalLineNum, String newText)
      Edit a line by its global line number.
      Parameters:
      globalLineNum - The 1-based global line number to edit
      newText - The new text for the line
      Returns:
      true if edited, false if line not found
    • insertLine

      public boolean insertLine(int globalLineNum, String text, ReplMode currentMode)
      Insert a line before the specified global line number. If globalLineNum is one past the last line, appends to the specified mode's buffer.
      Parameters:
      globalLineNum - The 1-based position to insert before
      text - The text to insert
      currentMode - The current mode (used for append case)
      Returns:
      true if inserted, false if line number out of range
    • getTotalLineCount

      public int getTotalLineCount()
      Get the total number of lines across all buffers.
    • clearAll

      public void clearAll()
      Clear all buffers.
    • isEmpty

      public boolean isEmpty()
      Check if all buffers are empty.
    • createSnapshot

      public SectionedBuffers.BufferSnapshot createSnapshot()
      Create a snapshot of all buffers for undo support.
    • restoreFromSnapshot

      public void restoreFromSnapshot(SectionedBuffers.BufferSnapshot snapshot)
      Restore buffers from a snapshot.
    • calculateBufferIndex

      public int calculateBufferIndex(int actualLineNumber)
      Calculate the buffer index for a given actual source line number.
      Parameters:
      actualLineNumber - The line number in assembled source
      Returns:
      The buffer index, or -1 if not a user line