Ragex.Editor.Core
(Ragex v0.8.0)
View Source
Core editing functionality with atomic operations and validation.
Provides safe file editing with:
- Automatic backups before editing
- Atomic write operations
- Concurrent modification detection
- Rollback support
- Integration with validation pipeline
Summary
Functions
Edits a file by applying a list of changes.
Gets editing history (backups) for a file.
Rolls back the most recent edit to a file.
Validates changes without applying them.
Functions
@spec edit_file(String.t(), [Ragex.Editor.Types.change()], keyword()) :: {:ok, Ragex.Editor.Types.edit_result()} | {:error, term()}
Edits a file by applying a list of changes.
Parameters
path: Path to the file to editchanges: List of change structs (seeTypes)opts: Options:validate- Validate changes before applying (default: true):create_backup- Create backup before editing (default: true):format- Format code after editing (default: false):validator- Custom validator module (optional):language- Explicit language for validation (optional, auto-detected from file extension)
Validation
When validation is enabled (default), the validator is automatically selected based on file extension. Supports: Elixir (.ex, .exs), Erlang (.erl, .hrl), Python (.py), JavaScript (.js, .jsx, .ts, .tsx, .mjs, .cjs)
Returns
{:ok, result}on success{:error, reason}on failure
Examples
iex> changes = [Types.replace(10, 15, "new content")]
iex> Core.edit_file("lib/my_file.ex", changes)
{:ok, %{path: "lib/my_file.ex", changes_applied: 1, ...}}
iex> # Disable validation
iex> Core.edit_file("lib/file.ex", changes, validate: false)
{:ok, %{...}}
@spec history( String.t(), keyword() ) :: {:ok, [Ragex.Editor.Types.backup_info()]} | {:error, term()}
Gets editing history (backups) for a file.
Parameters
path: Path to the fileopts: Options passed toBackup.list/2
Returns
List of backup info structs.
@spec rollback( String.t(), keyword() ) :: {:ok, Ragex.Editor.Types.backup_info()} | {:error, term()}
Rolls back the most recent edit to a file.
Parameters
path: Path to the fileopts: Options:backup_id- Specific backup to restore (optional)
Returns
{:ok, backup_info}on success{:error, reason}on failure
@spec validate_changes(String.t(), [Ragex.Editor.Types.change()], keyword()) :: :ok | {:error, [Ragex.Editor.Types.validation_error()]}
Validates changes without applying them.
Parameters
path: Path to the file (for context and validator auto-detection)changes: List of change structsopts: Options:validator- Custom validator module (optional):language- Explicit language for validation (optional)
Returns
:okif valid{:error, errors}if invalid