mirror of
https://github.com/coteditor/CotEditor.git
synced 2024-09-11 19:27:30 +03:00
Refactor syntax highlight in SyntaxParser
This commit is contained in:
parent
dd04417350
commit
13562e34af
@ -227,19 +227,16 @@ extension SyntaxParser {
|
||||
assert(!range.isEmpty)
|
||||
assert(!self.highlightParser.isEmpty)
|
||||
|
||||
let parser = self.highlightParser
|
||||
|
||||
self.highlightParseTask?.cancel()
|
||||
self.highlightParseTask = Task.detached(priority: .userInitiated) { [weak self] in
|
||||
self.highlightParseTask = Task {
|
||||
defer {
|
||||
Task { @MainActor [weak self] in
|
||||
self?.isHighlighting = false
|
||||
}
|
||||
self.isHighlighting = false
|
||||
}
|
||||
|
||||
let parser = self.highlightParser
|
||||
let highlights = try await parser.parse(string: string, range: range)
|
||||
|
||||
await self?.apply(highlights: highlights, range: range)
|
||||
self.apply(highlights: highlights, range: range)
|
||||
}
|
||||
|
||||
// make large parse cancellable
|
||||
|
@ -62,7 +62,7 @@ private struct NestableItem {
|
||||
|
||||
public struct HighlightParser: Sendable {
|
||||
|
||||
// MARK: Public Properties
|
||||
// MARK: Internal Properties
|
||||
|
||||
let extractors: [SyntaxType: [any HighlightExtractable]]
|
||||
let nestables: [NestableToken: SyntaxType]
|
||||
@ -86,8 +86,7 @@ public struct HighlightParser: Sendable {
|
||||
/// - Throws: CancellationError.
|
||||
public func parse(string: String, range: NSRange) async throws -> [Highlight] {
|
||||
|
||||
let highlightDictionary: [SyntaxType: [NSRange]] = try await withThrowingTaskGroup(of: [SyntaxType: [NSRange]].self) { group in
|
||||
|
||||
try await withThrowingTaskGroup(of: [SyntaxType: [NSRange]].self) { group in
|
||||
for (type, extractors) in self.extractors {
|
||||
for extractor in extractors {
|
||||
group.addTask { [type: try extractor.ranges(in: string, range: range)] }
|
||||
@ -95,12 +94,12 @@ public struct HighlightParser: Sendable {
|
||||
}
|
||||
group.addTask { try self.extractNestables(string: string, range: range) }
|
||||
|
||||
return try await group.reduce(into: .init()) {
|
||||
let dictionary = try await group.reduce(into: [SyntaxType: [NSRange]]()) {
|
||||
$0.merge($1, uniquingKeysWith: +)
|
||||
}
|
||||
|
||||
return try Self.sanitize(dictionary)
|
||||
}
|
||||
|
||||
return try Self.sanitize(highlightDictionary)
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user