Skip to content

Commit 63e4fa2

Browse files
Fix Editor Focus, Undo/Redo, Speed Up Cursor Labels (#1515)
1 parent e57e538 commit 63e4fa2

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
58798237292E30B90085B254 /* FeedbackView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58798230292E30B90085B254 /* FeedbackView.swift */; };
120120
58798238292E30B90085B254 /* FeedbackWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58798232292E30B90085B254 /* FeedbackWindowController.swift */; };
121121
5879824F292E78D80085B254 /* CodeFileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58798248292E78D80085B254 /* CodeFileView.swift */; };
122-
58798250292E78D80085B254 /* CodeFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58798249292E78D80085B254 /* CodeFile.swift */; };
122+
58798250292E78D80085B254 /* CodeFileDocument.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58798249292E78D80085B254 /* CodeFileDocument.swift */; };
123123
58798251292E78D80085B254 /* OtherFileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5879824B292E78D80085B254 /* OtherFileView.swift */; };
124124
58798252292E78D80085B254 /* ImageFileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5879824D292E78D80085B254 /* ImageFileView.swift */; };
125125
58798284292ED0FB0085B254 /* TerminalEmulatorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58798280292ED0FB0085B254 /* TerminalEmulatorView.swift */; };
@@ -646,7 +646,7 @@
646646
58798230292E30B90085B254 /* FeedbackView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FeedbackView.swift; sourceTree = "<group>"; };
647647
58798232292E30B90085B254 /* FeedbackWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FeedbackWindowController.swift; sourceTree = "<group>"; };
648648
58798248292E78D80085B254 /* CodeFileView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CodeFileView.swift; sourceTree = "<group>"; };
649-
58798249292E78D80085B254 /* CodeFile.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CodeFile.swift; sourceTree = "<group>"; };
649+
58798249292E78D80085B254 /* CodeFileDocument.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CodeFileDocument.swift; sourceTree = "<group>"; };
650650
5879824B292E78D80085B254 /* OtherFileView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OtherFileView.swift; sourceTree = "<group>"; };
651651
5879824D292E78D80085B254 /* ImageFileView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageFileView.swift; sourceTree = "<group>"; };
652652
58798280292ED0FB0085B254 /* TerminalEmulatorView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TerminalEmulatorView.swift; sourceTree = "<group>"; };
@@ -1624,7 +1624,7 @@
16241624
children = (
16251625
5879824C292E78D80085B254 /* Image */,
16261626
5879824A292E78D80085B254 /* Other */,
1627-
58798249292E78D80085B254 /* CodeFile.swift */,
1627+
58798249292E78D80085B254 /* CodeFileDocument.swift */,
16281628
58798248292E78D80085B254 /* CodeFileView.swift */,
16291629
6C5B63DD29C76213005454BA /* WindowCodeFileView.swift */,
16301630
);
@@ -3438,7 +3438,7 @@
34383438
6C97EBCC2978760400302F95 /* AcknowledgementsWindowController.swift in Sources */,
34393439
284DC84F2978B7B400BF2770 /* ContributorsView.swift in Sources */,
34403440
B62AEDC92A2704F3009A9F52 /* UtilityAreaTabView.swift in Sources */,
3441-
58798250292E78D80085B254 /* CodeFile.swift in Sources */,
3441+
58798250292E78D80085B254 /* CodeFileDocument.swift in Sources */,
34423442
5878DAA5291AE76700DD95A3 /* QuickOpenView.swift in Sources */,
34433443
201169D72837B2E300F92B46 /* SourceControlNavigatorView.swift in Sources */,
34443444
B6F0517929D9E3C900D72287 /* SourceControlGitView.swift in Sources */,

CodeEdit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CodeEdit/Features/CodeFile/CodeFile.swift renamed to CodeEdit/Features/CodeFile/CodeFileDocument.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import SwiftUI
1111
import UniformTypeIdentifiers
1212
import QuickLookUI
1313
import CodeEditSourceEditor
14+
import CodeEditTextView
1415
import CodeEditLanguages
1516
import Combine
1617

@@ -72,6 +73,8 @@ final class CodeFileDocument: NSDocument, ObservableObject, QLPreviewItem {
7273

7374
@Published var cursorPositions = [CursorPosition]()
7475

76+
var rangeTranslator: RangeTranslator = RangeTranslator()
77+
7578
private let isDocumentEditedSubject = PassthroughSubject<Bool, Never>()
7679

7780
/// Publisher for isDocumentEdited property
@@ -148,4 +151,33 @@ final class CodeFileDocument: NSDocument, ObservableObject, QLPreviewItem {
148151

149152
self.isDocumentEditedSubject.send(self.isDocumentEdited)
150153
}
154+
155+
class RangeTranslator: TextViewCoordinator {
156+
private weak var textViewController: TextViewController?
157+
158+
/// Returns the lines contained in the given range.
159+
/// - Parameter range: The range to use.
160+
/// - Returns: The number of lines contained by the given range. Or `0` if the text view could not be found,
161+
/// or lines could not be found for the given range.
162+
func linesInRange(_ range: NSRange) -> Int {
163+
// TODO: textView should be public, workaround for now
164+
guard let controller = textViewController,
165+
let scrollView = controller.view as? NSScrollView,
166+
let textView = scrollView.documentView as? TextView,
167+
// Find the lines at the beginning and end of the range
168+
let startTextLine = textView.layoutManager.textLineForOffset(range.location),
169+
let endTextLine = textView.layoutManager.textLineForOffset(range.upperBound) else {
170+
return 0
171+
}
172+
return (endTextLine.index - startTextLine.index) + 1
173+
}
174+
175+
func prepareCoordinator(controller: TextViewController) {
176+
self.textViewController = controller
177+
}
178+
179+
func destroy() {
180+
self.textViewController = nil
181+
}
182+
}
151183
}

CodeEdit/Features/CodeFile/CodeFileView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ struct CodeFileView: View {
114114
isEditable: isEditable,
115115
letterSpacing: letterSpacing,
116116
bracketPairHighlight: bracketPairHighlight,
117-
undoManager: undoManager
117+
undoManager: undoManager,
118+
coordinators: [codeFile.rangeTranslator]
118119
)
119120

120121
.id(codeFile.fileURL)

CodeEdit/Features/StatusBar/Views/StatusBarItems/StatusBarCursorLocationLabel.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ struct StatusBarCursorLocationLabel: View {
2424

2525
func getLines(_ range: NSRange) -> Int {
2626
if let fileDocument = file?.fileDocument {
27-
let selection = fileDocument.content[range] ?? ""
28-
let lines = selection.components(separatedBy: "\n")
29-
30-
return lines.count
27+
return fileDocument.rangeTranslator.linesInRange(range)
3128
}
3229

3330
return 0

0 commit comments

Comments
 (0)