Ver código fonte

【2025】【BOTA】- 大纲搜索新增全词匹配的正则表达式

yangliuhua 1 mês atrás
pai
commit
dbb8501577

+ 26 - 5
PDF Office/PDF Master/KMClass/Left/Outline/View/KMBOTAOutlineView.swift

@@ -35,7 +35,7 @@ class KMBOTAOutlineView: BaseXibView {
     
     var isSearchMode = false
     var searchKey = ""
-    var wholeWords = false {
+    var wholeWords = true {
         didSet {
             if isValidSearchMode() == false {
                 return
@@ -313,12 +313,33 @@ extension KMBOTAOutlineView : NSOutlineViewDataSource,NSOutlineViewDelegate {
                 _searchKey = _searchKey.lowercased()
             }
             
-            let ranges = label.ranges(of: _searchKey)
-            for range in ranges.nsRnage {
-                attri.addAttributes([.font : NSFont.SFProTextBoldFont(13), .foregroundColor: KMNColorTools.colorPrimary_textLight()], range: range)
+            if wholeWords {
+                let escapedKey = NSRegularExpression.escapedPattern(for: _searchKey)
+                
+                let pattern = "\\b\(escapedKey)\\b"
+
+                do {
+                    let regex = try NSRegularExpression(pattern: pattern, options: caseSensitive ? [] : [.caseInsensitive])
+                    let matches = regex.matches(in: label, range: NSRange(location: 0, length: label.utf16.count))
+                    
+                    for match in matches {
+                        attri.addAttributes([
+                            .font : NSFont.SFProTextBoldFont(13),
+                            .foregroundColor: KMNColorTools.colorPrimary_textLight()
+                        ], range: match.range)
+                    }
+                } catch {
+                    
+                }
+            } else {
+                let ranges = label.ranges(of: _searchKey)
+                for range in ranges.nsRnage {
+                    attri.addAttributes([.font : NSFont.SFProTextBoldFont(13), .foregroundColor: KMNColorTools.colorPrimary_textLight()], range: range)
+                }
+               
             }
-            cell.titleLabel.attributedStringValue = attri
             
+            cell.titleLabel.attributedStringValue = attri
             cell.iconButton.isHidden = model.searchChildren.isEmpty
         } else {
             cell.titleLabel.stringValue = model.outline.label ?? ""