Sapling/Sources/SaplingEditor/EditorActiveLineTracker.swift

19 lines
786 B
Swift
Raw Normal View History

import Foundation
public enum EditorActiveLineTracker {
public static func lines(from source: String, activeLineIndex: Int) -> [EditorLine] {
DocumentLineIndex(source: source).editorLines(activeLineIndex: activeLineIndex)
}
public static func lineIndex(containing location: Int, in source: String) -> Int {
DocumentLineIndex(source: source).lineIndex(containing: location)
}
public static func clampedSelection(_ selection: EditorSelection, in source: String) -> EditorSelection {
let sourceLength = source.utf16.count
let location = max(0, min(selection.location, sourceLength))
let length = max(0, min(selection.length, sourceLength - location))
return EditorSelection(location: location, length: length)
}
}