Skip to content

Commit 3e7a96f

Browse files
authored
{Core} Improve autocomplete performance by preventing loading all modules (#24765)
1 parent afe77bd commit 3e7a96f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/azure-cli-core/azure/cli/core/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,20 @@ def _get_extension_suppressions(mod_loaders):
449449
if raw_cmd in self.command_group_table:
450450
logger.debug("Found a match in the command group table for '%s'.", raw_cmd)
451451
return self.command_table
452+
if self.cli_ctx.data['completer_active']:
453+
# If the command is not complete in autocomplete mode, we should match shorter command.
454+
# For example, `account sho` should match `account`.
455+
logger.debug("Could not find a match in the command or command group table for '%s'", raw_cmd)
456+
trimmed_raw_cmd = ' '.join(raw_cmd.split()[:-1])
457+
logger.debug("In autocomplete mode, try to match trimmed raw cmd: '%s'", trimmed_raw_cmd)
458+
if not trimmed_raw_cmd:
459+
# If full command is 'az acc', raw_cmd is 'acc', trimmed_raw_cmd is ''.
460+
logger.debug("Trimmed raw cmd is empty, return command table.")
461+
return self.command_table
462+
if trimmed_raw_cmd in self.command_group_table:
463+
logger.debug("Found a match in the command group table for trimmed raw cmd: '%s'.",
464+
trimmed_raw_cmd)
465+
return self.command_table
452466

453467
logger.debug("Could not find a match in the command or command group table for '%s'. "
454468
"The index may be outdated.", raw_cmd)
@@ -527,6 +541,7 @@ def __init__(self, cli_ctx=None):
527541
if cli_ctx:
528542
self.version = __version__
529543
self.cloud_profile = cli_ctx.cloud.profile
544+
self.cli_ctx = cli_ctx
530545

531546
def get(self, args):
532547
"""Get the corresponding module and extension list of a command.
@@ -555,6 +570,13 @@ def get(self, args):
555570
# Check the command index for (command: [module]) mapping, like
556571
# "network": ["azure.cli.command_modules.natgateway", "azure.cli.command_modules.network", "azext_firewall"]
557572
index_modules_extensions = index.get(top_command)
573+
if not index_modules_extensions and self.cli_ctx.data['completer_active']:
574+
# If user type `az acco`, command begin with `acco` will be matched.
575+
logger.debug("In autocomplete mode, load commands starting with: '%s'", top_command)
576+
index_modules_extensions = []
577+
for command in index:
578+
if command.startswith(top_command):
579+
index_modules_extensions += index[command]
558580

559581
if index_modules_extensions:
560582
# This list contains both built-in modules and extensions

0 commit comments

Comments
 (0)