Skip to content

Commit

Permalink
feat: sort swift import too
Browse files Browse the repository at this point in the history
  • Loading branch information
hztanxin committed Jun 19, 2023
1 parent fd9be66 commit 8fe6641
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions XSortExtension/RTSortImportCommand.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ - (void)performCommandWithInvocation:(XCSourceEditorCommandInvocation *)invocati
NSRegularExpression *importPatternBrackets = [NSRegularExpression regularExpressionWithPattern:@"(//)?\\s*#\\s*import\\s*<" IMPORT_ALLOW_CHAR "+>\\s*"
options:NSRegularExpressionCaseInsensitive
error:NULL];
NSRegularExpression *swiftImportPattern = [NSRegularExpression regularExpressionWithPattern:@"(//)?\\s*import\\s*" IMPORT_ALLOW_CHAR "+\\s*"
options:NSRegularExpressionCaseInsensitive
error:NULL];
NSRegularExpression *ifPattern = [NSRegularExpression regularExpressionWithPattern:@"\\s*#\\s*if"
options:NSRegularExpressionCaseInsensitive
error:NULL];
Expand Down Expand Up @@ -71,6 +74,13 @@ - (void)performCommandWithInvocation:(XCSourceEditorCommandInvocation *)invocati
firstImportLine = idx;
}
}
if (ifdefCount == 0 && [swiftImportPattern firstMatchInString:obj options:0 range:range] != nil) {
[bracketsImportLines addObject:[self normalizedImportLine:obj]];
[lineIndexSet addIndex:idx];
if (firstImportLine < 0) {
firstImportLine = idx;
}
}
}];
if (lineIndexSet.count > 0) {
[invocation.buffer.lines removeObjectsAtIndexes:lineIndexSet];
Expand All @@ -84,8 +94,9 @@ - (void)performCommandWithInvocation:(XCSourceEditorCommandInvocation *)invocati

[invocation.buffer.lines insertObjects:quoteImportLines
atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(firstImportLine, quoteImportLines.count)]];

[invocation.buffer.lines insertObject:@"" atIndex:firstImportLine];
if (quoteImportLines.count > 0) {
[invocation.buffer.lines insertObject:@"" atIndex:firstImportLine];
}

[invocation.buffer.lines insertObjects:bracketsImportLines
atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(firstImportLine, bracketsImportLines.count)]];
Expand Down

0 comments on commit 8fe6641

Please sign in to comment.