mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
Update sortImports to support --import-grouping length,alpha (#2463)
Co-authored-by: calda <1811727+calda@users.noreply.github.com>
This commit is contained in:
@@ -84,7 +84,13 @@ extension Formatter {
|
||||
}
|
||||
|
||||
if grouping.contains(.length) {
|
||||
return lhs.module.count < rhs.module.count
|
||||
if lhs.module.count != rhs.module.count {
|
||||
return lhs.module.count < rhs.module.count
|
||||
}
|
||||
if grouping.contains(.alpha) {
|
||||
return lhs < rhs
|
||||
}
|
||||
return false
|
||||
}
|
||||
// Default to alphabetical
|
||||
return lhs < rhs
|
||||
|
||||
@@ -596,4 +596,36 @@ final class SortImportsTests: XCTestCase {
|
||||
let options = FormatOptions(importGrouping: [.length, .testableLast])
|
||||
testFormatting(for: input, output, rule: .sortImports, options: options)
|
||||
}
|
||||
|
||||
func testLengthThenAlphaSortImports() {
|
||||
let input = """
|
||||
import Module
|
||||
import Foo
|
||||
import Bar
|
||||
import Ab
|
||||
"""
|
||||
let output = """
|
||||
import Ab
|
||||
import Bar
|
||||
import Foo
|
||||
import Module
|
||||
"""
|
||||
let options = FormatOptions(importGrouping: [.length, .alpha])
|
||||
testFormatting(for: input, output, rule: .sortImports, options: options)
|
||||
}
|
||||
|
||||
func testLengthThenAlphaSortImportsWithSameLength() {
|
||||
let input = """
|
||||
import Zed
|
||||
import Foo
|
||||
import Bar
|
||||
"""
|
||||
let output = """
|
||||
import Bar
|
||||
import Foo
|
||||
import Zed
|
||||
"""
|
||||
let options = FormatOptions(importGrouping: [.length, .alpha])
|
||||
testFormatting(for: input, output, rule: .sortImports, options: options)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user