Skip to content

Commit dd52745

Browse files
AndreasArvidssonpre-commit-ci[bot]auscompgeeknriley
authored
Sort help contexts by specificity (talonhub#1460)
Instead of just sorting the help context alphabetically we now first sort them by context match specificity. Context with equal specificity are sorted alphabetically. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: David Vo <auscompgeek@users.noreply.github.com> Co-authored-by: Nicholas Riley <com-github@sabi.net>
1 parent fd17764 commit dd52745

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

core/help/help.py

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import math
33
import re
44
from collections import defaultdict
5+
from functools import cmp_to_key
56
from itertools import islice
6-
from typing import Iterable
7+
from typing import Any, Iterable, Tuple
78

89
from talon import Context, Module, actions, imgui, registry, settings
910

@@ -22,6 +23,12 @@
2223
default=50,
2324
desc="Max lines of command to display per page in help",
2425
)
26+
mod.setting(
27+
"help_sort_contexts_by_specificity",
28+
type=bool,
29+
default=True,
30+
desc="If true contexts are sorted by specificity before alphabetically. If false, contexts are just sorted alphabetically.",
31+
)
2532

2633
ctx = Context()
2734
# context name -> commands
@@ -205,10 +212,17 @@ def gui_context_help(gui: imgui.GUI):
205212

206213
current_item_index = 1
207214
current_selection_index = 1
208-
for display_name in sorted_display_list:
215+
current_group = ""
216+
for display_name, group, _ in sorted_display_list:
209217
target_page = get_context_page(current_item_index)
210218
context_name = display_name_to_context_name_map[display_name]
211219
if current_context_page == target_page:
220+
if current_group != group:
221+
if current_group:
222+
gui.line()
223+
gui.text(f"{group}:")
224+
current_group = group
225+
212226
button_name = format_context_button(
213227
current_selection_index,
214228
display_name,
@@ -429,7 +443,10 @@ def refresh_context_command_map(enabled_only=False):
429443

430444
context_map = local_context_map
431445
context_command_map = local_context_command_map
432-
sorted_display_list = sorted(local_display_name_to_context_name_map.keys())
446+
sorted_display_list = get_sorted_display_keys(
447+
local_context_map,
448+
local_display_name_to_context_name_map,
449+
)
433450
show_enabled_contexts_only = enabled_only
434451
display_name_to_context_name_map = local_display_name_to_context_name_map
435452
rule_word_map = refresh_rule_word_map(local_context_command_map)
@@ -438,6 +455,48 @@ def refresh_context_command_map(enabled_only=False):
438455
update_active_contexts_cache(active_contexts)
439456

440457

458+
def get_sorted_display_keys(
459+
context_map: dict[str, Any],
460+
display_name_to_context_name_map: dict[str, str],
461+
):
462+
if settings.get("user.help_sort_contexts_by_specificity"):
463+
return get_sorted_keys_by_context_specificity(
464+
context_map,
465+
display_name_to_context_name_map,
466+
)
467+
return [
468+
(display_name, "", 0)
469+
for display_name in sorted(display_name_to_context_name_map.keys())
470+
]
471+
472+
473+
def get_sorted_keys_by_context_specificity(
474+
context_map: dict[str, Any],
475+
display_name_to_context_name_map: dict[str, str],
476+
) -> list[Tuple[str, str, int]]:
477+
def get_group(display_name) -> Tuple[str, str, int]:
478+
try:
479+
context_name = display_name_to_context_name_map[display_name]
480+
context = context_map[context_name]
481+
keys = context._match.keys()
482+
if "app.app" in keys:
483+
return (display_name, "Application-specific", 2)
484+
if keys:
485+
return (display_name, "Context-dependent", 1)
486+
return (display_name, "Global", 0)
487+
except Exception as ex:
488+
return (display_name, "", 0)
489+
490+
grouped_list = [
491+
get_group(display_name)
492+
for display_name in display_name_to_context_name_map.keys()
493+
]
494+
return sorted(
495+
grouped_list,
496+
key=lambda item: (-item[2], item[0]),
497+
)
498+
499+
441500
def refresh_rule_word_map(context_command_map):
442501
rule_word_map = defaultdict(set)
443502

settings.talon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ settings():
1818
# Set the number of contexts to display per help page
1919
user.help_max_contexts_per_page = 20
2020

21+
# Uncomment to always sort help contexts alphabetically.
22+
# user.help_sort_contexts_by_specificity = false
23+
2124
# Set the scroll amount for continuous scroll/gaze scroll
2225
user.mouse_continuous_scroll_amount = 80
2326

0 commit comments

Comments
 (0)