rfmt 0.1.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/rfmt/rfmt.so ADDED
Binary file
data/lib/rfmt/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rfmt
4
- VERSION = "0.1.0"
4
+ VERSION = '0.2.2'
5
5
  end
data/lib/rfmt.rb CHANGED
@@ -1,21 +1,172 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "rfmt/version"
4
- require_relative "rfmt/rfmt"
3
+ require_relative 'rfmt/version'
4
+ require_relative 'rfmt/rfmt'
5
+ require_relative 'rfmt/prism_bridge'
5
6
 
6
7
  module Rfmt
7
8
  class Error < StandardError; end
9
+ # Errors from Rust side
10
+ class RfmtError < Error; end
11
+ # AST validation errors
12
+ class ValidationError < RfmtError; end
13
+
14
+ # Format Ruby source code
15
+ # @param source [String] Ruby source code to format
16
+ # @return [String] Formatted Ruby code
8
17
  def self.format(source)
9
- format_code(source)
18
+ # Step 1: Parse with Prism (Ruby side)
19
+ prism_json = PrismBridge.parse(source)
20
+
21
+ # Step 2: Format in Rust
22
+ # Pass both source and AST to enable source extraction fallback
23
+ format_code(source, prism_json)
24
+ rescue PrismBridge::ParseError => e
25
+ # Re-raise with more context
26
+ raise Error, "Failed to parse Ruby code: #{e.message}"
27
+ rescue RfmtError
28
+ # Rust side errors are re-raised as-is to preserve error details
29
+ raise
30
+ rescue StandardError => e
31
+ raise Error, "Unexpected error during formatting: #{e.class}: #{e.message}"
10
32
  end
11
33
 
34
+ # Format a Ruby file
35
+ # @param path [String] Path to Ruby file
36
+ # @return [String] Formatted Ruby code
12
37
  def self.format_file(path)
13
38
  source = File.read(path)
14
- formatted = format(source)
15
- formatted
39
+ format(source)
40
+ rescue Errno::ENOENT
41
+ raise Error, "File not found: #{path}"
16
42
  end
17
43
 
44
+ # Get version information
45
+ # @return [String] Version string including Ruby and Rust versions
18
46
  def self.version_info
19
47
  "Ruby: #{VERSION}, Rust: #{rust_version}"
20
48
  end
49
+
50
+ # Parse Ruby code to AST (for debugging)
51
+ # @param source [String] Ruby source code
52
+ # @return [String] AST representation
53
+ def self.parse(source)
54
+ prism_json = PrismBridge.parse(source)
55
+ parse_to_json(prism_json)
56
+ end
57
+
58
+ # Configuration management
59
+ module Config
60
+ # Default configuration template
61
+ DEFAULT_CONFIG = <<~YAML
62
+ # rfmt Configuration File
63
+ # This file controls how rfmt formats your Ruby code.
64
+ # See https://github.com/fujitanisora/rfmt for full documentation.
65
+
66
+ version: "1.0"
67
+
68
+ # Formatting options
69
+ formatting:
70
+ # Maximum line length before wrapping (40-500)
71
+ line_length: 100
72
+
73
+ # Number of spaces or tabs per indentation level (1-8)
74
+ indent_width: 2
75
+
76
+ # Use "spaces" or "tabs" for indentation
77
+ indent_style: "spaces"
78
+
79
+ # Quote style for strings: "double", "single", or "consistent"
80
+ quote_style: "double"
81
+
82
+ # Files to include in formatting (glob patterns)
83
+ include:
84
+ - "**/*.rb"
85
+ - "**/*.rake"
86
+ - "**/Rakefile"
87
+ - "**/Gemfile"
88
+
89
+ # Files to exclude from formatting (glob patterns)
90
+ exclude:
91
+ - "vendor/**/*"
92
+ - "tmp/**/*"
93
+ - "node_modules/**/*"
94
+ - "db/schema.rb"
95
+ YAML
96
+
97
+ # Generate a default configuration file
98
+ # @param path [String] Path where to create the config file (default: .rfmt.yml)
99
+ # @param force [Boolean] Overwrite existing file if true
100
+ # @return [Boolean] true if file was created, false if already exists
101
+ def self.init(path = '.rfmt.yml', force: false)
102
+ if File.exist?(path) && !force
103
+ warn "Configuration file already exists: #{path}"
104
+ warn 'Use force: true to overwrite'
105
+ return false
106
+ end
107
+
108
+ File.write(path, DEFAULT_CONFIG)
109
+ puts "Created rfmt configuration file: #{path}"
110
+ true
111
+ end
112
+
113
+ # Find configuration file in current or parent directories
114
+ # @return [String, nil] Path to config file or nil if not found
115
+ def self.find
116
+ current_dir = Dir.pwd
117
+
118
+ loop do
119
+ ['.rfmt.yml', '.rfmt.yaml'].each do |filename|
120
+ config_path = File.join(current_dir, filename)
121
+ return config_path if File.exist?(config_path)
122
+ end
123
+
124
+ parent = File.dirname(current_dir)
125
+ break if parent == current_dir # Reached root
126
+
127
+ current_dir = parent
128
+ end
129
+
130
+ # Check user home directory
131
+ home_dir = begin
132
+ Dir.home
133
+ rescue StandardError
134
+ nil
135
+ end
136
+ if home_dir
137
+ ['.rfmt.yml', '.rfmt.yaml'].each do |filename|
138
+ config_path = File.join(home_dir, filename)
139
+ return config_path if File.exist?(config_path)
140
+ end
141
+ end
142
+
143
+ nil
144
+ end
145
+
146
+ # Check if configuration file exists
147
+ # @return [Boolean] true if config file exists
148
+ def self.exists?
149
+ !find.nil?
150
+ end
151
+
152
+ # Load and validate configuration file
153
+ # @param path [String, nil] Path to config file (default: auto-detect)
154
+ # @return [Hash] Loaded configuration
155
+ def self.load(path = nil)
156
+ require 'yaml'
157
+
158
+ config_path = path || find
159
+
160
+ unless config_path
161
+ warn 'No configuration file found, using defaults'
162
+ return {}
163
+ end
164
+
165
+ YAML.load_file(config_path)
166
+ rescue Errno::ENOENT
167
+ raise Error, "Configuration file not found: #{config_path}"
168
+ rescue Psych::SyntaxError => e
169
+ raise Error, "Invalid YAML in configuration file: #{e.message}"
170
+ end
171
+ end
21
172
  end
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rfmt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - fujitani sora
8
+ autorequire:
8
9
  bindir: exe
9
10
  cert_chain: []
10
- date: 1980-01-02 00:00:00.000000000 Z
11
+ date: 2025-11-24 00:00:00.000000000 Z
11
12
  dependencies:
12
13
  - !ruby/object:Gem::Dependency
13
14
  name: rb_sys
@@ -26,7 +27,8 @@ dependencies:
26
27
  description: Write a longer description or delete this line.
27
28
  email:
28
29
  - fujitanisora0414@gmail.com
29
- executables: []
30
+ executables:
31
+ - rfmt
30
32
  extensions:
31
33
  - ext/rfmt/extconf.rb
32
34
  extra_rdoc_files: []
@@ -36,11 +38,29 @@ files:
36
38
  - Cargo.toml
37
39
  - LICENSE.txt
38
40
  - README.md
41
+ - exe/rfmt
39
42
  - ext/rfmt/Cargo.toml
40
43
  - ext/rfmt/extconf.rb
44
+ - ext/rfmt/spec/config_spec.rb
45
+ - ext/rfmt/spec/spec_helper.rb
46
+ - ext/rfmt/src/ast/mod.rs
47
+ - ext/rfmt/src/config/mod.rs
48
+ - ext/rfmt/src/emitter/mod.rs
49
+ - ext/rfmt/src/error/mod.rs
41
50
  - ext/rfmt/src/lib.rs
51
+ - ext/rfmt/src/logging/logger.rs
52
+ - ext/rfmt/src/logging/mod.rs
53
+ - ext/rfmt/src/parser/mod.rs
54
+ - ext/rfmt/src/parser/prism_adapter.rs
55
+ - ext/rfmt/src/policy/mod.rs
56
+ - ext/rfmt/src/policy/validation.rs
42
57
  - lib/rfmt.rb
43
- - lib/rfmt/rfmt.bundle
58
+ - lib/rfmt/cache.rb
59
+ - lib/rfmt/cli.rb
60
+ - lib/rfmt/configuration.rb
61
+ - lib/rfmt/prism_bridge.rb
62
+ - lib/rfmt/prism_node_extractor.rb
63
+ - lib/rfmt/rfmt.so
44
64
  - lib/rfmt/version.rb
45
65
  homepage: https://github.com/fs0414/rfmt
46
66
  licenses:
@@ -50,6 +70,7 @@ metadata:
50
70
  homepage_uri: https://github.com/fs0414/rfmt
51
71
  source_code_uri: https://github.com/fs0414/rfmt
52
72
  changelog_uri: https://github.com/fs0414/rfmt/releases
73
+ post_install_message:
53
74
  rdoc_options: []
54
75
  require_paths:
55
76
  - lib
@@ -57,14 +78,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
57
78
  requirements:
58
79
  - - ">="
59
80
  - !ruby/object:Gem::Version
60
- version: 3.2.0
81
+ version: 3.1.0
61
82
  required_rubygems_version: !ruby/object:Gem::Requirement
62
83
  requirements:
63
84
  - - ">="
64
85
  - !ruby/object:Gem::Version
65
- version: 3.3.11
86
+ version: 3.0.0
66
87
  requirements: []
67
- rubygems_version: 3.7.1
88
+ rubygems_version: 3.5.22
89
+ signing_key:
68
90
  specification_version: 4
69
91
  summary: Ruby Formatter impl Rust lang.
70
92
  test_files: []
data/lib/rfmt/rfmt.bundle DELETED
Binary file