changeset 13507:d3ec6003856d

mx: experimental autocompletion generation for zsh (GRAAL-297)
author Bernhard Urban <bernhard.urban@jku.at>
date Fri, 03 Jan 2014 16:36:15 +0200
parents 2a4569fa9aa4
children 8a5b39d0bfb5
files mx/mx_graal.py
diffstat 1 files changed, 41 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mx/mx_graal.py	Fri Jan 03 23:56:03 2014 +0100
+++ b/mx/mx_graal.py	Fri Jan 03 16:36:15 2014 +0200
@@ -1383,12 +1383,53 @@
                     '--title', 'Graal OpenJDK Project Documentation',
                     '--dot-output-base', 'projects'] + args)
 
+def generateZshCompletion(args):
+    """generate zsh completion for mx"""
+    try:
+        from genzshcomp import CompletionGenerator
+    except ImportError:
+        mx.abort("install genzshcomp (pip install genzshcomp)")
+
+    # need to fake module for the custom mx arg parser, otherwise a check in genzshcomp fails
+    originalModule = mx._argParser.__module__
+    mx._argParser.__module__ = "argparse"
+    generator = CompletionGenerator("mx", mx._argParser)
+    mx._argParser.__module__ = originalModule
+
+    # strip last line and define local variable "ret"
+    complt = "\n".join(generator.get().split('\n')[0:-1]).replace('context state line', 'context state line ret=1')
+
+    # add array of possible subcommands (as they are not part of the argument parser)
+    complt += '\n  ": :->command" \\\n'
+    complt += '  "*::args:_files" && ret=0\n'
+    complt += '\n'
+    complt += 'case $state in\n'
+    complt += '  (command)\n'
+    complt += '    local -a main_commands\n'
+    complt += '    main_commands=(\n'
+    for cmd in sorted(mx._commands.iterkeys()):
+        c, _ = mx._commands[cmd][:2]
+        doc = c.__doc__
+        complt += '      "{0}'.format(cmd)
+        if doc:
+            complt += ':{0}'.format(doc.split('\n', 1)[0].replace('\"', '').replace("\'", ""))
+        complt += '"\n'
+    complt += '    )\n'
+    complt += '    _describe -t main_commands command main_commands && ret=0\n'
+    complt += '    ;;\n'
+    complt += 'esac\n'
+    complt += '\n'
+    complt += 'return $ret'
+    print complt
+
+
 def mx_init(suite):
     commands = {
         'build': [build, ''],
         'buildvars': [buildvars, ''],
         'buildvms': [buildvms, '[-options]'],
         'clean': [clean, ''],
+        'generateZshCompletion' : [generateZshCompletion, ''],
         'hsdis': [hsdis, '[att]'],
         'hcfdis': [hcfdis, ''],
         'igv' : [igv, ''],