Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c33d2b7

Browse files
committedMay 19, 2020
Add command to format entire file
1 parent d8dd266 commit c33d2b7

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed
 

‎clang-format/ClangFormatCommand.mm

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ void updateOffsets(std::vector<size_t>& offsets, NSMutableArray<NSString*>* line
3838
@implementation ClangFormatCommand
3939

4040
NSUserDefaults* defaults = nil;
41+
NSString* kFormatSelectionCommandIdentifier = [NSString stringWithFormat:@"%@.FormatSelection", [[NSBundle mainBundle] bundleIdentifier]];
42+
NSString* kFormatFileCommandIdentifier = [NSString stringWithFormat:@"%@.FormatFile", [[NSBundle mainBundle] bundleIdentifier]];
4143

4244
- (NSData*)getCustomStyle {
4345
// First, read the regular bookmark because it could've been changed by the wrapper app.
@@ -170,10 +172,23 @@ - (void)performCommandWithInvocation:(XCSourceEditorCommandInvocation*)invocatio
170172
updateOffsets(offsets, lines);
171173

172174
std::vector<clang::tooling::Range> ranges;
173-
for (XCSourceTextRange* range in invocation.buffer.selections) {
174-
const size_t start = offsets[range.start.line] + range.start.column;
175-
const size_t end = offsets[range.end.line] + range.end.column;
176-
ranges.emplace_back(start, end - start);
175+
176+
if ([invocation.commandIdentifier isEqualToString:kFormatSelectionCommandIdentifier]) {
177+
for (XCSourceTextRange* range in invocation.buffer.selections) {
178+
const size_t start = offsets[range.start.line] + range.start.column;
179+
const size_t end = offsets[range.end.line] + range.end.column;
180+
ranges.emplace_back(start, end - start);
181+
}
182+
} else if ([invocation.commandIdentifier isEqualToString:kFormatFileCommandIdentifier]) {
183+
ranges.emplace_back(0, code.size());
184+
} else {
185+
completionHandler([NSError
186+
errorWithDomain:errorDomain
187+
code:0
188+
userInfo:@{
189+
NSLocalizedDescriptionKey : @"Unknown command"
190+
}]);
191+
return;
177192
}
178193

179194
// Calculated replacements and apply them to the input buffer.

‎clang-format/Info.plist

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,17 @@
3232
<key>XCSourceEditorCommandClassName</key>
3333
<string>ClangFormatCommand</string>
3434
<key>XCSourceEditorCommandIdentifier</key>
35-
<string>$(PRODUCT_BUNDLE_IDENTIFIER).ClangFormatCommand</string>
35+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).FormatSelection</string>
3636
<key>XCSourceEditorCommandName</key>
37-
<string>Format Source Code</string>
37+
<string>Format Selection</string>
38+
</dict>
39+
<dict>
40+
<key>XCSourceEditorCommandClassName</key>
41+
<string>ClangFormatCommand</string>
42+
<key>XCSourceEditorCommandIdentifier</key>
43+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).FormatFile</string>
44+
<key>XCSourceEditorCommandName</key>
45+
<string>Format Entire File</string>
3846
</dict>
3947
</array>
4048
<key>XCSourceEditorExtensionPrincipalClass</key>

0 commit comments

Comments
 (0)
Please sign in to comment.