Skip to content

Commit 88b8296

Browse files
author
Lukas Pistrol
authored
Merge branch 'CodeEditApp:main' into static-libs
2 parents 85c4fd8 + 0458bd1 commit 88b8296

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

Sources/CodeEditTextView/CodeEditTextView.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
2626
theme: Binding<EditorTheme>,
2727
font: Binding<NSFont>,
2828
tabWidth: Binding<Int>,
29-
lineHeight: Binding<Double>
29+
lineHeight: Binding<Double>,
30+
cursorPosition: Published<(Int, Int)>.Publisher? = nil
3031
) {
3132
self._text = text
3233
self.language = language
3334
self._theme = theme
3435
self._font = font
3536
self._tabWidth = tabWidth
3637
self._lineHeight = lineHeight
38+
self.cursorPosition = cursorPosition
3739
}
3840

3941
@Binding private var text: String
@@ -42,6 +44,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
4244
@Binding private var font: NSFont
4345
@Binding private var tabWidth: Int
4446
@Binding private var lineHeight: Double
47+
private var cursorPosition: Published<(Int, Int)>.Publisher?
4548

4649
public typealias NSViewControllerType = STTextViewController
4750

@@ -51,7 +54,8 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
5154
language: language,
5255
font: font,
5356
theme: theme,
54-
tabWidth: tabWidth
57+
tabWidth: tabWidth,
58+
cursorPosition: cursorPosition
5559
)
5660
controller.lineHeightMultiple = lineHeight
5761
return controller

Sources/CodeEditTextView/STTextViewController.swift

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import AppKit
99
import SwiftUI
10+
import Combine
1011
import STTextView
1112
import SwiftTreeSitter
1213
import CodeEditLanguages
@@ -48,13 +49,20 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
4849

4950
// MARK: Init
5051

51-
public init(text: Binding<String>, language: CodeLanguage, font: NSFont, theme: EditorTheme, tabWidth: Int) {
52+
public init(
53+
text: Binding<String>,
54+
language: CodeLanguage,
55+
font: NSFont,
56+
theme: EditorTheme,
57+
tabWidth: Int,
58+
cursorPosition: Published<(Int, Int)>.Publisher? = nil
59+
) {
5260
self.text = text
5361
self.language = language
5462
self.font = font
5563
self.theme = theme
5664
self.tabWidth = tabWidth
57-
65+
self.cursorPosition = cursorPosition
5866
super.init(nibName: nil, bundle: nil)
5967
}
6068

@@ -125,6 +133,10 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
125133
}
126134

127135
setUpHighlighting()
136+
137+
self.cursorPositionCancellable = self.cursorPosition?.sink(receiveValue: { value in
138+
self.setCursorPosition(value)
139+
})
128140
}
129141

130142
internal func setUpHighlighting() {
@@ -224,6 +236,35 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
224236
keyIsDown = false
225237
}
226238

239+
// MARK: Cursor Position
240+
241+
private var cursorPosition: Published<(Int, Int)>.Publisher?
242+
private var cursorPositionCancellable: AnyCancellable?
243+
244+
private func setCursorPosition(_ position: (Int, Int)) {
245+
guard let provider = textView.textLayoutManager.textContentManager else {
246+
return
247+
}
248+
249+
var (line, column) = position
250+
let string = textView.string
251+
if line > 0 {
252+
string.enumerateSubstrings(in: string.startIndex..<string.endIndex) { _, lineRange, _, done in
253+
line -= 1
254+
if line < 1 {
255+
// If `column` exceeds the line length, set cursor to the end of the line.
256+
let index = min(lineRange.upperBound, string.index(lineRange.lowerBound, offsetBy: column - 1))
257+
if let newRange = NSTextRange(NSRange(index..<index, in: string), provider: provider) {
258+
self.textView.setSelectedRange(newRange)
259+
}
260+
done = true
261+
} else {
262+
done = false
263+
}
264+
}
265+
}
266+
}
267+
227268
deinit {
228269
textView = nil
229270
highlighter = nil

0 commit comments

Comments
 (0)