Skip to content

Commit

Permalink
static highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed May 14, 2024
1 parent 26f53e5 commit 709683a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,27 @@ let highlights = try client.highlights(in: NSRange(0..<24), provider: provider,
print("highlights:", highlights)
```

TreeSitterClient can also perform static highlighting. This can be handy for applying style to a string.

```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 }
)
```

## Contributing and Collaboration

I would love to hear from you! Issues or pull requests work great. A [Discord server][discord] is also available for live help, but I have a strong bias towards answering in the form of documentation.
Expand Down
2 changes: 2 additions & 0 deletions Sources/Neon/PlatformTextSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import RangeState
import AppKit

public typealias TextView = NSTextView
typealias PlatformColor = NSColor
#elseif os(iOS) || os(visionOS)
import UIKit

public typealias TextView = UITextView
typealias PlatformColor = UIColor
#endif

#if os(macOS) || os(iOS) || os(visionOS)
Expand Down
42 changes: 42 additions & 0 deletions Sources/Neon/TreeSitterClient+Neon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,45 @@ extension TextViewSystemInterface {
}
}
#endif

@available(macOS 12, *)
extension TreeSitterClient {
/// Highlight an input string.
public static func highlight(
string: String,
attributeProvider: TokenAttributeProvider,
rootLanguageConfig: LanguageConfiguration,
languageProvider: @escaping LanguageLayer.LanguageProvider
) async throws -> AttributedString {

Check failure on line 78 in Sources/Neon/TreeSitterClient+Neon.swift

View workflow job for this annotation

GitHub Actions / Test (platform=iOS Simulator,name=iPhone 15)

'AttributedString' is only available in iOS 15 or newer

Check failure on line 78 in Sources/Neon/TreeSitterClient+Neon.swift

View workflow job for this annotation

GitHub Actions / Test (platform=tvOS Simulator,name=Apple TV)

'AttributedString' is only available in tvOS 15 or newer

Check failure on line 78 in Sources/Neon/TreeSitterClient+Neon.swift

View workflow job for this annotation

GitHub Actions / Test (platform=tvOS Simulator,name=Apple TV)

'AttributedString' is only available in tvOS 15 or newer

Check failure on line 78 in Sources/Neon/TreeSitterClient+Neon.swift

View workflow job for this annotation

GitHub Actions / Test (platform=watchOS Simulator,name=Apple Watch Series 6 (40mm))

'AttributedString' is only available in watchOS 8 or newer
let content = LanguageLayer.Content(string: string)
let length = string.utf16.count

let client = try TreeSitterClient(
rootLanguageConfig: rootLanguageConfig,
configuration: Configuration(
languageProvider: languageProvider,
contentProvider: { _ in content },
lengthProvider: { length },
invalidationHandler: { _ in },
locationTransformer: { _ in nil }
)
)

client.didChangeContent(in: NSRange(0..<0), delta: length)

let ranges = try await client.highlights(in: NSRange(0..<length), provider: content.textProvider)

var attributedString = AttributedString(stringLiteral: string)

Check failure on line 97 in Sources/Neon/TreeSitterClient+Neon.swift

View workflow job for this annotation

GitHub Actions / Test (platform=tvOS Simulator,name=Apple TV)

'AttributedString' is only available in tvOS 15 or newer

Check failure on line 97 in Sources/Neon/TreeSitterClient+Neon.swift

View workflow job for this annotation

GitHub Actions / Test (platform=tvOS Simulator,name=Apple TV)

'init(stringLiteral:)' is only available in tvOS 15 or newer

for range in ranges {
let token = Token(name: range.name, range: range.range)
let attrs = attributeProvider(token)
guard let strRange = Range<AttributedString.Index>(token.range, in: attributedString) else { continue }

Check failure on line 102 in Sources/Neon/TreeSitterClient+Neon.swift

View workflow job for this annotation

GitHub Actions / Test (platform=tvOS Simulator,name=Apple TV)

'AttributedString' is only available in tvOS 15 or newer

Check failure on line 102 in Sources/Neon/TreeSitterClient+Neon.swift

View workflow job for this annotation

GitHub Actions / Test (platform=tvOS Simulator,name=Apple TV)

'init(_:in:)' is only available in tvOS 15 or newer

attributedString[strRange].foregroundColor = attrs[.foregroundColor] as? PlatformColor

Check failure on line 104 in Sources/Neon/TreeSitterClient+Neon.swift

View workflow job for this annotation

GitHub Actions / Test (platform=tvOS Simulator,name=Apple TV)

cannot find type 'PlatformColor' in scope
}

return attributedString
}
}

3 changes: 0 additions & 3 deletions Tests/NeonTests/TextViewSystemInterfaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import XCTest
#if os(macOS)
import AppKit

typealias PlatformColor = NSColor

extension NSTextView {
var text: String {
get { return string }
Expand All @@ -31,7 +29,6 @@ extension NSTextView {
#elseif os(iOS)
import UIKit

typealias PlatformColor = UIColor
#endif

//final class TextViewSystemInterfaceTests: XCTestCase {
Expand Down

0 comments on commit 709683a

Please sign in to comment.