Skip to content

Commit 3b8acc9

Browse files
committed
Support open file from search result list
1 parent e8078de commit 3b8acc9

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

CodeEdit/Documents/WorkspaceDocument.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ extension WorkspaceDocument {
179179
class SearchState: ObservableObject {
180180

181181
var workspace: WorkspaceDocument
182-
@Published var searchResult: [URL: [AttributedString]] = [:]
182+
@Published var searchResult: [WorkspaceClient.FileItem: [AttributedString]] = [:]
183183

184184
init(_ workspace: WorkspaceDocument) {
185185
self.workspace = workspace
@@ -196,8 +196,10 @@ extension WorkspaceDocument {
196196
.skipsPackageDescendants
197197
])
198198
if let filePaths = enumerator?.allObjects as? [URL] {
199-
filePaths.forEach { fileURL in
200-
let data = try? String(contentsOf: fileURL)
199+
filePaths.map { url in
200+
WorkspaceClient.FileItem(url: url, children: nil)
201+
}.forEach { fileItem in
202+
let data = try? String(contentsOf: fileItem.url)
201203
data?.split(separator: "\n").forEach { line in
202204
let noSpaceLine = line.trimmingCharacters(in: .whitespaces)
203205
if noSpaceLine.contains(text) {
@@ -213,9 +215,9 @@ extension WorkspaceDocument {
213215
attributedString.append(
214216
AttributedString(String(noSpaceLine[range.upperBound..<noSpaceLine.endIndex]))
215217
)
216-
var lines = self.searchResult[fileURL] ?? []
218+
var lines = self.searchResult[fileItem] ?? []
217219
lines.append(attributedString)
218-
self.searchResult[fileURL] = lines
220+
self.searchResult[fileItem] = lines
219221
}
220222
}
221223
}

CodeEdit/SideBar/Search/FindResultList.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
//
77

88
import SwiftUI
9+
import WorkspaceClient
910

1011
struct FindResultList: View {
1112
@ObservedObject var state: WorkspaceDocument.SearchState
1213
@State var selectedResult: AttributedString?
1314

1415
var body: some View {
1516
List(selection: $selectedResult) {
16-
ForEach(Array(state.searchResult.keys), id: \.self) { fileURL in
17+
ForEach(Array(state.searchResult.keys), id: \.self) { (file: WorkspaceClient.FileItem) in
1718
Section {
18-
ForEach(state.searchResult[fileURL] ?? [], id: \.self) { line in
19+
ForEach(state.searchResult[file] ?? [], id: \.self) { line in
1920
HStack(alignment: .top) {
2021
Image(systemName: "text.alignleft")
2122
.font(.system(size: 12))
@@ -27,15 +28,18 @@ struct FindResultList: View {
2728
}
2829
.padding(.leading, 15)
2930
.tag(line)
31+
.onTapGesture {
32+
state.workspace.openFile(item: file)
33+
}
3034
}
3135
} header: {
3236
HStack(alignment: .center) {
3337
// Image(nsImage: NSWorkspace.shared.icon(forFile: fileURL.path))
3438
// .frame(width: 13, height: 13)
35-
Text(fileURL.lastPathComponent)
39+
Text(file.url.lastPathComponent)
3640
.font(.system(size: 13, weight: .semibold))
3741
.foregroundColor(Color(nsColor: NSColor.headerTextColor))
38-
Text(fileURL.path.replacingOccurrences(of: state.workspace.fileURL?.path ?? "", with: ""))
42+
Text(file.url.path.replacingOccurrences(of: state.workspace.fileURL?.path ?? "", with: ""))
3943
}
4044
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
4145
}

0 commit comments

Comments
 (0)