isort 0.1.4 → 0.1.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a046b9382cc4f3416cfd1105cfb5c248c04a634cb0f9a3a95cb9c4ddab8938e2
4
- data.tar.gz: 0ff466bd33cff96343da32c8483e9f5fc64cc437c3fd02674951434b25762027
3
+ metadata.gz: 6c121d3b6fba0a8fcf8a644eb26ebce0e35b11452cb1a3a644dfbee265a6bd1d
4
+ data.tar.gz: a80ae99d0f3d627407f7ccdd377365d10412ef9ade8b196b839285cee916a935
5
5
  SHA512:
6
- metadata.gz: 0e3e720fca7b0947fc2f7c164898651863a0b816e5ec9316f51ada1e06fc86ef72066cad22a2a0ae0f531667afe54445dab638da9e226f2baeb81c18de851d7c
7
- data.tar.gz: ebac9c17a54d5f0e499daece31cad74375f5969443326c1d47769a2b7b9fa081e1df27386b516baf71301606a28cc0174abe5426d296f7dca76a5cb87c41438c
6
+ metadata.gz: 4a92208a0dfd4b76459832abb07ee6878976adfd8c40f8dd249bd330c1d2717e046bd4427a1078a43ff113c3d6adf82b994f3b4dcdf30471f1b1d17a1e54d3d1
7
+ data.tar.gz: 5ca8c19a71ed5c79135ed98b8f0ca74fe74a7b0ccb22d1de67d93e8fb75192d5f2f69957ab776a231989064a45b966ec2fb9f6a3cea02791acd5740939680635
data/CHANGELOG.md CHANGED
@@ -1,4 +1,4 @@
1
- ## [Unreleased]
1
+ ## [Released]
2
2
 
3
3
  ## [0.1.3] - 2024-12-28
4
4
 
@@ -17,4 +17,3 @@
17
17
 
18
18
  ### Added
19
19
  - Import sorting functionality support for a whole directory
20
- -
data/README.md CHANGED
@@ -14,6 +14,8 @@ gem install isort
14
14
 
15
15
  ```bash
16
16
  isort --file path/to/your/file.rb
17
+ or
18
+ isort -f path/to/your/file.rb
17
19
  ```
18
20
 
19
21
  ### In Ruby Code
@@ -35,7 +37,7 @@ sorter.sort_and_format_imports
35
37
 
36
38
  ## Contributing
37
39
 
38
- Bug reports and pull requests are welcome on GitHub at https://github.com/yourusername/isort.
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/abhinvv1/isort.
39
41
 
40
42
  ## License
41
43
 
data/lib/isort/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Isort
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.5"
5
5
  end
data/lib/isort.rb CHANGED
@@ -1,8 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "isort/version"
4
-
5
1
  require 'optparse'
2
+ require_relative "isort/version"
6
3
 
7
4
  module Isort
8
5
  class Error < StandardError; end
@@ -35,12 +32,12 @@ module Isort
35
32
  lines = File.readlines(@file_path)
36
33
 
37
34
  # Separate and group lines
38
- requires = extract_lines(lines, /^require\s/)
39
- require_relatives = extract_lines(lines, /^require_relative\s/)
40
- includes = extract_lines(lines, /^include\s/)
41
- extends = extract_lines(lines, /^extend\s/)
42
- autoloads = extract_lines(lines, /^autoload\s/)
43
- usings = extract_lines(lines, /^using\s/)
35
+ requires = extract_lines_with_comments(lines, /^require\s/)
36
+ require_relatives = extract_lines_with_comments(lines, /^require_relative\s/)
37
+ includes = extract_lines_with_comments(lines, /^include\s/)
38
+ extends = extract_lines_with_comments(lines, /^extend\s/)
39
+ autoloads = extract_lines_with_comments(lines, /^autoload\s/)
40
+ usings = extract_lines_with_comments(lines, /^using\s/)
44
41
  others = lines.reject { |line| [requires, require_relatives, includes, extends, autoloads, usings].flatten.include?(line) }
45
42
 
46
43
  # Format and sort each group
@@ -52,11 +49,16 @@ module Isort
52
49
  formatted_imports << format_group("autoload", autoloads)
53
50
  formatted_imports << format_group("using", usings)
54
51
 
52
+ return if [requires, require_relatives, includes, extends, autoloads, usings].all?(&:empty?)
53
+
55
54
  # Combine formatted imports with the rest of the file
56
- sorted_content = (formatted_imports + others).join
55
+ sorted_content = "#{formatted_imports.reject(&:empty?).join("\n")}\n#{others.join}".strip
57
56
 
58
- # Write the sorted content back to the file
59
- File.write(@file_path, sorted_content)
57
+ # Add a trailing newline only if imports exist
58
+ sorted_content = "#{sorted_content.rstrip}\n" if !formatted_imports.empty? && !sorted_content.empty?
59
+
60
+ # Write the sorted content back to the file only if imports exist
61
+ File.write(@file_path, sorted_content) unless formatted_imports.empty? && sorted_content.empty?
60
62
  end
61
63
 
62
64
  private
@@ -65,11 +67,37 @@ module Isort
65
67
  lines.select { |line| line =~ regex }
66
68
  end
67
69
 
68
- def format_group(type, lines)
69
- return [] if lines.empty?
70
+ def extract_lines_with_comments(lines, regex)
71
+ grouped_lines = []
72
+ buffer = []
73
+
74
+ lines.each do |line|
75
+ if line.strip.start_with?("#") || line.strip.empty?
76
+ # If the line is a comment or blank, add it to the buffer
77
+ buffer << line
78
+ elsif line =~ regex
79
+ # If it's an import line matching the regex, attach the buffer as comments
80
+ grouped_lines << (buffer + [line])
81
+ buffer = [] # Reset buffer
82
+ else
83
+ # If it's a non-matching line, reset the buffer
84
+ buffer = []
85
+ end
86
+ end
87
+
88
+ grouped_lines
89
+ end
90
+
91
+ def format_group(type, grouped_lines)
92
+ return [] if grouped_lines.empty?
70
93
 
71
- # Remove duplicates and sort
72
- lines.uniq.sort
94
+ # Flatten and sort each group by the import line
95
+ grouped_lines
96
+ .sort_by { |lines| lines.last.strip } # Sort by the actual import statement
97
+ .map { |lines| lines.join } # Combine comments with the import
98
+ .map(&:strip) # Remove trailing newlines
99
+ .join("\n") # Join all imports in the group with newlines
100
+ .concat("\n") # Add a newline between groups
73
101
  end
74
102
 
75
103
  end
data/sample.rb CHANGED
@@ -1,20 +1,4 @@
1
- require 'csv'
2
- require 'json'
3
- require_relative 'a_file'
4
- require_relative 'b_file'
5
- include SomeModule
6
- extend AnotherModule
7
- autoload :CSV, 'csv'
8
- using SomeRefinement
9
- class A
10
-
11
- end
12
-
13
-
14
-
15
-
16
-
17
-
18
- class B
19
-
20
- end
1
+ require_relative "aa"
2
+ require_relative "aaaa"
3
+ require_relative "aaaaac"
4
+ require_relative "abc"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: isort
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - abhinvv1
@@ -67,7 +67,10 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
69
  description: Isort automatically sorts and organizes your Ruby imports, including
70
- require, require_relative, include, and extend statements
70
+ require, require_relative, include, using, and extend statements.Has the ability
71
+ to group different types of imports together. It can process single files or entire
72
+ directories directly throughcommand-line interface, and preserve the code comments
73
+ and inline documentation
71
74
  email:
72
75
  - abhinav.p@browserstack.com
73
76
  executables:
@@ -82,19 +85,18 @@ files:
82
85
  - README.md
83
86
  - Rakefile
84
87
  - exe/isort
85
- - isort-0.1.0.gem
86
88
  - lib/isort.rb
87
89
  - lib/isort/version.rb
88
90
  - sample.rb
89
91
  - sig/isort.rbs
90
92
  - sig/isort/file_sorter.rbs
91
- homepage: https://github.com/abhinvv1/sort
93
+ homepage: https://github.com/abhinvv1/isort
92
94
  licenses:
93
95
  - MIT
94
96
  metadata:
95
- homepage_uri: https://github.com/abhinvv1/sort
96
- source_code_uri: https://github.com/abhinvv1/sort
97
- changelog_uri: https://github.com/abhinvv1/sort
97
+ homepage_uri: https://github.com/abhinvv1/isort
98
+ source_code_uri: https://github.com/abhinvv1/isort
99
+ changelog_uri: https://github.com/abhinvv1/isort
98
100
  post_install_message:
99
101
  rdoc_options: []
100
102
  require_paths:
data/isort-0.1.0.gem DELETED
File without changes