Skip to content

API: convert offset/location to TextLocation and vice versa  #137

@rebornix

Description

@rebornix

Is your feature request related to a problem? Please describe.

TextView currently supports converting offset (Int) to line/column (TextLocation). With this API we could build our own Line/Column status, but if we have a Line/Column there is no API to help convert it to offset.

Describe the solution you'd like

We can have an override of textLocation API so embedders can covert between offset and line/column with the same method.

    public func textLocation(at location: Int) -> TextLocation? {
        if let linePosition = textInputView.linePosition(at: location) {
            return TextLocation(linePosition)
        } else {
            return nil
        }
    }
    
+    public func textLocation(at textLocation: TextLocation) -> Int? {
+        let lineIndex = textLocation.lineNumber
+        
+        guard lineIndex >= 0 && lineIndex < textInputView.lineManager.lineCount else {
+            return nil
+        }
+
+        let line = textInputView.lineManager.line(atRow: lineIndex)
+        return line.location + textLocation.column
+    }

Describe alternatives you've considered

Without this we would need to either calculate line ends ourselves or introduce "proxy functions" (like GoToLine).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions