added mhtml support and fixed some bugs in the process

This commit is contained in:
2026-04-22 21:19:55 -07:00
parent 90787bd0a2
commit 67c272db4b
9 changed files with 564 additions and 66 deletions
+99
View File
@@ -546,6 +546,35 @@ class CmdletIntrospection:
except Exception:
return []
@staticmethod
def query_args(cmd_name: str,
config: Optional[Dict[str,
Any]] = None) -> List[Dict[str,
Any]]:
try:
meta = get_cmdlet_metadata(cmd_name, config=config) or {}
except Exception:
return []
args = meta.get("args", []) if isinstance(meta, dict) else []
if not isinstance(args, list):
return []
query_args: List[Dict[str, Any]] = []
for arg in args:
if not isinstance(arg, dict):
continue
key = str(arg.get("query_key") or "").strip().lower()
aliases = [
str(value).strip().lower()
for value in (arg.get("query_aliases") or [])
if str(value).strip()
]
if not key and not aliases:
continue
query_args.append(arg)
return query_args
class CmdletCompleter(Completer):
"""Prompt-toolkit completer for the Medeia cmdlet REPL."""
@@ -678,6 +707,76 @@ class CmdletCompleter(Completer):
if cmd_name == "search-file":
provider_name = self._flag_value(stage_tokens, "-plugin", "--plugin")
query_specs = CmdletIntrospection.query_args(cmd_name, config)
query_flag_index = -1
for idx, tok in enumerate(stage_tokens):
if str(tok or "").strip().lower() in {"-query", "--query"}:
query_flag_index = idx
if query_specs and query_flag_index >= 0:
query_parts = stage_tokens[query_flag_index + 1:]
query_started_quoted = bool(query_parts and str(query_parts[0] or "")[:1] in {"'", '"'})
query_fragment: Optional[str] = None
if prev_token in {"-query", "--query"} and current_token[:1] in {"'", '"'}:
query_fragment = current_token
elif query_started_quoted and not ends_with_space:
query_fragment = current_token
elif query_started_quoted and ends_with_space and ":" in prev_token:
query_fragment = ""
if query_fragment is not None:
field_choices: Dict[str, List[str]] = {}
ordered_fields: List[str] = []
for spec in query_specs:
key = str(spec.get("query_key") or spec.get("name") or "").strip().lower()
if not key:
continue
if key not in field_choices:
ordered_fields.append(key)
field_choices[key] = [str(choice) for choice in list(spec.get("choices", []) or [])]
for alias in spec.get("query_aliases", []) or []:
alias_text = str(alias or "").strip().lower()
if not alias_text:
continue
field_choices.setdefault(alias_text, field_choices[key])
raw_fragment = str(query_fragment or "")
segment = raw_fragment[1:] if raw_fragment[:1] in {"'", '"'} else raw_fragment
if "," in segment:
segment = segment.rsplit(",", 1)[-1].lstrip()
segment = segment.lstrip()
if ":" in segment:
field, partial = segment.split(":", 1)
field = field.strip().lower()
partial_lower = partial.strip().lower()
inline_choices = []
if cmd_name == "search-file" and provider_name:
inline_choices = plugin_inline_query_choices(provider_name, field, config)
choice_pool = inline_choices or field_choices.get(field, [])
if choice_pool:
filtered = (
[choice for choice in choice_pool if partial_lower in str(choice).lower()]
if partial_lower else list(choice_pool)
)
for choice in (filtered or choice_pool):
yield Completion(str(choice), start_position=-len(partial))
return
else:
partial_lower = segment.strip().lower()
field_pool = ordered_fields
filtered_fields = (
[field for field in field_pool if field.startswith(partial_lower)]
if partial_lower else field_pool
)
for field in (filtered_fields or field_pool):
yield Completion(f"{field}:", start_position=-len(segment))
if filtered_fields or field_pool:
return
if (
cmd_name == "search-file"
and provider_name