Module: Rfmt

Defined in:
lib/rfmt.rb,
lib/rfmt/cli.rb,
lib/rfmt/cache.rb,
lib/rfmt/version.rb,
lib/rfmt/prism_bridge.rb,
lib/rfmt/configuration.rb,
lib/rfmt/prism_node_extractor.rb,
lib/rfmt/native_extension_loader.rb

Defined Under Namespace

Modules: Config, NativeExtensionLoader, PrismNodeExtractor Classes: CLI, Cache, CacheCommands, Configuration, Error, PrismBridge, RfmtError, ValidationError

Constant Summary collapse

VERSION =
'1.6.0'

Class Method Summary collapse

Class Method Details

.format(source) ⇒ String

Format Ruby source code

Parameters:

  • source (String)

    Ruby source code to format

Returns:

  • (String)

    Formatted Ruby code



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rfmt.rb', line 20

def self.format(source)
  # Step 1: Parse with Prism (Ruby side)
  prism_json = PrismBridge.parse(source)

  # Step 2: Format in Rust
  # Pass both source and AST to enable source extraction fallback
  format_code(source, prism_json)
rescue PrismBridge::ParseError => e
  # Re-raise with more context
  raise Error, "Failed to parse Ruby code: #{e.message}"
rescue RfmtError
  # Rust side errors are re-raised as-is to preserve error details
  raise
rescue StandardError => e
  raise Error, "Unexpected error during formatting: #{e.class}: #{e.message}"
end

.format_file(path) ⇒ String

Format a Ruby file

Parameters:

  • path (String)

    Path to Ruby file

Returns:

  • (String)

    Formatted Ruby code



40
41
42
43
44
45
# File 'lib/rfmt.rb', line 40

def self.format_file(path)
  source = File.read(path)
  format(source)
rescue Errno::ENOENT
  raise Error, "File not found: #{path}"
end

.parse(source) ⇒ String

Parse Ruby code to AST (for debugging)

Parameters:

  • source (String)

    Ruby source code

Returns:

  • (String)

    AST representation



56
57
58
59
# File 'lib/rfmt.rb', line 56

def self.parse(source)
  prism_json = PrismBridge.parse(source)
  parse_to_json(prism_json)
end

.version_infoString

Get version information

Returns:

  • (String)

    Version string including Ruby and Rust versions



49
50
51
# File 'lib/rfmt.rb', line 49

def self.version_info
  "Ruby: #{VERSION}, Rust: #{rust_version}"
end