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 +4 -4
- data/CHANGELOG.md +1 -2
- data/README.md +3 -1
- data/lib/isort/version.rb +1 -1
- data/lib/isort.rb +45 -17
- data/sample.rb +4 -20
- metadata +9 -7
- data/isort-0.1.0.gem +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c121d3b6fba0a8fcf8a644eb26ebce0e35b11452cb1a3a644dfbee265a6bd1d
|
4
|
+
data.tar.gz: a80ae99d0f3d627407f7ccdd377365d10412ef9ade8b196b839285cee916a935
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a92208a0dfd4b76459832abb07ee6878976adfd8c40f8dd249bd330c1d2717e046bd4427a1078a43ff113c3d6adf82b994f3b4dcdf30471f1b1d17a1e54d3d1
|
7
|
+
data.tar.gz: 5ca8c19a71ed5c79135ed98b8f0ca74fe74a7b0ccb22d1de67d93e8fb75192d5f2f69957ab776a231989064a45b966ec2fb9f6a3cea02791acd5740939680635
|
data/CHANGELOG.md
CHANGED
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/
|
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
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 =
|
39
|
-
require_relatives =
|
40
|
-
includes =
|
41
|
-
extends =
|
42
|
-
autoloads =
|
43
|
-
usings =
|
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 = (
|
55
|
+
sorted_content = "#{formatted_imports.reject(&:empty?).join("\n")}\n#{others.join}".strip
|
57
56
|
|
58
|
-
#
|
59
|
-
|
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
|
69
|
-
|
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
|
-
#
|
72
|
-
|
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
|
-
|
2
|
-
|
3
|
-
require_relative
|
4
|
-
require_relative
|
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
|
+
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/
|
93
|
+
homepage: https://github.com/abhinvv1/isort
|
92
94
|
licenses:
|
93
95
|
- MIT
|
94
96
|
metadata:
|
95
|
-
homepage_uri: https://github.com/abhinvv1/
|
96
|
-
source_code_uri: https://github.com/abhinvv1/
|
97
|
-
changelog_uri: https://github.com/abhinvv1/
|
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
|