Skip to content

Commit 58dfff4

Browse files
committed
highlight search query in 'Open Quickly' results
1 parent 373d477 commit 58dfff4

5 files changed

Lines changed: 72 additions & 50 deletions

File tree

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@
325325
61A53A812B4449F00093BF8A /* WorkspaceDocument+Index.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61A53A802B4449F00093BF8A /* WorkspaceDocument+Index.swift */; };
326326
661EF7B82BEE215300C3E577 /* ImageFileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 661EF7B72BEE215300C3E577 /* ImageFileView.swift */; };
327327
661EF7BD2BEE215300C3E577 /* LoadingFileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 661EF7BC2BEE215300C3E577 /* LoadingFileView.swift */; };
328+
6653EE552C34817900B82DE2 /* QuickSearchResultLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6653EE542C34817900B82DE2 /* QuickSearchResultLabel.swift */; };
328329
669BC4082BED306400D1197C /* AnyFileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 669BC4072BED306400D1197C /* AnyFileView.swift */; };
329330
66AF6CE22BF17CC300D83C9D /* StatusBarViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66AF6CE12BF17CC300D83C9D /* StatusBarViewModel.swift */; };
330331
66AF6CE42BF17F6800D83C9D /* StatusBarFileInfoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66AF6CE32BF17F6800D83C9D /* StatusBarFileInfoView.swift */; };
@@ -943,6 +944,7 @@
943944
61A53A802B4449F00093BF8A /* WorkspaceDocument+Index.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WorkspaceDocument+Index.swift"; sourceTree = "<group>"; };
944945
661EF7B72BEE215300C3E577 /* ImageFileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageFileView.swift; sourceTree = "<group>"; };
945946
661EF7BC2BEE215300C3E577 /* LoadingFileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadingFileView.swift; sourceTree = "<group>"; };
947+
6653EE542C34817900B82DE2 /* QuickSearchResultLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuickSearchResultLabel.swift; sourceTree = "<group>"; };
946948
669BC4072BED306400D1197C /* AnyFileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyFileView.swift; sourceTree = "<group>"; };
947949
66AF6CE12BF17CC300D83C9D /* StatusBarViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusBarViewModel.swift; sourceTree = "<group>"; };
948950
66AF6CE32BF17F6800D83C9D /* StatusBarFileInfoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusBarFileInfoView.swift; sourceTree = "<group>"; };
@@ -1807,6 +1809,7 @@
18071809
58798210292D92370085B254 /* Search */ = {
18081810
isa = PBXGroup;
18091811
children = (
1812+
6653EE532C34816F00B82DE2 /* Views */,
18101813
613899AF2B6E6FB800A5CAF6 /* FuzzySearch */,
18111814
58798212292D92370085B254 /* Extensions */,
18121815
58798214292D92370085B254 /* Model */,
@@ -2535,6 +2538,14 @@
25352538
path = ActivityViewer;
25362539
sourceTree = "<group>";
25372540
};
2541+
6653EE532C34816F00B82DE2 /* Views */ = {
2542+
isa = PBXGroup;
2543+
children = (
2544+
6653EE542C34817900B82DE2 /* QuickSearchResultLabel.swift */,
2545+
);
2546+
path = Views;
2547+
sourceTree = "<group>";
2548+
};
25382549
66AF6CE02BF17CB100D83C9D /* ViewModels */ = {
25392550
isa = PBXGroup;
25402551
children = (
@@ -3653,6 +3664,7 @@
36533664
58822528292C280D00E83CDE /* StatusBarEncodingSelector.swift in Sources */,
36543665
0FD96BCE2BEF42530025A697 /* CodeEditWindowController+Toolbar.swift in Sources */,
36553666
6C7F37FE2A3EA6FA00217B83 /* View+focusedValue.swift in Sources */,
3667+
6653EE552C34817900B82DE2 /* QuickSearchResultLabel.swift in Sources */,
36563668
B6C4F2A12B3CA37500B2B140 /* SourceControlNavigatorHistoryView.swift in Sources */,
36573669
6CBE1CFB2B71DAA6003AC32E /* Loopable.swift in Sources */,
36583670
30B088092C0D53080063A882 /* LanguageServer+Formatting.swift in Sources */,

CodeEdit/Features/Commands/Views/QuickActionsView.swift

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ struct QuickActionsView: View {
4343
}
4444

4545
var body: some View {
46-
SearchPanelView<SearchResultLabel, EmptyView, Command>(
46+
SearchPanelView<QuickSearchResultLabel, EmptyView, Command>(
4747
title: "Commands",
4848
image: Image(systemName: "magnifyingglass"),
4949
options: $state.filteredCommands,
5050
text: $state.commandQuery,
5151
alwaysShowOptions: true,
5252
optionRowHeight: 30
5353
) { command in
54-
SearchResultLabel(labelName: command.title, textToMatch: state.commandQuery)
54+
QuickSearchResultLabel(labelName: command.title, textToMatch: state.commandQuery)
5555
} onRowClick: { command in
5656
callHandler(command: command)
5757
} onClose: {
@@ -62,47 +62,3 @@ struct QuickActionsView: View {
6262
}
6363
}
6464
}
65-
66-
/// Implementation of command palette entity. While swiftui does not allow to use NSMutableAttributeStrings,
67-
/// the only way to fallback to UIKit and have NSViewRepresentable to be a bridge between UIKit and SwiftUI.
68-
/// Highlights currently entered text query
69-
70-
struct SearchResultLabel: NSViewRepresentable {
71-
72-
var labelName: String
73-
var textToMatch: String
74-
75-
public func makeNSView(context: Context) -> some NSTextField {
76-
let label = NSTextField(wrappingLabelWithString: labelName)
77-
label.translatesAutoresizingMaskIntoConstraints = false
78-
label.drawsBackground = false
79-
label.textColor = .labelColor
80-
label.isEditable = false
81-
label.isSelectable = false
82-
label.font = .labelFont(ofSize: 13)
83-
label.allowsDefaultTighteningForTruncation = false
84-
label.cell?.truncatesLastVisibleLine = true
85-
label.cell?.wraps = true
86-
label.maximumNumberOfLines = 1
87-
label.attributedStringValue = highlight()
88-
return label
89-
}
90-
91-
func highlight() -> NSAttributedString {
92-
let attribText = NSMutableAttributedString(string: self.labelName)
93-
let range: NSRange = attribText.mutableString.range(
94-
of: self.textToMatch,
95-
options: NSString.CompareOptions.caseInsensitive
96-
)
97-
attribText.addAttribute(.foregroundColor, value: NSColor(Color(.labelColor)), range: range)
98-
attribText.addAttribute(.font, value: NSFont.boldSystemFont(ofSize: NSFont.systemFontSize), range: range)
99-
100-
return attribText
101-
}
102-
103-
func updateNSView(_ nsView: NSViewType, context: Context) {
104-
nsView.textColor = textToMatch.isEmpty ? .labelColor : .secondaryLabelColor
105-
nsView.attributedStringValue = highlight()
106-
}
107-
108-
}

CodeEdit/Features/QuickOpen/Views/QuickOpenItem.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ import SwiftUI
1010
struct QuickOpenItem: View {
1111
private let baseDirectory: URL
1212
private let fileURL: URL
13+
private let textToMatch: String
1314

1415
init(
1516
baseDirectory: URL,
16-
fileURL: URL
17+
fileURL: URL,
18+
textToMatch: String
1719
) {
1820
self.baseDirectory = baseDirectory
1921
self.fileURL = fileURL
22+
self.textToMatch = textToMatch
2023
}
2124

2225
var relativePathComponents: ArraySlice<String> {
@@ -30,8 +33,7 @@ struct QuickOpenItem: View {
3033
.aspectRatio(contentMode: .fit)
3134
.frame(width: 24, height: 24)
3235
VStack(alignment: .leading, spacing: 0) {
33-
Text(fileURL.lastPathComponent).font(.system(size: 13))
34-
.lineLimit(1)
36+
QuickSearchResultLabel(labelName: fileURL.lastPathComponent, textToMatch: textToMatch)
3537
Text(relativePathComponents.joined(separator: ""))
3638
.font(.system(size: 10.5))
3739
.foregroundColor(.secondary)

CodeEdit/Features/QuickOpen/Views/QuickOpenView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct QuickOpenView: View {
4141
text: $state.openQuicklyQuery,
4242
optionRowHeight: 40
4343
) { file in
44-
QuickOpenItem(baseDirectory: state.fileURL, fileURL: file)
44+
QuickOpenItem(baseDirectory: state.fileURL, fileURL: file, textToMatch: state.openQuicklyQuery)
4545
} preview: { fileURL in
4646
QuickOpenPreviewView(item: CEWorkspaceFile(url: fileURL))
4747
} onRowClick: { fileURL in
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// QuickSearchResultLabel.swift
3+
// CodeEdit
4+
//
5+
// Copyright © 2024-present Paul Ebose. All rights reserved.
6+
//
7+
8+
import SwiftUI
9+
10+
/// Implementation of command palette entity. While swiftui does not allow to use NSMutableAttributeStrings,
11+
/// the only way to fallback to UIKit and have NSViewRepresentable to be a bridge between UIKit and SwiftUI.
12+
/// Highlights currently entered text query
13+
14+
struct QuickSearchResultLabel: NSViewRepresentable {
15+
16+
var labelName: String
17+
var textToMatch: String
18+
19+
public func makeNSView(context: Context) -> some NSTextField {
20+
let label = NSTextField(wrappingLabelWithString: labelName)
21+
label.translatesAutoresizingMaskIntoConstraints = false
22+
label.drawsBackground = false
23+
label.textColor = .labelColor
24+
label.isEditable = false
25+
label.isSelectable = false
26+
label.font = .labelFont(ofSize: 13)
27+
label.allowsDefaultTighteningForTruncation = false
28+
label.cell?.truncatesLastVisibleLine = true
29+
label.cell?.wraps = true
30+
label.maximumNumberOfLines = 1
31+
label.attributedStringValue = highlight()
32+
return label
33+
}
34+
35+
func highlight() -> NSAttributedString {
36+
let attribText = NSMutableAttributedString(string: self.labelName)
37+
let range: NSRange = attribText.mutableString.range(
38+
of: self.textToMatch,
39+
options: NSString.CompareOptions.caseInsensitive
40+
)
41+
attribText.addAttribute(.foregroundColor, value: NSColor(Color(.labelColor)), range: range)
42+
attribText.addAttribute(.font, value: NSFont.boldSystemFont(ofSize: NSFont.systemFontSize), range: range)
43+
44+
return attribText
45+
}
46+
47+
func updateNSView(_ nsView: NSViewType, context: Context) {
48+
nsView.textColor = textToMatch.isEmpty ? .labelColor : .secondaryLabelColor
49+
nsView.attributedStringValue = highlight()
50+
}
51+
52+
}

0 commit comments

Comments
 (0)