1
Fork 0
mirror of https://github.com/j1g5awi/krunner-vscode.git synced 2024-12-22 23:16:05 +00:00

Provide accurate values for relevance

This commit is contained in:
Jake Howard 2023-05-14 17:57:22 +01:00
parent e2cbbcd1f4
commit 102abf38e1
Signed by: jake
GPG key ID: 57AFB45680EDD477

View file

@ -1,3 +1,4 @@
import difflib
import json
import os
import sqlite3
@ -31,6 +32,21 @@ class Match(NamedTuple):
properties: dict
def get_matches(paths, query):
"""
Equivalent to `difflib.get_close_matches`, but returning the ratio too
"""
matches = []
s = difflib.SequenceMatcher()
s.set_seq2(query)
for path in paths:
s.set_seq1(path)
matches.append((s.ratio(), path))
return matches
# Read path_list from database
def get_path_list():
paths = set()
@ -72,17 +88,18 @@ class Runner(dbus.service.Object):
@dbus.service.method(iface, in_signature="s", out_signature="a(sssida{sv})")
def Match(self, query: str):
# data, display text, icon, type (Plasma::QueryType), relevance (0-1), properties (subtext, category and urls)
matches = get_matches(get_path_list(), query)
return [
Match(
path,
Path(path).name,
"com.visualstudio.code.oss",
100,
1.0,
ratio,
{"subtext": path},
)
for path in get_path_list()
if query.lower() in Path(path).name.lower()
for ratio, path in matches
]
@dbus.service.method(iface, out_signature="a(sss)")