-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathIndentationLineFoldProvider.swift
More file actions
34 lines (29 loc) · 1.02 KB
/
IndentationLineFoldProvider.swift
File metadata and controls
34 lines (29 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// IndentationLineFoldProvider.swift
// CodeEditSourceEditor
//
// Created by Khan Winter on 5/8/25.
//
import AppKit
import CodeEditTextView
final class IndentationLineFoldProvider: LineFoldProvider {
func foldLevelAtLine(_ lineNumber: Int, layoutManager: TextLayoutManager, textStorage: NSTextStorage) -> Int? {
guard let linePosition = layoutManager.textLineForIndex(lineNumber),
let indentLevel = indentLevelForPosition(linePosition, textStorage: textStorage) else {
return nil
}
return indentLevel
}
private func indentLevelForPosition(
_ position: TextLineStorage<TextLine>.TextLinePosition,
textStorage: NSTextStorage
) -> Int? {
guard let substring = textStorage.substring(from: position.range) else {
return nil
}
return substring.utf16 // Keep NSString units
.enumerated()
.first(where: { UnicodeScalar($0.element)?.properties.isWhitespace != true })?
.offset
}
}