Ragex.Editor.Refactor.MetaAST (Ragex v0.11.0)

View Source

Language-agnostic refactoring operations via Metastatic MetaAST.

Performs AST-aware rename operations on any language supported by Metastatic by traversing and transforming the MetaAST representation, then converting back to source code via Metastatic.Builder.to_source/1.

Supported Operations

  • rename_function/4 -- renames a function definition and all call sites
  • rename_module/3 -- renames a module/class and updates references

Usage

alias Ragex.Editor.Refactor.MetaAST, as: MetaRefactor

{:ok, new_source} = MetaRefactor.rename_function(source, :elixir, "old_name", "new_name")
{:ok, new_source} = MetaRefactor.rename_module(source, :elixir, "OldModule", "NewModule")

Summary

Functions

Renames a function definition and all its call sites in source code.

Renames a module/class and updates all references in source code.

Functions

rename_function(source, language, old_name, new_name, opts \\ [])

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

Renames a function definition and all its call sites in source code.

Parameters

  • source -- source code string
  • language -- language atom
  • old_name -- current function name (string)
  • new_name -- new function name (string)
  • opts -- options
    • :arity -- only rename functions with this arity (default: all arities)
    • :module -- only rename within this module/container (default: all)

Returns

  • {:ok, new_source} on success
  • {:error, reason} on failure

rename_function_doc(doc, old_name, new_name, opts \\ [])

@spec rename_function_doc(Metastatic.Document.t(), String.t(), String.t(), keyword()) ::
  {:ok, Metastatic.Document.t()}

Same as rename_function/5 but operates on a Metastatic.Document directly.

Returns {:ok, new_doc} with the transformed document.

rename_module(source, language, old_name, new_name)

@spec rename_module(String.t(), atom(), String.t(), String.t()) ::
  {:ok, String.t()} | {:error, term()}

Renames a module/class and updates all references in source code.

Parameters

  • source -- source code string
  • language -- language atom
  • old_name -- current module name (string, e.g. "MyApp.OldModule")
  • new_name -- new module name (string, e.g. "MyApp.NewModule")

Returns

  • {:ok, new_source} on success
  • {:error, reason} on failure

rename_module_doc(doc, old_name, new_name)

@spec rename_module_doc(Metastatic.Document.t(), String.t(), String.t()) ::
  {:ok, Metastatic.Document.t()}

Same as rename_module/4 but operates on a Metastatic.Document directly.