|
14 | 14 |
|
15 | 15 |
|
16 | 16 | mod = Module() |
| 17 | +mod.tag( |
| 18 | + "cursorless_default_vocabulary", |
| 19 | + desc="Use default cursorless vocabulary instead of user custom", |
| 20 | +) |
17 | 21 | cursorless_settings_directory = mod.setting( |
18 | 22 | "cursorless_settings_directory", |
19 | 23 | type=str, |
20 | 24 | default="cursorless-settings", |
21 | 25 | desc="The directory to use for cursorless settings csvs relative to talon user directory", |
22 | 26 | ) |
23 | 27 |
|
| 28 | +default_ctx = Context() |
| 29 | +default_ctx.matches = r""" |
| 30 | +tag: user.cursorless_default_vocabulary |
| 31 | +""" |
| 32 | + |
24 | 33 |
|
25 | 34 | def init_csv_and_watch_changes( |
26 | 35 | filename: str, |
@@ -68,6 +77,9 @@ def init_csv_and_watch_changes( |
68 | 77 |
|
69 | 78 | file_path.parent.mkdir(parents=True, exist_ok=True) |
70 | 79 |
|
| 80 | + check_for_duplicates(filename, default_values) |
| 81 | + create_default_vocabulary_dicts(default_values, pluralize_lists) |
| 82 | + |
71 | 83 | def on_watch(path, flags): |
72 | 84 | if file_path.match(path): |
73 | 85 | current_values, has_errors = read_file( |
@@ -126,27 +138,49 @@ def unsubscribe(): |
126 | 138 | return unsubscribe |
127 | 139 |
|
128 | 140 |
|
| 141 | +def check_for_duplicates(filename, default_values): |
| 142 | + results_map = {} |
| 143 | + for list_name, dict in default_values.items(): |
| 144 | + for key, value in dict.items(): |
| 145 | + if value in results_map: |
| 146 | + existing_list_name = results_map[value]["list"] |
| 147 | + warning = f"WARNING ({filename}): Value `{value}` duplicated between lists '{existing_list_name}' and '{list_name}'" |
| 148 | + print(warning) |
| 149 | + app.notify(warning) |
| 150 | + |
| 151 | + |
129 | 152 | def is_removed(value: str): |
130 | 153 | return value.startswith("-") |
131 | 154 |
|
132 | 155 |
|
| 156 | +def create_default_vocabulary_dicts( |
| 157 | + default_values: dict[str, dict], pluralize_lists: list[str] |
| 158 | +): |
| 159 | + default_values_updated = {} |
| 160 | + for key, value in default_values.items(): |
| 161 | + updated_dict = {} |
| 162 | + for key2, value2 in value.items(): |
| 163 | + # Enable deactivated(prefixed with a `-`) items |
| 164 | + active_key = key2[1:] if key2.startswith("-") else key2 |
| 165 | + if active_key: |
| 166 | + updated_dict[active_key] = value2 |
| 167 | + default_values_updated[key] = updated_dict |
| 168 | + assign_lists_to_context(default_ctx, default_values_updated, pluralize_lists) |
| 169 | + |
| 170 | + |
133 | 171 | def update_dicts( |
134 | 172 | default_values: dict[str, dict], |
135 | 173 | current_values: dict, |
136 | 174 | extra_ignored_values: list[str], |
137 | 175 | allow_unknown_values: bool, |
138 | 176 | default_list_name: Optional[str], |
139 | | - pluralize_lists: Optional[list[str]], |
| 177 | + pluralize_lists: list[str], |
140 | 178 | ctx: Context, |
141 | 179 | ): |
142 | 180 | # Create map with all default values |
143 | 181 | results_map = {} |
144 | 182 | for list_name, dict in default_values.items(): |
145 | 183 | for key, value in dict.items(): |
146 | | - if value in results_map: |
147 | | - warning = f"WARNING: Duplicate value `{value}` in {list_name}.csv" |
148 | | - print(warning) |
149 | | - app.notify(warning) |
150 | 184 | results_map[value] = {"key": key, "value": value, "list": list_name} |
151 | 185 |
|
152 | 186 | # Update result with current values |
@@ -183,6 +217,14 @@ def update_dicts( |
183 | 217 | results[obj["list"]][k.strip()] = value |
184 | 218 |
|
185 | 219 | # Assign result to talon context list |
| 220 | + assign_lists_to_context(ctx, results, pluralize_lists) |
| 221 | + |
| 222 | + |
| 223 | +def assign_lists_to_context( |
| 224 | + ctx: Context, |
| 225 | + results: dict, |
| 226 | + pluralize_lists: list[str], |
| 227 | +): |
186 | 228 | for list_name, dict in results.items(): |
187 | 229 | list_singular_name = get_cursorless_list_name(list_name) |
188 | 230 | ctx.lists[list_singular_name] = dict |
|
0 commit comments