@@ -38,6 +38,8 @@ void updateOffsets(std::vector<size_t>& offsets, NSMutableArray<NSString*>* line
38
38
@implementation ClangFormatCommand
39
39
40
40
NSUserDefaults * defaults = nil ;
41
+ NSString * kFormatSelectionCommandIdentifier = [NSString stringWithFormat: @" %@ .FormatSelection" , [[NSBundle mainBundle ] bundleIdentifier ]];
42
+ NSString * kFormatFileCommandIdentifier = [NSString stringWithFormat: @" %@ .FormatFile" , [[NSBundle mainBundle ] bundleIdentifier ]];
41
43
42
44
- (NSData *)getCustomStyle {
43
45
// First, read the regular bookmark because it could've been changed by the wrapper app.
@@ -170,10 +172,23 @@ - (void)performCommandWithInvocation:(XCSourceEditorCommandInvocation*)invocatio
170
172
updateOffsets (offsets, lines);
171
173
172
174
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 ;
177
192
}
178
193
179
194
// Calculated replacements and apply them to the input buffer.
0 commit comments