Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: convert row/column to offset #138

Merged
merged 4 commits into from
Aug 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Sources/Runestone/TextView/TextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,21 @@ open class TextView: UIScrollView {
}
}

/// Returns the character location at the specified row and column.
/// - Parameter textLocation: The row and column in the text.
/// - Returns: The location if the input row and column could be found in the text, otherwise nil.
public func location(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)
guard textLocation.column >= 0 && textLocation.column <= line.data.totalLength else {
return nil
}
return line.location + textLocation.column
}

/// Sets the language mode on a background thread.
///
/// - Parameters:
Expand Down