I need to find the linenumber the insertion point in on in an NSTextView
this code here works except for one situation
fileprivate func cursorPosition(textView:NSTextView) -> Int {
let layoutManager = textView.layoutManager
var lineNum = 0
var index = 0
var lineRange = NSRange()
let numberOfGlyphs = layoutManager!.numberOfGlyphs
let insertionPointIndex = textView.selectedRanges.first?.rangeValue.location
while index < numberOfGlyphs {
lineNum += 1
layoutManager!.lineFragmentRect(forGlyphAt: index, effectiveRange: &lineRange)
if lineRange.contains(insertionPointIndex!) { break }
index = NSMaxRange(lineRange)
}
return lineNum
}
The problem is if the caret in in the 1st postion of the LAST line, and that line is blank
then it reports the previous line. As the cursor is “technically” past the last character in the file, but none of the metrics above seem to be able to indicate that