Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Highlighter Support #273

Merged
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
62c1731
Add Object Scaffolding, RangeStore, VisualRange Tests, Range Store Tests
thecoolwinter Oct 16, 2024
6214f2d
I've Since Realized This Is Incorrect
thecoolwinter Oct 17, 2024
f313e7c
Finalize `StyledRangeStore` and Tests
thecoolwinter Oct 25, 2024
1fa046c
Add Multi-Run Method, Fix Tests, Update Tests
thecoolwinter Oct 26, 2024
0713d2c
Begin `StyledRangeContainer`
thecoolwinter Nov 4, 2024
f09822a
First Attempt At Overlapping Ranges
thecoolwinter Nov 4, 2024
5c1a696
Finish `StyledRangeContainer`
thecoolwinter Nov 4, 2024
f01eeb0
Apply Styles, Update Init Params, Update Capture Representation
thecoolwinter Nov 6, 2024
957b859
Add VisibleRangeProviderTests
thecoolwinter Nov 8, 2024
8d3cf5a
Docs, Docs, Docs
thecoolwinter Nov 8, 2024
3e21e21
Remove .swiftpm folder
thecoolwinter Nov 8, 2024
88b928e
Lint Errors (darn whitespace)
thecoolwinter Nov 8, 2024
fec3ab1
Remove Swift 6 Fixes (will replace in future)
thecoolwinter Nov 8, 2024
f042b38
Fix Warnings
thecoolwinter Nov 8, 2024
9102955
Fix Docs Typo
thecoolwinter Nov 8, 2024
0b00fb9
Merge branch 'main' into feat/multiple-highlighters
thecoolwinter Nov 9, 2024
053a7d4
Highlight Invalidation Fix
thecoolwinter Nov 10, 2024
aaef5f2
Merge branch 'feat/multiple-highlighters' of https://github.com/theco…
thecoolwinter Nov 10, 2024
09e5cac
Rename CaptureModifiers -> CaptureModifier
thecoolwinter Nov 10, 2024
ba1dadb
Fix Compile Error
thecoolwinter Nov 11, 2024
2271437
Update CaptureModifier.swift
thecoolwinter Nov 11, 2024
b7bd8fc
Add `insert` Method To Modifiers Set
thecoolwinter Nov 11, 2024
fc55634
Typo
thecoolwinter Nov 12, 2024
327fc72
Code Style, Add Docs, Clean Tests
thecoolwinter Nov 12, 2024
72c15c9
Merge branch 'feat/multiple-highlighters' of https://github.com/theco…
thecoolwinter Nov 12, 2024
53605e4
Slight Doc Rewrite
thecoolwinter Nov 12, 2024
4c0221e
Only Invalidate Cancellations
thecoolwinter Nov 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 0 additions & 101 deletions .swiftpm/xcode/xcshareddata/xcschemes/CodeEditTextView.xcscheme

This file was deleted.

4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "3d2dc41a01f9e49d84f0a3925fb858bed64f702d",
"version" : "1.1.2"
"revision" : "671108c96644956dddcd89dd59c203dcdb36cec7",
"version" : "1.1.4"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
editorOverscroll: CGFloat = 0,
cursorPositions: Binding<[CursorPosition]>,
useThemeBackground: Bool = true,
highlightProvider: HighlightProviding? = nil,
highlightProviders: [HighlightProviding] = [TreeSitterClient()],
contentInsets: NSEdgeInsets? = nil,
isEditable: Bool = true,
isSelectable: Bool = true,
Expand All @@ -78,7 +78,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
self.wrapLines = wrapLines
self.editorOverscroll = editorOverscroll
self.cursorPositions = cursorPositions
self.highlightProvider = highlightProvider
self.highlightProviders = highlightProviders
self.contentInsets = contentInsets
self.isEditable = isEditable
self.isSelectable = isSelectable
Expand Down Expand Up @@ -132,7 +132,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
editorOverscroll: CGFloat = 0,
cursorPositions: Binding<[CursorPosition]>,
useThemeBackground: Bool = true,
highlightProvider: HighlightProviding? = nil,
highlightProviders: [HighlightProviding] = [TreeSitterClient()],
contentInsets: NSEdgeInsets? = nil,
isEditable: Bool = true,
isSelectable: Bool = true,
Expand All @@ -153,7 +153,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
self.wrapLines = wrapLines
self.editorOverscroll = editorOverscroll
self.cursorPositions = cursorPositions
self.highlightProvider = highlightProvider
self.highlightProviders = highlightProviders
self.contentInsets = contentInsets
self.isEditable = isEditable
self.isSelectable = isSelectable
Expand All @@ -179,7 +179,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
private var editorOverscroll: CGFloat
package var cursorPositions: Binding<[CursorPosition]>
private var useThemeBackground: Bool
private var highlightProvider: HighlightProviding?
private var highlightProviders: [HighlightProviding]
private var contentInsets: NSEdgeInsets?
private var isEditable: Bool
private var isSelectable: Bool
Expand All @@ -204,7 +204,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
cursorPositions: cursorPositions.wrappedValue,
editorOverscroll: editorOverscroll,
useThemeBackground: useThemeBackground,
highlightProvider: highlightProvider,
highlightProviders: highlightProviders,
contentInsets: contentInsets,
isEditable: isEditable,
isSelectable: isSelectable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,14 @@ extension TextViewController {
self.highlighter = nil
}

self.highlighter = Highlighter(
let highlighter = Highlighter(
textView: textView,
highlightProvider: highlightProvider,
theme: theme,
providers: highlightProviders,
attributeProvider: self,
language: language
)
textView.addStorageDelegate(highlighter!)
setHighlightProvider(self.highlightProvider)
}

internal func setHighlightProvider(_ highlightProvider: HighlightProviding? = nil) {
var provider: HighlightProviding?

if let highlightProvider = highlightProvider {
provider = highlightProvider
} else {
self.treeSitterClient = TreeSitterClient()
provider = self.treeSitterClient!
}

if let provider = provider {
self.highlightProvider = provider
highlighter?.setHighlightProvider(provider)
}
textView.addStorageDelegate(highlighter)
self.highlighter = highlighter
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public class TextViewController: NSViewController {
public var useThemeBackground: Bool

/// The provided highlight provider.
public var highlightProvider: HighlightProviding?
public var highlightProviders: [HighlightProviding]

/// Optional insets to offset the text view in the scroll view by.
public var contentInsets: NSEdgeInsets?
Expand Down Expand Up @@ -217,7 +217,7 @@ public class TextViewController: NSViewController {
cursorPositions: [CursorPosition],
editorOverscroll: CGFloat,
useThemeBackground: Bool,
highlightProvider: HighlightProviding?,
highlightProviders: [HighlightProviding] = [TreeSitterClient()],
contentInsets: NSEdgeInsets?,
isEditable: Bool,
isSelectable: Bool,
Expand All @@ -237,7 +237,7 @@ public class TextViewController: NSViewController {
self.cursorPositions = cursorPositions
self.editorOverscroll = editorOverscroll
self.useThemeBackground = useThemeBackground
self.highlightProvider = highlightProvider
self.highlightProviders = highlightProviders
self.contentInsets = contentInsets
self.isEditable = isEditable
self.isSelectable = isSelectable
Expand Down Expand Up @@ -307,7 +307,7 @@ public class TextViewController: NSViewController {
textView.removeStorageDelegate(highlighter)
}
highlighter = nil
highlightProvider = nil
highlightProviders.removeAll()
textCoordinators.values().forEach {
$0.destroy()
}
Expand Down
69 changes: 69 additions & 0 deletions Sources/CodeEditSourceEditor/Enums/CaptureModifiers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// CaptureModifiers.swift
// CodeEditSourceEditor
//
// Created by Khan Winter on 10/24/24.
//

// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#semanticTokenModifiers

public enum CaptureModifiers: Int8, CaseIterable, Sendable {
case declaration
case definition
case readonly
case `static`
case deprecated
case abstract
case async
case modification
case documentation
case defaultLibrary
}

extension CaptureModifiers: CustomDebugStringConvertible {
public var debugDescription: String {
switch self {
case .declaration: return "declaration"
case .definition: return "definition"
case .readonly: return "readonly"
case .static: return "static"
case .deprecated: return "deprecated"
case .abstract: return "abstract"
case .async: return "async"
case .modification: return "modification"
case .documentation: return "documentation"
case .defaultLibrary: return "defaultLibrary"
}
}
}

/// A set of capture modifiers, efficiently represented by a single integer.
public struct CaptureModifierSet: OptionSet, Equatable, Hashable, Sendable {
public let rawValue: UInt

public init(rawValue: UInt) {
self.rawValue = rawValue
}

static let declaration = CaptureModifierSet(rawValue: 1 << CaptureModifiers.declaration.rawValue)
static let definition = CaptureModifierSet(rawValue: 1 << CaptureModifiers.definition.rawValue)
static let readonly = CaptureModifierSet(rawValue: 1 << CaptureModifiers.readonly.rawValue)
static let `static` = CaptureModifierSet(rawValue: 1 << CaptureModifiers.static.rawValue)
static let deprecated = CaptureModifierSet(rawValue: 1 << CaptureModifiers.deprecated.rawValue)
static let abstract = CaptureModifierSet(rawValue: 1 << CaptureModifiers.abstract.rawValue)
static let async = CaptureModifierSet(rawValue: 1 << CaptureModifiers.async.rawValue)
static let modification = CaptureModifierSet(rawValue: 1 << CaptureModifiers.modification.rawValue)
static let documentation = CaptureModifierSet(rawValue: 1 << CaptureModifiers.documentation.rawValue)
static let defaultLibrary = CaptureModifierSet(rawValue: 1 << CaptureModifiers.defaultLibrary.rawValue)

/// All values in the set.
var values: [CaptureModifiers] {
var rawValue = self.rawValue
var values: [Int8] = []
while rawValue > 0 {
values.append(Int8(rawValue.trailingZeroBitCount))
rawValue &= ~UInt(1 << rawValue.trailingZeroBitCount)
}
return values.compactMap({ CaptureModifiers(rawValue: $0) })
}
}
Loading
Loading