♻️ refactor(clipboard): improve search functionality - search through all items, sort by relevance, limit results after filtering and sorting
This commit is contained in:
2 files changed
+76
-16
No files matched your search
@@ -279,28 +279,35 @@ class ClipboardPlugin(PluginBase):
|
||||
if not clipboard_items:
|
||||
return results
|
||||
|
||||
# Filter items based on query with early termination
|
||||
# Filter items based on query - search through ALL items first
|
||||
filtered_items = []
|
||||
query_lower = query_string.lower() if query_string else ""
|
||||
|
||||
for item in clipboard_items:
|
||||
# Stop if we have enough results
|
||||
if len(filtered_items) >= self.max_results:
|
||||
break
|
||||
|
||||
parts = item.split("\t", 1)
|
||||
content = parts[1] if len(parts) > 1 else item
|
||||
content_lower = content.lower()
|
||||
|
||||
# Fast filtering
|
||||
if not query_lower or query_lower in content.lower():
|
||||
filtered_items.append(item)
|
||||
# Fast filtering - search through all items, don't limit here
|
||||
if not query_lower or query_lower in content_lower:
|
||||
# Calculate relevance score for better sorting
|
||||
relevance = 1.0
|
||||
if query_lower:
|
||||
if content_lower.startswith(query_lower):
|
||||
relevance = 1.0 # Exact start match
|
||||
elif query_lower in content_lower:
|
||||
# Position-based scoring: earlier matches get higher scores
|
||||
position = content_lower.find(query_lower)
|
||||
relevance = max(0.5, 1.0 - (position / len(content_lower)) * 0.4)
|
||||
|
||||
filtered_items.append((item, relevance))
|
||||
|
||||
# Sort by relevance (highest first) and then limit results
|
||||
filtered_items.sort(key=lambda x: x[1], reverse=True)
|
||||
filtered_items = filtered_items[:self.max_results]
|
||||
|
||||
# Process items with lazy image loading
|
||||
for i, item in enumerate(filtered_items):
|
||||
# Limit total results
|
||||
if len(results) >= self.max_results:
|
||||
break
|
||||
|
||||
for i, (item, relevance) in enumerate(filtered_items):
|
||||
parts = item.split("\t", 1)
|
||||
item_id = parts[0] if len(parts) > 1 else str(i)
|
||||
content = parts[1] if len(parts) > 1 else item
|
||||
@@ -319,7 +326,7 @@ class ClipboardPlugin(PluginBase):
|
||||
subtitle="Click to copy image to clipboard",
|
||||
description="Image content",
|
||||
icon=cached_pixbuf,
|
||||
relevance=1.0,
|
||||
relevance=relevance,
|
||||
plugin_name=self.name,
|
||||
action=lambda id=item_id: self._copy_to_clipboard(id),
|
||||
data={"bypass_max_results": True},
|
||||
@@ -331,7 +338,7 @@ class ClipboardPlugin(PluginBase):
|
||||
subtitle="Loading preview...",
|
||||
description="Image content",
|
||||
icon_name="image-x-generic",
|
||||
relevance=1.0,
|
||||
relevance=relevance,
|
||||
plugin_name=self.name,
|
||||
action=lambda id=item_id: self._copy_to_clipboard(id),
|
||||
data={"bypass_max_results": True},
|
||||
@@ -351,7 +358,7 @@ class ClipboardPlugin(PluginBase):
|
||||
if len(content) <= 100
|
||||
else content[:97] + "...",
|
||||
icon_name="edit-paste",
|
||||
relevance=1.0,
|
||||
relevance=relevance,
|
||||
plugin_name=self.name,
|
||||
action=lambda id=item_id: self._copy_to_clipboard(id),
|
||||
data={"bypass_max_results": True},
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify clipboard search improvements.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def test_clipboard_search():
|
||||
"""Test clipboard search functionality."""
|
||||
try:
|
||||
from modules.launcher.plugins.clipboard import ClipboardPlugin
|
||||
|
||||
print("Testing clipboard search improvements...")
|
||||
|
||||
# Create plugin instance
|
||||
plugin = ClipboardPlugin()
|
||||
plugin.initialize()
|
||||
|
||||
print("✅ Plugin initialized successfully")
|
||||
|
||||
# Test empty query (should show all items up to max_results)
|
||||
print("\n🔍 Testing empty query...")
|
||||
results = plugin.query("")
|
||||
print(f"Found {len(results)} results for empty query")
|
||||
|
||||
# Test specific search
|
||||
print("\n🔍 Testing search query 'test'...")
|
||||
results = plugin.query("test")
|
||||
print(f"Found {len(results)} results for 'test' query")
|
||||
|
||||
# Show relevance scores
|
||||
if results:
|
||||
print("Relevance scores:")
|
||||
for i, result in enumerate(results[:5]): # Show first 5
|
||||
print(f" {i+1}. {result.relevance:.2f} - {result.title[:50]}...")
|
||||
|
||||
print("\n✅ Clipboard search test completed!")
|
||||
print("The plugin now:")
|
||||
print(" - Searches through ALL clipboard items (not just first 20)")
|
||||
print(" - Sorts results by relevance (exact matches first)")
|
||||
print(" - Limits results only after filtering and sorting")
|
||||
|
||||
plugin.cleanup()
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Error testing clipboard search: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_clipboard_search()
|
||||
Reference in new issue
Block a user