Textkit2 NSTextView get cursor coordinate

I need to render some view at cursor position, how can I get the current coordinate of cursor?

var location = NSRange(location: textView.selectedRange().location, length: 0)

// the following is not available
textLayoutManager.boundingRect(forGlyphRange: location, in: textContainer)

can we use Textkit2 in production? I think there are lots of methods missing in NSTextLayoutManager while available in NSLayoutManager.

Use NSTextView.textSelections, then use NSTextLayoutManager to get the position of a first selection NSTextRange and frame

let selectionTextLocation = textSelections.flatMap(\.textRanges)[0].location

next find a frame

textLayoutManager.enumerateSegments(in: NSTextRange(location: selectionTextLocation), type: .standard, options: .upstreamAffinity) { _, segmentFrame, baselinePosition, _ in
  // use segmentFrame and baselinePosition to calculate insertion point location in NSTextContainer
}
Textkit2 NSTextView get cursor coordinate
 
 
Q