-
Notifications
You must be signed in to change notification settings - Fork 107
New Feature: Compute the cursor position #134
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0368bce
Added function to compute the cursor position as (line, column), Chan…
Eliulm fd73286
Update Sources/CodeEditTextView/STTextViewController.swift
Eliulm b4a72ec
Update Sources/CodeEditTextView/STTextViewController.swift
Eliulm ab9d27e
Update Sources/CodeEditTextView/STTextViewController.swift
Eliulm c5e72f1
Fixed SwiftLint violations, Extended CodeEditTextView DocC, moved tex…
Eliulm f061ee8
Adapted updateCursorPosition() to benefit from viewport rendering.
Eliulm ea8e462
Added functionality to compute cursor position at last line
Eliulm 4edd7fa
Moved STTextviewController to new folder. Moved cursor logic to STTex…
Eliulm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
Sources/CodeEditTextView/Controller/STTextViewController+Cursor.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// | ||
// STTextViewController+Cursor.swift | ||
// | ||
// | ||
// Created by Elias Wahl on 15.03.23. | ||
// | ||
|
||
import Foundation | ||
import AppKit | ||
|
||
extension STTextViewController { | ||
func setCursorPosition(_ position: (Int, Int)) { | ||
guard let provider = textView.textLayoutManager.textContentManager else { | ||
return | ||
} | ||
|
||
var (line, column) = position | ||
let string = textView.string | ||
if line > 0 { | ||
if string.isEmpty { | ||
// If the file is blank, automatically place the cursor in the first index. | ||
let range = NSRange(string.startIndex..<string.endIndex, in: string) | ||
if let newRange = NSTextRange(range, provider: provider) { | ||
_ = self.textView.becomeFirstResponder() | ||
self.textView.setSelectedRange(newRange) | ||
return | ||
} | ||
} | ||
|
||
string.enumerateSubstrings(in: string.startIndex..<string.endIndex) { _, lineRange, _, done in | ||
line -= 1 | ||
if line < 1 { | ||
// If `column` exceeds the line length, set cursor to the end of the line. | ||
let index = min(lineRange.upperBound, string.index(lineRange.lowerBound, offsetBy: column - 1)) | ||
if let newRange = NSTextRange(NSRange(index..<index, in: string), provider: provider) { | ||
self.textView.setSelectedRange(newRange) | ||
} | ||
done = true | ||
} else { | ||
done = false | ||
} | ||
} | ||
} | ||
} | ||
|
||
func updateCursorPosition() { | ||
guard let textLayoutManager = textView.textLayoutManager as NSTextLayoutManager?, | ||
let textContentManager = textLayoutManager.textContentManager as NSTextContentManager?, | ||
let insertionPointLocation = textLayoutManager.insertionPointLocation, | ||
let documentStartLocation = textLayoutManager.documentRange.location as NSTextLocation?, | ||
let documentEndLocation = textLayoutManager.documentRange.endLocation as NSTextLocation? | ||
else { | ||
return | ||
} | ||
|
||
let textElements = textContentManager.textElements( | ||
for: NSTextRange(location: textLayoutManager.documentRange.location, end: insertionPointLocation)!) | ||
var line = textElements.count | ||
|
||
textLayoutManager.enumerateTextSegments( | ||
in: NSTextRange(location: insertionPointLocation), | ||
type: .standard, | ||
options: [.rangeNotRequired, .upstreamAffinity] | ||
) { _, textSegmentFrame, _, _ -> Bool | ||
in | ||
var col = 1 | ||
/// If the cursor is at the end of the document: | ||
if textLayoutManager.offset(from: insertionPointLocation, to: documentEndLocation) == 0 { | ||
/// If document is empty: | ||
if textLayoutManager.offset(from: documentStartLocation, to: documentEndLocation) == 0 { | ||
self.cursorPosition.wrappedValue = (1, 1) | ||
return false | ||
} | ||
guard let cursorTextFragment = textLayoutManager.textLayoutFragment(for: textSegmentFrame.origin), | ||
let cursorTextLineFragment = cursorTextFragment.textLineFragments.last | ||
else { return false } | ||
|
||
col = cursorTextLineFragment.characterRange.length + 1 | ||
if col == 1 { line += 1 } | ||
} else { | ||
guard let cursorTextLineFragment = textLayoutManager.textLineFragment(at: insertionPointLocation) | ||
else { return false } | ||
|
||
/// +1, because we start with the first character with 1 | ||
let tempCol = cursorTextLineFragment.characterIndex(for: textSegmentFrame.origin) | ||
let result = tempCol.addingReportingOverflow(1) | ||
|
||
if !result.overflow { col = result.partialValue } | ||
/// If cursor is at end of line add 1: | ||
if cursorTextLineFragment.characterRange.length != 1 && | ||
(cursorTextLineFragment.typographicBounds.width == (textSegmentFrame.maxX + 5.0)) { | ||
col += 1 | ||
} | ||
|
||
/// If cursor is at first character of line, the current line is not being included | ||
if col == 1 { line += 1 } | ||
} | ||
|
||
self.cursorPosition.wrappedValue = (line, col) | ||
return false | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.