SourceKitten:一个可爱的小框架与命令行工具用于与SourceKit交互

网友投稿 1280 2022-10-13

SourceKitten:一个可爱的小框架与命令行工具用于与SourceKit交互

SourceKitten:一个可爱的小框架与命令行工具用于与SourceKit交互

SourceKitten

An adorable little framework and command line tool for interacting with SourceKit.

SourceKitten links and communicates with sourcekitd.framework to parse the Swift AST, extract comment docs for Swift or Objective-C projects, get syntax data for a Swift file and lots more!

Installation

Building SourceKitten requires Xcode 10.2 or later or a Swift 5.0 toolchain or later with the Swift Package Manager.

SourceKitten typically supports previous versions of SourceKit.

Homebrew

Run brew install sourcekitten.

Swift Package Manager

Run swift build in the root directory of this project.

Xcode (via Make)

Run make install in the root directory of this project.

Package

Download and open SourceKitten.pkg from the releases tab.

Command Line Usage

Once SourceKitten is installed, you may use it from the command line.

$ sourcekitten helpAvailable commands: complete Generate code completion options doc Print Swift or Objective-C docs as jsON format Format Swift file help Display general or command-specific help index Index Swift file and print as JSON request Run a raw sourcekit request structure Print Swift structure information as JSON syntax Print Swift syntax information as JSON version Display the current version of SourceKitten

How is SourceKit resolved?

SourceKitten searches for SourceKit in the following order:

$XCODE_DEFAULT_TOOLCHAIN_OVERRIDE$TOOLCHAIN_DIRxcrun -find swift/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain~/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain~/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain

On Linux, SourceKit is expected to be located in /usr/lib/libsourcekitdInProc.so or specified by the LINUX_SOURCEKIT_LIB_PATH environment variable.

Projects Built With SourceKitten

SwiftLint: A tool to enforce Swift style and conventions.Jazzy: Soulful docs for Swift & Objective-C.Sourcery: Meta-programming for Swift, stop writing boilerplate code.SwiftyMocky: Framework for mock generation.SourceKittenDaemon: Swift Auto Completions for any Text Editor.SourceDocs: Command Line Tool that generates Markdown documentation from inline source code comments.Cuckoo: First boilerplate-free mocking framework for Swift.IBAnalyzer: Find common xib and storyboard-related problems without running your app or writing unit tests.Taylor: Measure Swift code metrics and get reports in Xcode, Jenkins and other CI platforms.

See Morehttps://github.com/appsquickly/TyphoonSwifthttps://github.com/banjun/bansanhttps://github.com/Beaver/BeaverCodeGenhttps://github.com/Ben-G/Meethttps://github.com/dfreemanRIIS/ETAMockhttps://github.com/dostu/SwiftMetrichttps://github.com/draven-archive/MetaKithttps://github.com/geosor/SwiftVisualizerhttps://github.com/godfreynolan/AgileSwiftTsthttps://github.com/godfreynolan/CodeCraftsmanhttps://github.com/ilyapuchka/dipgenhttps://github.com/ilyapuchka/SourceKittenEditorExtensionhttps://github.com/interstateone/Unusedhttps://github.com/ishkawa/DIKithttps://github.com/IvanovGeorge/FBAuthhttps://github.com/jmpg93/NavigatorSwifthttps://github.com/jpmartha/Pancakehttps://github.com/jpweber/Kontexthttps://github.com/KenichiroSato/CatDogTubehttps://github.com/klundberg/grifthttps://github.com/kovtun1/DependenciesGraphhttps://github.com/lvsti/Bridgecrafthttps://github.com/maralla/completor-swifthttps://github.com/marcsnts/Shopify-Winter18-Technicalhttps://github.com/momentumworks/Formulahttps://github.com/nevil/UNClassDiagramhttps://github.com/norio-nomura/LinuxSupportForXcodehttps://github.com/paulofaria/swift-package-crawler-datahttps://github.com/rajat-explorer/Github-Profilerhttps://github.com/rockbruno/swiftshieldhttps://github.com/S2dentik/Enlighthttps://github.com/seanhenry/SwiftMockGeneratorForXcodehttps://github.com/sharplet/swiftagshttps://github.com/siejkowski/Croupierhttps://github.com/SwiftKit/CuckooGeneratorhttps://github.com/SwiftKit/Torchhttps://github.com/SwiftTools/SwiftFelisCatushttps://github.com/swizzlr/luxhttps://github.com/tid-kijyun/XcodeSourceEditorExtension-ProtocolImplementationhttps://github.com/tjarratt/fake4swifthttps://github.com/tkohout/Geniehttps://github.com/tomquist/MagicMirrorhttps://github.com/TurfDb/TurfGenhttps://github.com/vadimue/AwesomeWeatherhttps://github.com/yonaskolb/Beakhttps://github.com/zenzz/vs-swifter-serverhttps://github.com/zenzz/zxxswifter-serverhttps://github.com/scribd/Weaverhttps://github.com/Nonchalant/FactoryProvider

Complete

Running sourcekitten complete --file file.swift --offset 123 or sourcekitten complete --text "0." --offset 2 will print out code completion options for the offset in the file/text provided:

[{ "descriptionKey" : "advancedBy(n: Distance)", "associatedUSRs" : "s:FSi10advancedByFSiFSiSi s:FPSs21RandomAccessIndexType10advancedByuRq_S__Fq_Fqq_Ss16ForwardIndexType8Distanceq_ s:FPSs16ForwardIndexType10advancedByuRq_S__Fq_Fqq_S_8Distanceq_ s:FPSs10Strideable10advancedByuRq_S__Fq_Fqq_S_6Strideq_ s:FPSs11_Strideable10advancedByuRq_S__Fq_Fqq_S_6Strideq_", "kind" : "source.lang.swift.decl.function.method.instance", "sourcetext" : "advancedBy(<#T##n: Distance##Distance#>)", "context" : "source.codecompletion.context.thisclass", "typeName" : "Int", "moduleName" : "Swift", "name" : "advancedBy(n: Distance)"},{ "descriptionKey" : "advancedBy(n: Self.Distance, limit: Self)", "associatedUSRs" : "s:FeRq_Ss21RandomAccessIndexType_SsS_10advancedByuRq_S__Fq_FTqq_Ss16ForwardIndexType8Distance5limitq__q_", "kind" : "source.lang.swift.decl.function.method.instance", "sourcetext" : "advancedBy(<#T##n: Self.Distance##Self.Distance#>, limit: <#T##Self#>)", "context" : "source.codecompletion.context.superclass", "typeName" : "Self", "moduleName" : "Swift", "name" : "advancedBy(n: Self.Distance, limit: Self)"},...]

To use the iOS SDK, pass -sdk and -target arguments preceded by --:

sourcekitten complete --text "import UIKit ; UIColor." --offset 22 -- -target arm64-apple-ios9.0 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.0.sdk

Doc

Running sourcekitten doc will pass all arguments after what is parsed to xcodebuild (or directly to the compiler, SourceKit/clang, in the --single-file mode).

Example usage

sourcekitten doc -- -workspace SourceKitten.xcworkspace -scheme SourceKittenFrameworksourcekitten doc --single-file file.swift -- -j4 file.swiftsourcekitten doc --module-name Alamofire -- -project Alamofire.xcodeprojsourcekitten doc -- -workspace Haneke.xcworkspace -scheme Hanekesourcekitten doc --objc Realm/Realm.h -- -x objective-c -isysroot $(xcrun --show-sdk-path) -I $(pwd)

Structure

Running sourcekitten structure --file file.swift or sourcekitten structure --text "struct A { func b() {} }" will return a JSON array of structure information:

{ "key.substructure" : [ { "key.kind" : "source.lang.swift.decl.struct", "key.offset" : 0, "key.nameoffset" : 7, "key.namelength" : 1, "key.bodyoffset" : 10, "key.bodylength" : 13, "key.length" : 24, "key.substructure" : [ { "key.kind" : "source.lang.swift.decl.function.method.instance", "key.offset" : 11, "key.nameoffset" : 16, "key.namelength" : 3, "key.bodyoffset" : 21, "key.bodylength" : 0, "key.length" : 11, "key.substructure" : [ ], "key.name" : "b()" } ], "key.name" : "A" } ], "key.offset" : 0, "key.diagnostic_stage" : "source.diagnostic.stage.swift.parse", "key.length" : 24}

Syntax

Running sourcekitten syntax --file file.swift or sourcekitten syntax --text "import Foundation // Hello World" will return a JSON array of syntax highlighting information:

[ { "offset" : 0, "length" : 6, "type" : "source.lang.swift.syntaxtype.keyword" }, { "offset" : 7, "length" : 10, "type" : "source.lang.swift.syntaxtype.identifier" }, { "offset" : 18, "length" : 14, "type" : "source.lang.swift.syntaxtype.comment" }]

Request

Running sourcekitten request --yaml [FILE|TEXT] will execute a sourcekit request with the given yaml. For example:

key.request: source.request.cursorinfokey.sourcefile: "/tmp/foo.swift"key.offset: 8key.compilerargs: - "/tmp/foo.swift"

SourceKittenFramework

Most of the functionality of the sourcekitten command line tool is actually encapsulated in a framework named SourceKittenFramework.

If you’re interested in using SourceKitten as part of another tool, or perhaps extending its functionality, take a look at the SourceKittenFramework source code to see if the API fits your needs.

Note: SourceKitten is written entirely in Swift, and the SourceKittenFramework API is not designed to interface with Objective-C.

License

MIT licensed.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:自学鸿蒙应用开发(12)- Checkbox
下一篇:shell脚本监控某些文件夹下文件数量
相关文章

 发表评论

暂时没有评论,来抢沙发吧~