Skip to content

Commit 539c3b0

Browse files
committed
Improve codes
1 parent 3b8acc9 commit 539c3b0

File tree

10 files changed

+286
-152
lines changed

10 files changed

+286
-152
lines changed

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
B658FB3727DA9E1000EA4DBD /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B658FB3627DA9E1000EA4DBD /* Preview Assets.xcassets */; };
4040
B65E614627E6765D00255275 /* Introspect in Frameworks */ = {isa = PBXBuildFile; productRef = B65E614527E6765D00255275 /* Introspect */; };
4141
B673FDAD27E8296A00795864 /* PressActionsModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = B673FDAC27E8296A00795864 /* PressActionsModifier.swift */; };
42-
D7012EE827E757850001E1EF /* SidebarSearch.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7012EE727E757850001E1EF /* SidebarSearch.swift */; };
42+
D7012EE827E757850001E1EF /* SidebarFind.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7012EE727E757850001E1EF /* SidebarFind.swift */; };
4343
D70F5E2C27E4E8CF004EE4B9 /* WelcomeModule in Frameworks */ = {isa = PBXBuildFile; productRef = D70F5E2B27E4E8CF004EE4B9 /* WelcomeModule */; };
4444
D7211D4327E066CE008F2ED7 /* Localized+Ex.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7211D4227E066CE008F2ED7 /* Localized+Ex.swift */; };
4545
D7211D4727E06BFE008F2ED7 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = D7211D4927E06BFE008F2ED7 /* Localizable.strings */; };
@@ -49,7 +49,10 @@
4949
D7E201AE27E8B3C000CB86D0 /* String+Ranges.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7E201AD27E8B3C000CB86D0 /* String+Ranges.swift */; };
5050
D7E201B027E8C07300CB86D0 /* SearchBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7E201AF27E8C07300CB86D0 /* SearchBar.swift */; };
5151
D7E201B227E8D50000CB86D0 /* SearchModeSelector.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7E201B127E8D50000CB86D0 /* SearchModeSelector.swift */; };
52-
D7E201B427E9989900CB86D0 /* FindResultList.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7E201B327E9989900CB86D0 /* FindResultList.swift */; };
52+
D7E201B427E9989900CB86D0 /* SearchResultList.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7E201B327E9989900CB86D0 /* SearchResultList.swift */; };
53+
D7E201BB27E9ECD600CB86D0 /* SearchResultModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7E201BA27E9ECD600CB86D0 /* SearchResultModel.swift */; };
54+
D7E201BD27EA00E200CB86D0 /* SearchResultFileItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7E201BC27EA00E200CB86D0 /* SearchResultFileItem.swift */; };
55+
D7E201BF27EA1ACD00CB86D0 /* FindModeModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7E201BE27EA1ACD00CB86D0 /* FindModeModel.swift */; };
5356
/* End PBXBuildFile section */
5457

5558
/* Begin PBXContainerItemProxy section */
@@ -109,7 +112,7 @@
109112
B658FB3D27DA9E1000EA4DBD /* CodeEditTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CodeEditTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
110113
B658FB4727DA9E1000EA4DBD /* CodeEditUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CodeEditUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
111114
B673FDAC27E8296A00795864 /* PressActionsModifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PressActionsModifier.swift; sourceTree = "<group>"; };
112-
D7012EE727E757850001E1EF /* SidebarSearch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarSearch.swift; sourceTree = "<group>"; };
115+
D7012EE727E757850001E1EF /* SidebarFind.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarFind.swift; sourceTree = "<group>"; };
113116
D7211D4227E066CE008F2ED7 /* Localized+Ex.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Localized+Ex.swift"; sourceTree = "<group>"; };
114117
D7211D4827E06BFE008F2ED7 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
115118
D7211D4A27E06C01008F2ED7 /* sr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sr; path = sr.lproj/Localizable.strings; sourceTree = "<group>"; };
@@ -119,7 +122,10 @@
119122
D7E201AD27E8B3C000CB86D0 /* String+Ranges.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Ranges.swift"; sourceTree = "<group>"; };
120123
D7E201AF27E8C07300CB86D0 /* SearchBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchBar.swift; sourceTree = "<group>"; };
121124
D7E201B127E8D50000CB86D0 /* SearchModeSelector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchModeSelector.swift; sourceTree = "<group>"; };
122-
D7E201B327E9989900CB86D0 /* FindResultList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindResultList.swift; sourceTree = "<group>"; };
125+
D7E201B327E9989900CB86D0 /* SearchResultList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResultList.swift; sourceTree = "<group>"; };
126+
D7E201BA27E9ECD600CB86D0 /* SearchResultModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResultModel.swift; sourceTree = "<group>"; };
127+
D7E201BC27EA00E200CB86D0 /* SearchResultFileItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResultFileItem.swift; sourceTree = "<group>"; };
128+
D7E201BE27EA1ACD00CB86D0 /* FindModeModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindModeModel.swift; sourceTree = "<group>"; };
123129
/* End PBXFileReference section */
124130

125131
/* Begin PBXFrameworksBuildPhase section */
@@ -294,10 +300,12 @@
294300
D7012EE627E757660001E1EF /* Search */ = {
295301
isa = PBXGroup;
296302
children = (
297-
D7012EE727E757850001E1EF /* SidebarSearch.swift */,
303+
D7E201B527E9EA8A00CB86D0 /* Model */,
304+
D7012EE727E757850001E1EF /* SidebarFind.swift */,
298305
D7E201AF27E8C07300CB86D0 /* SearchBar.swift */,
299306
D7E201B127E8D50000CB86D0 /* SearchModeSelector.swift */,
300-
D7E201B327E9989900CB86D0 /* FindResultList.swift */,
307+
D7E201B327E9989900CB86D0 /* SearchResultList.swift */,
308+
D7E201BC27EA00E200CB86D0 /* SearchResultFileItem.swift */,
301309
);
302310
path = Search;
303311
sourceTree = "<group>";
@@ -321,6 +329,15 @@
321329
path = Welcome;
322330
sourceTree = "<group>";
323331
};
332+
D7E201B527E9EA8A00CB86D0 /* Model */ = {
333+
isa = PBXGroup;
334+
children = (
335+
D7E201BA27E9ECD600CB86D0 /* SearchResultModel.swift */,
336+
D7E201BE27EA1ACD00CB86D0 /* FindModeModel.swift */,
337+
);
338+
path = Model;
339+
sourceTree = "<group>";
340+
};
324341
/* End PBXGroup section */
325342

326343
/* Begin PBXNativeTarget section */
@@ -516,7 +533,8 @@
516533
2B7A583527E4BA0100D25D4E /* AppDelegate.swift in Sources */,
517534
2875A46D27E3BE5B007805F8 /* BreadcrumbsView.swift in Sources */,
518535
0485EB1D27E7338100138301 /* QuickOpenItem.swift in Sources */,
519-
D7012EE827E757850001E1EF /* SidebarSearch.swift in Sources */,
536+
D7E201BB27E9ECD600CB86D0 /* SearchResultModel.swift in Sources */,
537+
D7012EE827E757850001E1EF /* SidebarFind.swift in Sources */,
520538
D7E201AE27E8B3C000CB86D0 /* String+Ranges.swift in Sources */,
521539
04540D5B27DD08C300E91B77 /* SettingsView.swift in Sources */,
522540
D7E201B027E8C07300CB86D0 /* SearchBar.swift in Sources */,
@@ -532,11 +550,13 @@
532550
287776E927E34BC700D46668 /* TabBar.swift in Sources */,
533551
0485EB1F27E7458B00138301 /* WorkspaceCodeFileView.swift in Sources */,
534552
0485EB1927E70F4900138301 /* QuickOpenView.swift in Sources */,
553+
D7E201BD27EA00E200CB86D0 /* SearchResultFileItem.swift in Sources */,
535554
286620A527E4AB6900E18C2B /* BreadcrumbsComponent.swift in Sources */,
536555
287776EF27E3515300D46668 /* TabBarItem.swift in Sources */,
537556
D72E1A8727E4242900EB11B9 /* RecentProjectsView.swift in Sources */,
538557
04660F6A27E51E5C00477777 /* CodeEditWindowController.swift in Sources */,
539-
D7E201B427E9989900CB86D0 /* FindResultList.swift in Sources */,
558+
D7E201BF27EA1ACD00CB86D0 /* FindModeModel.swift in Sources */,
559+
D7E201B427E9989900CB86D0 /* SearchResultList.swift in Sources */,
540560
287776E727E3413200D46668 /* SideBar.swift in Sources */,
541561
287776ED27E350D800D46668 /* SideBarItem.swift in Sources */,
542562
289978ED27E4E97E00BB0357 /* FileIconStyle.swift in Sources */,

CodeEdit/Documents/WorkspaceDocument.swift

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

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

184184
init(_ workspace: WorkspaceDocument) {
185185
self.workspace = workspace
186186
}
187187

188188
func search(_ text: String) {
189+
self.searchResult = []
189190
if let url = self.workspace.fileURL {
190191
let enumerator = FileManager.default.enumerator(at: url,
191192
includingPropertiesForKeys: [
@@ -199,28 +200,35 @@ extension WorkspaceDocument {
199200
filePaths.map { url in
200201
WorkspaceClient.FileItem(url: url, children: nil)
201202
}.forEach { fileItem in
202-
let data = try? String(contentsOf: fileItem.url)
203-
data?.split(separator: "\n").forEach { line in
204-
let noSpaceLine = line.trimmingCharacters(in: .whitespaces)
205-
if noSpaceLine.contains(text) {
206-
noSpaceLine.ranges(of: text).forEach { range in
207-
var attributedString = AttributedString()
208-
attributedString.append(
209-
AttributedString(String(noSpaceLine[noSpaceLine.startIndex..<range.lowerBound]))
210-
)
211-
var searchedString = AttributedString(String(noSpaceLine[range]))
212-
searchedString.font = .system(size: 12, weight: .bold)
213-
searchedString.foregroundColor = .labelColor
214-
attributedString.append(searchedString)
215-
attributedString.append(
216-
AttributedString(String(noSpaceLine[range.upperBound..<noSpaceLine.endIndex]))
217-
)
218-
var lines = self.searchResult[fileItem] ?? []
219-
lines.append(attributedString)
220-
self.searchResult[fileItem] = lines
203+
var fileAddedFlag = true
204+
do {
205+
let data = try Data(contentsOf: fileItem.url)
206+
data.withUnsafeBytes {
207+
$0.split(separator: UInt8(ascii: "\n"))
208+
.map { String(decoding: UnsafeRawBufferPointer(rebasing: $0), as: UTF8.self) }
209+
}.enumerated().forEach { (index: Int, line: String) in
210+
let noSpaceLine = line.trimmingCharacters(in: .whitespaces)
211+
if noSpaceLine.contains(text) {
212+
if fileAddedFlag {
213+
searchResult.append(SearchResultModel(
214+
file: fileItem,
215+
lineNumber: nil,
216+
lineContent: nil,
217+
keywordRange: nil)
218+
)
219+
fileAddedFlag = false
220+
}
221+
noSpaceLine.ranges(of: text).forEach { range in
222+
searchResult.append(SearchResultModel(
223+
file: fileItem,
224+
lineNumber: index,
225+
lineContent: noSpaceLine,
226+
keywordRange: range)
227+
)
228+
}
221229
}
222230
}
223-
}
231+
} catch {}
224232
}
225233
}
226234
}

CodeEdit/SideBar/Search/FindResultList.swift

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//
2+
// SearchModeModel.swift
3+
// CodeEdit
4+
//
5+
// Created by Ziyuan Zhao on 2022/3/22.
6+
//
7+
8+
import Foundation
9+
10+
struct SearchModeModel {
11+
let title: String
12+
let children: [SearchModeModel]
13+
let needSelectionHightlight: Bool
14+
15+
static let Containing = SearchModeModel(title: "Containing", children: [], needSelectionHightlight: false)
16+
static let MatchingWord = SearchModeModel(title: "Matching Word", children: [], needSelectionHightlight: true)
17+
static let StartingWith = SearchModeModel(title: "Starting With", children: [], needSelectionHightlight: true)
18+
static let EndingWith = SearchModeModel(title: "Ending With", children: [], needSelectionHightlight: true)
19+
20+
static let Text = SearchModeModel(title: "Text",
21+
children: [.Containing, .MatchingWord, .StartingWith, .EndingWith],
22+
needSelectionHightlight: false)
23+
static let References = SearchModeModel(title: "References",
24+
children: [.Containing, .MatchingWord, .StartingWith, .EndingWith],
25+
needSelectionHightlight: true)
26+
static let Definitions = SearchModeModel(title: "Definitions",
27+
children: [.Containing, .MatchingWord, .StartingWith, .EndingWith],
28+
needSelectionHightlight: true)
29+
static let RegularExpression = SearchModeModel(title: "Regular Expression",
30+
children: [],
31+
needSelectionHightlight: true)
32+
static let CallHierarchy = SearchModeModel(title: "Call Hierarchy", children: [], needSelectionHightlight: true)
33+
34+
static let Find = SearchModeModel(title: "Find",
35+
children: [.Text, .References, .Definitions, .RegularExpression, .CallHierarchy],
36+
needSelectionHightlight: false)
37+
static let Replace = SearchModeModel(title: "Replace",
38+
children: [.Text, .RegularExpression],
39+
needSelectionHightlight: true)
40+
41+
static let TextMatchingModes: [SearchModeModel] = [.Containing, .MatchingWord, .StartingWith, .EndingWith]
42+
static let FindModes: [SearchModeModel] = [.Text, .References, .Definitions, .RegularExpression, .CallHierarchy]
43+
static let ReplaceModes: [SearchModeModel] = [.Text, .RegularExpression]
44+
static let SearchModes: [SearchModeModel] = [.Find, .Replace]
45+
46+
static func getAllModes(_ index: Int, currentSelected: [SearchModeModel]) -> [SearchModeModel] {
47+
switch index {
48+
case 0:
49+
return SearchModes
50+
case 1:
51+
if let searchMode = currentSelected.first {
52+
if searchMode == SearchModeModel.Find {
53+
return SearchModes
54+
} else if searchMode == searchMode {
55+
return ReplaceModes
56+
}
57+
} else {
58+
return []
59+
}
60+
case 2:
61+
return TextMatchingModes
62+
default:
63+
return []
64+
}
65+
return []
66+
}
67+
}
68+
69+
extension SearchModeModel: Equatable {
70+
static func == (lhs: SearchModeModel, rhs: SearchModeModel) -> Bool {
71+
return lhs.title == rhs.title
72+
&& lhs.children == rhs.children
73+
&& lhs.needSelectionHightlight == rhs.needSelectionHightlight
74+
}
75+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// SearchResultModel.swift
3+
// CodeEdit
4+
//
5+
// Created by Ziyuan Zhao on 2022/3/22.
6+
//
7+
8+
import Foundation
9+
import WorkspaceClient
10+
11+
struct SearchResultModel: Hashable {
12+
let file: WorkspaceClient.FileItem
13+
let lineNumber: Int?
14+
let lineContent: String?
15+
let keywordRange: Range<String.Index>?
16+
17+
var hasKeywordInfo: Bool {
18+
return lineNumber != nil && lineContent != nil && keywordRange != nil
19+
}
20+
21+
// var tag: String {
22+
// return [
23+
// file.url.path,
24+
// "\(lineNumber ?? 0)",
25+
// "\(keywordRange?.lowerBound.utf16Offset(in: lineContent ?? "") ?? 0)",
26+
// "\(keywordRange?.upperBound.utf16Offset(in: lineContent ?? "") ?? 0)"
27+
// ].joined(separator: "-")
28+
// }
29+
}

CodeEdit/SideBar/Search/SearchBar.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import SwiftUI
99

1010
struct SearchBar: View {
11+
@ObservedObject var state: WorkspaceDocument.SearchState
1112
let title: String
1213
@Binding var text: String
1314

@@ -32,6 +33,7 @@ struct SearchBar: View {
3233
private var clearButton: some View {
3334
Button {
3435
self.text = ""
36+
state.search("")
3537
} label: {
3638
Image(systemName: "xmark.circle.fill")
3739
}
@@ -43,7 +45,7 @@ struct SearchBar: View {
4345
struct SearchBar_Previews: PreviewProvider {
4446
static var previews: some View {
4547
HStack {
46-
SearchBar(title: "placeholder", text: .constant("value"))
48+
SearchBar(state: .init(WorkspaceDocument.init()), title: "placeholder", text: .constant("value"))
4749
}
4850
.padding()
4951
}

0 commit comments

Comments
 (0)