### Neon Swift Package Manager Installation Source: https://github.com/chimehq/neon/blob/main/README.md This code snippet shows how to add the Neon library and its products (TreeSitterClient, RangeState) as dependencies in a Swift Package Manager project. ```swift dependencies: [ .package(url: "https://github.com/ChimeHQ/Neon", branch: "main") ], targets: [ .target( name: "MyTarget", dependencies: [ "Neon", .product(name: "TreeSitterClient", package: "Neon"), .product(name: "RangeState", package: "Neon") ] ) ] ``` -------------------------------- ### Initialize and Use TreeSitterClient for Syntax Highlighting (Swift) Source: https://github.com/chimehq/neon/blob/main/README.md Demonstrates the initialization of TreeSitterClient with a Swift language configuration and its use for retrieving syntax highlights from a given text range. It requires SwiftTreeSitter for parsing and Neon for client functionality. ```swift import Neon import SwiftTreeSitter import TreeSitterClient import TreeSitterSwift // this parser is available via SPM (see SwiftTreeSitter's README.md) // assume we have a text view available that has been loaded with some Swift source let languageConfig = try LanguageConfiguration( tree_sitter_swift(), name: "Swift" ) let clientConfig = TreeSitterClient.Configuration( languageProvider: { identifier in // look up nested languages by identifier here. If done // asynchronously, inform the client they are ready with // `languageConfigurationChanged(for:)` return nil }, contentSnapshotProvider: { [textView] length in // given a maximum needed length, produce a `ContentSnapshot` structure // that will be used to access immutable text data // this can work for any system that efficiently produce a `String` return .init(string: textView.string) }, lengthProvider: { [textView] in textView.string.utf16.count }, invalidationHandler: { set in // take action on invalidated regions of the text }, locationTransformer: { location in // optionally, use the UTF-16 location to produce a line-relative Point structure. return nil } ) let client = try TreeSitterClient( rootLanguageConfig: languageConfig, configuration: clientConfig ) let source = textView.string let provider = source.predicateTextProvider // this uses the synchronous query API, but with the `.required` mode, which will force the client // to do all processing necessary to satisfy the request. let highlights = try client.highlights(in: NSRange(0..<24), provider: provider, mode: .required)! print("highlights:", highlights) ``` -------------------------------- ### TreeSitterClient for Semantic Analysis Source: https://github.com/chimehq/neon/blob/main/Sources/Neon/Neon.docc/ChoosingComponents.md TreeSitterClient offers optional integration with Tree-sitter for performing semantic analysis on source code, commonly used with TextViewHighlighter. ```swift TreeSitterClient ``` -------------------------------- ### Perform Static Highlighting with TreeSitterClient (Swift) Source: https://github.com/chimehq/neon/blob/main/README.md Illustrates how to use TreeSitterClient for static highlighting of a given string. It involves creating a language configuration and an attribute provider to define how tokens should be styled. ```swift let languageConfig = try LanguageConfiguration( tree_sitter_swift(), name: "Swift" ) let attrProvider: TokenAttributeProvider = { token in return [.foregroundColor: NSColor.red] } // produce an AttributedString let highlightedSource = try await TreeSitterClient.highlight( string: source, attributeProvider: attrProvider, rootLanguageConfig: languageConfig, languageProvider: { _ in nil } ) ``` -------------------------------- ### Integrate Syntax Highlighting with TextViewHighlighter Source: https://github.com/chimehq/neon/blob/main/Sources/Neon/Neon.docc/ChoosingComponents.md TextViewHighlighter provides a quick way to add syntax highlighting to NS/UITextView. It handles many integration details but may reduce flexibility and performance. ```swift TextViewHighlighter ``` -------------------------------- ### TokenProvider for Style-to-Text Mapping Source: https://github.com/chimehq/neon/blob/main/Sources/Neon/Neon.docc/ChoosingComponents.md The TokenProvider type is used to determine which style applies to specific text ranges, often driven by a parser assigning semantic meaning to ranges. ```swift TokenProvider ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.