Skip to content

Commit 2619cb9

Browse files
Shift-Click to Extend Selection (#45)
1 parent eb1d382 commit 2619cb9

15 files changed

+181
-93
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// NSRange.swift
3+
// CodeEditTextView
4+
//
5+
// Created by Khan Winter on 8/20/24.
6+
//
7+
8+
import Foundation
9+
10+
extension NSRange {
11+
@inline(__always)
12+
init(start: Int, end: Int) {
13+
self.init(location: start, length: end - start)
14+
}
15+
}

Sources/CodeEditTextView/Extensions/NSTextStorage+getLine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extension NSString {
1414
var contentsEnd: Int = NSNotFound
1515
self.getLineStart(nil, end: &end, contentsEnd: &contentsEnd, for: range)
1616
if end != NSNotFound && contentsEnd != NSNotFound && end != contentsEnd {
17-
return NSRange(location: contentsEnd, length: end - contentsEnd)
17+
return NSRange(start: contentsEnd, end: end)
1818
} else {
1919
return nil
2020
}

Sources/CodeEditTextView/MarkedTextManager/MarkedTextManager.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,14 @@ class MarkedTextManager {
4949
/// - textSelections: The current text selections.
5050
func updateMarkedRanges(insertLength: Int, textSelections: [NSRange]) {
5151
var cumulativeExistingDiff = 0
52-
let lengthDiff = insertLength
5352
var newRanges = [NSRange]()
5453
let ranges: [NSRange] = if markedRanges.isEmpty {
5554
textSelections.sorted(by: { $0.location < $1.location })
5655
} else {
5756
markedRanges.sorted(by: { $0.location < $1.location })
5857
}
5958

60-
for (idx, range) in ranges.enumerated() {
59+
for range in ranges {
6160
newRanges.append(NSRange(location: range.location + cumulativeExistingDiff, length: insertLength))
6261
cumulativeExistingDiff += insertLength - range.length
6362
}

Sources/CodeEditTextView/TextLayoutManager/TextLayoutManager+Edits.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extension TextLayoutManager: NSTextStorageDelegate {
4343
if !string.isEmpty {
4444
var index = 0
4545
while let nextLine = (string as NSString).getNextLine(startingAt: index) {
46-
let lineRange = NSRange(location: index, length: nextLine.max - index)
46+
let lineRange = NSRange(start: index, end: nextLine.max)
4747
applyLineInsert((string as NSString).substring(with: lineRange) as NSString, at: range.location + index)
4848
index = nextLine.max
4949
}

Sources/CodeEditTextView/TextLayoutManager/TextLayoutManager+Public.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,7 @@ extension TextLayoutManager {
188188
let originalHeight = lineStorage.height
189189

190190
for linePosition in lineStorage.linesInRange(
191-
NSRange(
192-
location: startingLinePosition.range.location,
193-
length: linePosition.range.max - startingLinePosition.range.location
194-
)
191+
NSRange(start: startingLinePosition.range.location, end: linePosition.range.max)
195192
) {
196193
let height = ensureLayoutFor(position: linePosition)
197194
if height != linePosition.height {

Sources/CodeEditTextView/TextLine/Typesetter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ final class Typesetter {
7979
constrainingWidth: maxWidth
8080
)
8181
let lineFragment = typesetLine(
82-
range: NSRange(location: startIndex, length: lineBreak - startIndex),
82+
range: NSRange(start: startIndex, end: lineBreak),
8383
lineHeightMultiplier: lineHeightMultiplier
8484
)
8585
lines.append(.init(
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Destination.swift
3+
// CodeEditTextView
4+
//
5+
// Created by Khan Winter on 8/20/24.
6+
//
7+
8+
public extension TextSelectionManager {
9+
enum Destination {
10+
case character
11+
case word
12+
case line
13+
case visualLine
14+
case page
15+
case document
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Direction.swift
3+
// CodeEditTextView
4+
//
5+
// Created by Khan Winter on 8/20/24.
6+
//
7+
8+
public extension TextSelectionManager {
9+
enum Direction {
10+
case up
11+
case down
12+
case forward
13+
case backward
14+
}
15+
}

Sources/CodeEditTextView/TextSelectionManager/SelectionManipulation/SelectionManipulation+Horizontal.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ package extension TextSelectionManager {
4242
return extendSelectionVisualLine(string: string, from: offset, delta: delta)
4343
case .document:
4444
if delta > 0 {
45-
return NSRange(location: offset, length: string.length - offset)
45+
return NSRange(start: offset, end: string.length)
4646
} else {
4747
return NSRange(location: 0, length: offset)
4848
}
@@ -194,8 +194,8 @@ package extension TextSelectionManager {
194194
delta: Int
195195
) -> NSRange {
196196
var foundRange = NSRange(
197-
location: min(lineBound, offset),
198-
length: max(lineBound, offset) - min(lineBound, offset)
197+
start: min(lineBound, offset),
198+
end: max(lineBound, offset)
199199
)
200200
let originalFoundRange = foundRange
201201

0 commit comments

Comments
 (0)