Skip to content

Commit 3bd9c71

Browse files
Find and Replace Refactor (#311)
### Description @austincondiff has been building out the 'replace' functionality and realized the way we had structured the panel, controller, target, and model was extremely overcomplicating things. This PR is an attempt to fix that Changes: - Moves all 'business logic' into the `FindPanelViewModel` observable object. This includes clarified methods like `find`, `replace`, and `moveToNextMatch/moveToPreviousMatch`. All state has been moved to this object as well, out of a combination of both the SwiftUI view and the find view controller. - Removes the `FindPanelDelegate` type entirely. All that type was doing was passing method calls from the find panel to it's controller. Since all that logic is now in the shared view model, the controller & view can just call the necessary methods on the model. - Simplifies the `FindViewController` to only handle view/model setup and layout. - Changes the focus state variable to an enum instead of two `Bool`s. This fixes an issue where there was a moment of nothing being focused when switching between the find and replace text fields. - Removes the unnecessary `NSHostingView -> NSView -> SwiftUI View` structure, replacing it with an `NSHostingView` subclass `FindPanelHostingView` that hosts a `FindPanelView`. - Clarifies some view naming to reflect what each type does. - `FindPanel` -> `FindPanelHostingView` - `FindPanelView` search fields moved to: - `FindSearchField` - `ReplaceSearchField` ### Related Issues * #295 ### Checklist <!--- Add things that are not yet implemented above --> - [x] I read and understood the [contributing guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md) as well as the [code of conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md) - [x] The issues this PR addresses are related to each other - [x] My changes generate no new warnings - [x] My code builds and runs on my machine - [x] My changes are all related to the related issue above - [x] I documented my code ### Screenshots <!--- REQUIRED: if issue is UI related --> <!--- IMPORTANT: Fill out all required fields. Otherwise we might close this PR temporarily -->
1 parent a67b451 commit 3bd9c71

24 files changed

+699
-934
lines changed

Sources/CodeEditSourceEditor/Controller/TextViewController+Cursor.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import AppKit
1111
extension TextViewController {
1212
/// Sets new cursor positions.
1313
/// - Parameter positions: The positions to set. Lines and columns are 1-indexed.
14-
public func setCursorPositions(_ positions: [CursorPosition]) {
14+
public func setCursorPositions(_ positions: [CursorPosition], scrollToVisible: Bool = false) {
1515
if isPostingCursorNotification { return }
1616
var newSelectedRanges: [NSRange] = []
1717
for position in positions {
@@ -33,6 +33,10 @@ extension TextViewController {
3333
}
3434
}
3535
textView.selectionManager.setSelectedRanges(newSelectedRanges)
36+
37+
if scrollToVisible {
38+
textView.scrollSelectionToVisible()
39+
}
3640
}
3741

3842
/// Update the ``TextViewController/cursorPositions`` variable with new text selections from the text view.

Sources/CodeEditSourceEditor/Controller/TextViewController+FindPanelTarget.swift

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
// Created by Khan Winter on 3/16/25.
66
//
77

8-
import Foundation
8+
import AppKit
99
import CodeEditTextView
1010

1111
extension TextViewController: FindPanelTarget {
12+
var findPanelTargetView: NSView {
13+
textView
14+
}
15+
1216
func findPanelWillShow(panelHeight: CGFloat) {
1317
updateContentInsets()
1418
}
@@ -17,9 +21,8 @@ extension TextViewController: FindPanelTarget {
1721
updateContentInsets()
1822
}
1923

20-
func findPanelModeDidChange(to mode: FindPanelMode, panelHeight: CGFloat) {
21-
scrollView.contentInsets.top += mode == .replace ? panelHeight/2 : -panelHeight
22-
gutterView.frame.origin.y = -scrollView.contentInsets.top
24+
func findPanelModeDidChange(to mode: FindPanelMode) {
25+
updateContentInsets()
2326
}
2427

2528
var emphasisManager: EmphasisManager? {

Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ extension TextViewController {
192192
self.findViewController?.showFindPanel()
193193
return nil
194194
case (0, "\u{1b}"): // Escape key
195-
self.findViewController?.findPanel.dismiss()
195+
self.findViewController?.hideFindPanel()
196196
return nil
197197
case (_, _):
198198
return event

Sources/CodeEditSourceEditor/Controller/TextViewController+StyleViews.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ extension TextViewController {
9696
minimapView.scrollView.contentInsets.bottom += additionalTextInsets?.bottom ?? 0
9797

9898
// Inset the top by the find panel height
99-
let findInset = (findViewController?.isShowingFindPanel ?? false) ? findViewController?.panelHeight ?? 0 : 0
99+
let findInset: CGFloat = if findViewController?.viewModel.isShowingFindPanel ?? false {
100+
findViewController?.viewModel.panelHeight ?? 0
101+
} else {
102+
0
103+
}
100104
scrollView.contentInsets.top += findInset
101105
minimapView.scrollView.contentInsets.top += findInset
102106

Sources/CodeEditSourceEditor/Find/FindPanelDelegate.swift

-24
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// FindPanelMode.swift
3+
// CodeEditSourceEditor
4+
//
5+
// Created by Khan Winter on 4/18/25.
6+
//
7+
8+
enum FindPanelMode: CaseIterable {
9+
case find
10+
case replace
11+
12+
var displayName: String {
13+
switch self {
14+
case .find:
15+
return "Find"
16+
case .replace:
17+
return "Replace"
18+
}
19+
}
20+
}

Sources/CodeEditSourceEditor/Find/FindPanelTarget.swift

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
// Created by Khan Winter on 3/10/25.
66
//
77

8-
import Foundation
8+
import AppKit
99
import CodeEditTextView
1010

1111
protocol FindPanelTarget: AnyObject {
1212
var emphasisManager: EmphasisManager? { get }
1313
var text: String { get }
14+
var findPanelTargetView: NSView { get }
1415

1516
var cursorPositions: [CursorPosition] { get }
16-
func setCursorPositions(_ positions: [CursorPosition])
17+
func setCursorPositions(_ positions: [CursorPosition], scrollToVisible: Bool)
1718
func updateCursorPosition()
1819

1920
func findPanelWillShow(panelHeight: CGFloat)
2021
func findPanelWillHide(panelHeight: CGFloat)
21-
func findPanelModeDidChange(to mode: FindPanelMode, panelHeight: CGFloat)
22+
func findPanelModeDidChange(to mode: FindPanelMode)
2223
}

Sources/CodeEditSourceEditor/Find/FindViewController+FindPanelDelegate.swift

-221
This file was deleted.

0 commit comments

Comments
 (0)