mirror of
https://github.com/j1g5awi/krunner-vscode.git
synced 2024-11-22 15:19:30 +00:00
Provide accurate values for relevance
This commit is contained in:
parent
e2cbbcd1f4
commit
102abf38e1
1 changed files with 20 additions and 3 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
import difflib
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
@ -31,6 +32,21 @@ class Match(NamedTuple):
|
||||||
properties: dict
|
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
|
# Read path_list from database
|
||||||
def get_path_list():
|
def get_path_list():
|
||||||
paths = set()
|
paths = set()
|
||||||
|
@ -72,17 +88,18 @@ class Runner(dbus.service.Object):
|
||||||
@dbus.service.method(iface, in_signature="s", out_signature="a(sssida{sv})")
|
@dbus.service.method(iface, in_signature="s", out_signature="a(sssida{sv})")
|
||||||
def Match(self, query: str):
|
def Match(self, query: str):
|
||||||
# data, display text, icon, type (Plasma::QueryType), relevance (0-1), properties (subtext, category and urls)
|
# data, display text, icon, type (Plasma::QueryType), relevance (0-1), properties (subtext, category and urls)
|
||||||
|
matches = get_matches(get_path_list(), query)
|
||||||
|
|
||||||
return [
|
return [
|
||||||
Match(
|
Match(
|
||||||
path,
|
path,
|
||||||
Path(path).name,
|
Path(path).name,
|
||||||
"com.visualstudio.code.oss",
|
"com.visualstudio.code.oss",
|
||||||
100,
|
100,
|
||||||
1.0,
|
ratio,
|
||||||
{"subtext": path},
|
{"subtext": path},
|
||||||
)
|
)
|
||||||
for path in get_path_list()
|
for ratio, path in matches
|
||||||
if query.lower() in Path(path).name.lower()
|
|
||||||
]
|
]
|
||||||
|
|
||||||
@dbus.service.method(iface, out_signature="a(sss)")
|
@dbus.service.method(iface, out_signature="a(sss)")
|
||||||
|
|
Loading…
Reference in a new issue