XcodeKit 和 SourceKit

XcodeKit and SourceKit

除了我阅读的几篇文章和官方页面外,我没有使用 XcodeKit 和 SourceKit 的经验 https://developer.apple.com/documentation/xcodekit#topics https://github.com/apple/swift/tree/master/tools/SourceKit

我正在尝试了解两者之间的差异以及它们究竟能够做什么。

我认为 XcodeKit 本质上允许您向 Xcode 菜单添加命令,并且可以通过创建应用程序扩展与您打开的源代码进行交互,然后您也可以通过应用商店。 据我所知 XcodeKit 只能让你从当前的焦点文件(不是项目中的所有文件)中获取信息,然后你可以获得选定的文本等......至少那是我读到的 Xcode 9,不知道现在好点没有。

SourceKit 也与源代码交互,但看起来更强大但更底层。

我想知道的是:

https://github.com/apple/swift/blob/master/tools/SourceKit/docs/Protocol.md

谢谢

您的分析是正确的,XcodeKit 允许与编辑器 window 和其中的(选定的)文本进行交互,并且 SourceKit 具有编程语言语法知识。 AFAIK,SourceKit 非常活跃,下一件大事将是 LSP - https://github.com/apple/sourcekit-lsp

根据您的最终目标,您有以下几种选择:

  • SwiftSyntax 是 Apple 的一个新的(测试版)开源框架,用于遍历 through/modifying 文档的 AST(抽象语法树)。 GitHub repository 上提供了更多信息和示例。

  • 如前所述,SourceKitten is a Swift library for communicating with SourceKit. SourceKit is the backbone of Xcode, which is really just a glorified text editor without it. It provides everything from semantically-analyzed substructures and generated interfaces to code-completion and syntax highlighting—everything an IDE needs. It is the most encompassing option and it's not going anywhere anytime soon (as mentioned, Apple's even developing a LSP interface for it.) Shameless plug: I’ve further developed Sylvester 是一个类型化的 XPC 可用 SourceKitten (SourceKit) 接口,带有一些糖。 SKEditorOpen 请求应该为您提供足够的信息以从源文档中提取您想要的任何内容。

  • XcodeKit 公开了极其有限的 API 与源代码的交互。事实上,它只在用户调用命令的编辑器中提供打开文档的可变缓冲区。

至于您关于在编辑器中检索当前打开的文档的查询,您唯一的选择是使用 Xcode 公开的 wonderful 脚本接口。 Xcode SDEF 文件位于 /Applications/Xcode.app/Contents/Resources/Xcode.sdef。您可以使用 ScriptingBridge framework to do so from a Cocoa application. If you're using Swift, then the SwiftScripting 存储库非常有用。