动手自己写一个 xcode 插件(Xcode Source Editor Extensions)附源码

动手自己写一个 xcode插件  (暂时需要注释功能,所以简单的写一个注释插件)

-----Xcode Source Editor Extensions-----


插件是可以在Xcode的Preferences的Key Bindings中设置快捷键的


-----------------------------------------------------------------------

开始:

1:创建Project,并且选中OS X - Application - Cocoa Application,

就像穿件一个iOS 项目和其他插件(today,share,action.....)等等一样的关系,

2:添加Target,选择OS X - Application Extensions - Xcode Source Editor Extension

这样就创建了插件,并生成了两个(默认名称为SourceEditorCommand,SourceEditorExtension)

这里名称是可以改的,改了之后只需要在Target的info.plist中修改

XCSourceEditorCommandClassName 对应 SourceEditorCommand

XCSourceEditorExtensionPrincipalClass 对应 SourceEditorExtension

我们看到XCSourceEditorCommandDefinitions这个数组

这个地方就是这个插件所有的功能配置的地方,使用XCSourceEditorCommandIdentifier区分

我们主要撰写的代码在SourceEditorCommand中,需要运行插件的时候,会回调

- (void)performCommandWithInvocation:(XCSourceEditorCommandInvocation *)invocation completionHandler:(void (^)(NSError * _Nullable nilOrError))completionHandler

{

completionHandler(nil);

}

这个XCSourceEditorCommandInvocation *invocation中就有我们需要的全部信息,选择了哪一行,那一列,内容,等等等等


运行效果如上,选择两个功能的时候,使用

NSString *identifier = invocation.commandIdentifier;

来区分identifier是哪一个XCSourceEditorCommandIdentifier以调取不同的功能代码


-----------------------------------------------------------------------

Demo 下载地址(Github):

WTxcodeToolBox (注释功能)https://github.com/wintelsui/WTxcodeToolBox.git

其他项目-插件源码

App Store:https://itunes.apple.com/cn/app/myutils/id1457192526?mt=12

Github:  https://github.com/wintelsui/MyUtilsForXcode

最近用xcode 8,

1:CommentStatement 插件

包含功能:

(1):CommentStatement 注释掉代码(即xcode原有的Comment Statement功能,因为xcode 8有bug 所以...)等效于 "command + /"; Screenshot

(2):DocumentAdd 添加注释 (超级简单的山寨VVDocumenter)...暂时只能识别Objective-C Screenshot


-----------------------------------------------------------------------

ps:

插件显示,需要开发者证书(免费 or 付费)

Extensions for Xcode 8

据说10.11需要打开terminal,然后执行命令sudo /usr/libexec/xpccachectl 一次(建议重启)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容