Class: Isort::FileProcessor
- Inherits:
-
Object
- Object
- Isort::FileProcessor
- Defined in:
- lib/isort/file_processor.rb
Overview
Main orchestrator for processing Ruby files Finds import blocks, sorts them, and reconstructs the file
Constant Summary collapse
- SKIP_FILE_PATTERN =
/^#\s*isort:\s*skip_file\b/i.freeze
- SKIP_LINE_PATTERN =
/#\s*isort:\s*skip\b/i.freeze
Instance Method Summary collapse
-
#check ⇒ Object
Check if file would change (dry-run mode for --check) Returns true if file would be modified, false otherwise.
-
#diff ⇒ Object
Get diff of changes without applying (for --diff mode) Returns diff string or nil if no changes.
-
#initialize(file_path, options = {}) ⇒ FileProcessor
constructor
A new instance of FileProcessor.
-
#process ⇒ Object
Process the file - returns true if changes were made.
Constructor Details
#initialize(file_path, options = {}) ⇒ FileProcessor
Returns a new instance of FileProcessor.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/isort/file_processor.rb', line 15 def initialize(file_path, = {}) @file_path = file_path @options = { check: false, diff: false, atomic: false, quiet: false, verbose: false }.merge() @parser = Parser.new end |
Instance Method Details
#check ⇒ Object
Check if file would change (dry-run mode for --check) Returns true if file would be modified, false otherwise
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/isort/file_processor.rb', line 76 def check original_content = read_file_content return false if original_content.nil? || original_content.empty? # Check for skip_file directive begin check_skip_file_directive!(original_content) rescue FileSkipped return false end lines = parse_lines(original_content) return false if lines.empty? blocks_with_positions = find_import_blocks(lines) return false if blocks_with_positions.empty? # Sort each block (on a copy) blocks_with_positions.each do |block_info| block_info[:block].sort_and_dedupe! end new_content = reconstruct_file(lines, blocks_with_positions) new_content != original_content end |
#diff ⇒ Object
Get diff of changes without applying (for --diff mode) Returns diff string or nil if no changes
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/isort/file_processor.rb', line 104 def diff original_content = read_file_content return nil if original_content.nil? || original_content.empty? # Check for skip_file directive begin check_skip_file_directive!(original_content) rescue FileSkipped return nil end lines = parse_lines(original_content) return nil if lines.empty? blocks_with_positions = find_import_blocks(lines) return nil if blocks_with_positions.empty? # Sort each block (on a copy) blocks_with_positions.each do |block_info| block_info[:block].sort_and_dedupe! end new_content = reconstruct_file(lines, blocks_with_positions) return nil if new_content == original_content generate_diff(original_content, new_content) end |
#process ⇒ Object
Process the file - returns true if changes were made
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/isort/file_processor.rb', line 28 def process original_content = read_file_content return false if original_content.nil? || original_content.empty? # Check for skip_file directive check_skip_file_directive!(original_content) # Atomic mode: validate original syntax first if @options[:atomic] validate_original_syntax!(original_content) end lines = parse_lines(original_content) return false if lines.empty? # Find all import blocks in the file blocks_with_positions = find_import_blocks(lines) return false if blocks_with_positions.empty? # Sort each block blocks_with_positions.each do |block_info| block_info[:block].sort_and_dedupe! end # Reconstruct the file with sorted blocks new_content = reconstruct_file(lines, blocks_with_positions) # No changes needed return false if new_content == original_content # Atomic mode: validate new syntax before writing if @options[:atomic] validate_new_syntax!(new_content) end # Write the file write_file(new_content) true rescue ArgumentError => e # Handle encoding errors from strip/other string operations if e..include?("invalid byte sequence") raise Encoding::CompatibilityError, "Invalid encoding in #{@file_path}: #{e.}" end raise end |