To find and collapse duplicated logic into a single, tested helper in Crystal, Atlas leverages its semantic search capabilities to identify near-identical code, even with differing variable names. It then uses its `write` tool to create the new helper module and `apply_patch` to replace each duplicate with a call, all while integrating directly with your Crystal toolchain, including `crystal spec` for testing and `crystal tool format` for consistent styling.
How to find duplicated logic in Crystal codebases with Atlas
Finding duplicated logic in a Crystal codebase in 2026 is significantly more efficient with Atlas's `codebase_search` tool, which uses semantic retrieval to identify near-identical code patterns that traditional `grep` commands would miss. This approach is crucial for Crystal's Ruby-like syntax, where variable names often change across 2 or more copies of the same underlying behavior.
Atlas's `codebase_search` tool goes beyond simple text matching by indexing your Crystal code using AST declarations via tree-sitter. This allows it to understand the structure and meaning of your code, not just its literal text. When you ask Atlas to find a specific behavior, like 'parse a JSON string into a User struct', it searches for the semantic intent, even if variable names or minor structural elements differ between implementations. This is particularly powerful in Crystal, where developers might copy-paste a block of code and then adjust local variable names, making it invisible to a keyword-based search. Atlas can build its code index with local Ollama embeddings, ensuring your Crystal source code remains off third-party servers. After `codebase_search` surfaces potential duplicates, you can use Atlas's `read` tool to inspect each hit and confirm they are genuinely equivalent before proceeding with the refactoring.
Replacing duplicated Crystal code with helper calls and ensuring type safety
Replacing each instance of duplicated Crystal code with a call to your new helper is managed by Atlas's `apply_patch` tool, which generates one reviewable patch per file, ensuring granular control over each modification. Crucially, after each swap, Atlas can run `crystal spec` to immediately verify type safety and catch any compiler errors, often related to 3 or more union types.
After the shared helper module is created, Atlas uses `apply_patch` to systematically replace each identified duplicate with a call to the new helper method. This tool is designed for safety and reviewability: it generates a separate, unified diff for each file where a replacement occurs. This 'one file per patch' approach means you can review and approve or reject each swap independently, making the refactoring process less risky. Following each `apply_patch` operation, Atlas can automatically execute `bash -c "crystal spec"` to run your project's test suite. This immediate feedback loop is invaluable in Crystal, where the compiler's strictness, especially with union types and `Nil` narrowing, can quickly expose issues introduced by refactoring. Atlas is configured to let it run `crystal spec` behind a permission prompt and iterate on any compiler errors, ensuring your refactored Crystal code remains robust and type-safe throughout the process. It can also run `crystal tool format` on the diff before you approve it, maintaining code style consistency.
Reviewing and committing Crystal refactorings with Atlas
Reviewing and committing your Crystal refactorings with Atlas is a transparent process, as Atlas computes a unified diff for every file edit and surfaces it for approval before writing. This allows you to inspect every change, including those formatted by `crystal tool format`, before staging and committing with built-in `git` integration in 2026.
Atlas provides comprehensive review mechanisms for every step of the refactoring workflow. Before any file is written or modified, Atlas computes a unified diff and presents it to you for approval. This includes the creation of the new helper file and every `apply_patch` operation that replaces duplicated code. Atlas also integrates with `crystal tool format`, ensuring that any changes it proposes adhere to your project's formatting standards before you even see the diff. This means you're always reviewing clean, consistently styled Crystal code. Furthermore, Atlas reads `git` branches, status, and diffs, and can stage and create commits on your behalf. It snapshots file changes as `git` patches, so edits can be diffed and rolled back easily. This robust review and version control integration ensures that your Crystal codebase remains stable and maintainable throughout the refactoring process, giving you confidence in the changes made.
Step by step
- 011. Run Atlas in your Crystal project directory, ensuring a `shard.yml` is present and `shards install` has resolved dependencies into `lib/`.
- 022. Ask Atlas's `codebase_search` for the behavior of the duplicated logic in your Crystal project, like `codebase_search "parse a JSON string into a User struct"`.
- 033. Use Atlas's `read` tool to inspect each `codebase_search` hit and confirm the copies are genuinely equivalent and ready for extraction.
- 044. Instruct Atlas's `write` tool to create the new shared helper file, for example, `write src/my_app/helpers/string_utils.cr` with the extracted logic, reviewing the full diff before approval.
- 055. Use Atlas's `apply_patch` tool to replace each duplicate instance with a call to the new helper, approving each file's patch independently.
- 066. After each `apply_patch` operation, let Atlas run `bash -c "crystal spec"` to execute your Crystal test suite and iterate on any compiler errors, especially those related to union types.
- 077. Finish by asking Atlas to run `bash -c "grep -r 'old_duplicated_logic_pattern' src/"` to ensure no surviving copies remain, then approve the final `crystal tool format` diffs and commit with Atlas's `git` integration.
Frequently asked questions
- How does Atlas identify duplicated Crystal code when variable names differ?
- Atlas uses `codebase_search` which indexes your Crystal code by AST declarations using tree-sitter. This allows it to understand the semantic structure and behavior of the code, rather than just keyword matching, effectively finding near-duplicate logic despite varying variable names.
- Can Atlas handle Crystal's strict type checking during refactoring?
- Yes, Atlas is designed to work with Crystal's strict type checking. After every code modification, it can run `crystal spec` to execute your test suite and catch any compiler errors, including those related to union types or `Nil` narrowing, allowing for immediate iteration and correction.
- How does Atlas ensure I review every change it makes to my Crystal files?
- Atlas computes a unified diff for every file edit, whether creating a new helper or applying a patch, and surfaces it for your explicit approval before writing. This permission-gated approach, combined with 'one file per patch' for replacements, ensures you have granular control over all changes.
- Does Atlas integrate with Crystal's formatting tools?
- Absolutely. Atlas can run `crystal tool format` on the diff before you approve it. This ensures that any code generated or modified by Atlas adheres to your project's established Crystal formatting standards, maintaining code consistency.
- What if I need to roll back a refactoring step in Crystal?
- Atlas integrates deeply with `git`. It snapshots file changes as `git` patches, allowing edits to be easily diffed and rolled back. You can also use Atlas to stage and create commits on your behalf, providing robust version control throughout the refactoring process.
- Can Atlas help with Crystal's `shard.yml` dependencies during refactoring?
- While Atlas primarily focuses on code refactoring, it operates within your Crystal project's context. It expects a `shard.yml` to be present and dependencies resolved by `shards install` into `lib/`. If your refactoring requires new dependencies, you would typically add them to `shard.yml` and run `shards install` manually, then Atlas can index the updated codebase.
Try Atlas in your terminal
The terminal-native AI coding agent. Free core, single binary.
Install AtlasRelated guides
Extract a Shared Helper from Duplicated Code with Atlas (2026 Workflow)
How to extract a shared helper from duplicated code with Atlas in 2026: codebase_search finds the copies by meaning, write creates the module, apply_patch swaps each call.
Atlas for Crystal: A Terminal-Native AI Coding Agent for shard.yml Projects in 2026
Atlas is a terminal-native AI coding agent for Crystal in 2026. It reads shard.yml and union types, narrows Nil unions, runs crystal spec behind a prompt, and formats the diff.
Rename a Symbol Across the Repo in Crystal with Atlas in 2026
In 2026, Crystal developers use Atlas to accurately rename functions, classes, and constants across their entire codebase, leveraging `crystal spec` and `shards` for verification.
Trace a runtime bug from a stack trace in Crystal with Atlas in 2026
Pinpoint Crystal runtime bugs from production stack traces in 2026 using Atlas, the terminal-native AI agent. Leverage `crystal spec` and `shards` for rapid, debugger-free fixes.
Diagnose a hanging or long-running command in Crystal with Atlas in 2026
In 2026, Crystal developers use Atlas to diagnose hanging or slow `crystal spec` tests or `shards` commands. Learn how Atlas identifies blocked input and helps you get unstuck.
Self-review your working diff before committing in Crystal with Atlas in 2026
Catch your own mistakes in uncommitted Crystal code with Atlas in 2026. Use Atlas to review diffs, run `crystal spec`, `crystal tool format`, and manage changes before committing.
Run the test suite and triage the failures in Crystal with Atlas in 2026
In 2026, Crystal developers use Atlas to efficiently run `crystal spec`, analyze extensive test output, and prioritize distinct root causes for failures, streamlining the debugging workflow.
Add a Regression Test for a Crystal Bug Fix with Atlas in 2026
In 2026, Crystal developers use Atlas to add regression tests for bug fixes, ensuring code quality. Learn how Atlas leverages `crystal spec` and `shards` to lock in fixes with red-green testing.