ClangFormat代码格式化

时间:2021-09-12 23:10:06

下载地址:https://github.com/travisjeffery/ClangFormat-Xcode

配合Xcode自带的格式化操作,就很不错了

选中内容组合操作:

第一步:ClangFormat(control+U)

第二步:XcodeFormat(control+I)

选中文件组合操作:

第一步:ClangFormat(control+shift+U)

第二步:XcodeFormat(control+A,control+I)

修改ClangFormat.xcodeproj工程里的TRVSClangFormat.m文件的内容,实现快捷键功能(control+U和control+shift+U):

 - (void)addActioningMenuItemsToFormatMenu {
NSMenuItem *formatActiveFileItem = [[NSMenuItem alloc]
initWithTitle:NSLocalizedString(@"Format File in Focus", nil)
action:@selector(formatActiveFile)
keyEquivalent:@""];
[formatActiveFileItem setTarget:self.formatter];
[self.formatMenu addItem:formatActiveFileItem];
NSMenuItem *formatSelectedCharacters = [[NSMenuItem alloc]
initWithTitle:NSLocalizedString(@"Format Selected Text", nil)
action:@selector(formatSelectedCharacters)
keyEquivalent:@"u"]; //modified by Kenmu
[formatSelectedCharacters setKeyEquivalentModifierMask:NSControlKeyMask]; //created by Kenmu, in order to use shortcut key to access it.
[formatSelectedCharacters setTarget:self.formatter];
[self.formatMenu addItem:formatSelectedCharacters];
NSMenuItem *formatSelectedFilesItem = [[NSMenuItem alloc]
initWithTitle:NSLocalizedString(@"Format Selected Files", nil)
action:@selector(formatSelectedFiles)
keyEquivalent:@"u"]; //modified by Kenmu
[formatSelectedFilesItem setKeyEquivalentModifierMask:NSControlKeyMask | NSShiftKeyMask]; //created by, in order to use shortcut key to access it. Kenmu
[formatSelectedFilesItem setTarget:self.formatter];
[self.formatMenu addItem:formatSelectedFilesItem];
}

跟VVDocumenter规范注释生成器的安装方式一样:

下载开源工程在Xcode重新编译运行会自动安装此插件,重启Xcode就可以使用了

PS:可以使用系统偏好设置中设置键盘里针对某应用程序的快捷键,如下操作:

ClangFormat代码格式化

插件设置:

ClangFormat代码格式化

使用方式:

ClangFormat代码格式化

ClangFormat代码格式化