Search documentation of Ragex

# Ragex.Editor.Undo (Ragex v0.4.0)

View Source

Multi-level undo/redo stack for refactoring operations.

Provides persistent history of refactoring operations with the ability to undo and redo changes. History is stored per-project in: `~/.ragex/undo/<project_hash>/`

Each undo entry contains:

- Operation metadata (type, timestamp, parameters)
- File states before the operation
- Operation result (success/failure)

## Summary

### Types

operation_type()
undo_entry()
undo_stack()

### Functions

clear_undo_stack(project_path, opts \\ [])

Clears the undo history for a project.

list_undo_stack(project_path, opts \\ [])

Lists the undo history for a project.

push_undo(project_path, operation, params, files_affected, result)

Pushes a new operation onto the undo stack.

redo(project_path)

Redoes the most recently undone operation.

undo(project_path)

Undoes the most recent operation.

## Types

# operation_type()

    @type operation_type() ::
      :rename_function
      | :rename_module
      | :extract_function
      | :inline_function
      | :convert_visibility
      | :rename_parameter
      | :modify_attributes
      | :change_signature
      | :move_function
      | :extract_module

# undo_entry()

    @type undo_entry() :: %{
      id: String.t(),
      operation: operation_type(),
      timestamp: DateTime.t(),
      params: map(),
      files_affected: [String.t()],
      file_states: %{required(String.t()) => String.t()},
      result: :success | :failure,
      description: String.t()
    }

# undo_stack()

    @type undo_stack() :: [undo_entry()]

## Functions

# clear_undo_stack(project_path, opts \\ [])

    @spec clear_undo_stack(
      String.t(),
      keyword()
    ) :: {:ok, non_neg_integer()} | {:error, term()}

Clears the undo history for a project.

## Parameters

- `project_path`: Project root path
- `opts`: Options
  - `:keep_last` - Number of entries to keep (default: 0)

## Returns

- `{:ok, count}` number of entries cleared

# list_undo_stack(project_path, opts \\ [])

    @spec list_undo_stack(
      String.t(),
      keyword()
    ) :: {:ok, [undo_entry()]} | {:error, term()}

Lists the undo history for a project.

## Parameters

- `project_path`: Project root path
- `opts`: Options
  - `:limit` - Maximum entries to return (default: 50)
  - `:include_undone` - Include undone entries (default: false)

## Returns

- `{:ok, entries}` list of undo entries

# push_undo(project_path, operation, params, files_affected, result)

    @spec push_undo(
      String.t(),
      operation_type(),
      map(),
      [String.t()],
      :success | :failure
    ) ::
      {:ok, String.t()} | {:error, term()}

Pushes a new operation onto the undo stack.

## Parameters

- `project_path`: Project root path
- `operation`: Operation type atom
- `params`: Operation parameters
- `files_affected`: List of file paths
- `result`: Operation result

## Returns

- `{:ok, entry_id}` on success
- `{:error, reason}` on failure

# redo(project_path)

    @spec redo(String.t()) :: {:ok, map()} | {:error, term()}

Redoes the most recently undone operation.

## Parameters

- `project_path`: Project root path

## Returns

- `{:ok, result}` with redo details on success
- `{:error, reason}` on failure

# undo(project_path)

    @spec undo(String.t()) :: {:ok, map()} | {:error, term()}

Undoes the most recent operation.

## Parameters

- `project_path`: Project root path

## Returns

- `{:ok, result}` with undo details on success
- `{:error, reason}` on failure