Repository: rsteube/carapace
Pull request #195 (2021-01-02 16:35:38)

simplify xonsh script


Patch:
diff --git a/internal/xonsh/snippet.go b/internal/xonsh/snippet.go
index dacb1f5..0f5b080 100644
--- a/internal/xonsh/snippet.go
+++ b/internal/xonsh/snippet.go
@@ -10,19 +10,13 @@ import (
 
 func Snippet(cmd *cobra.Command, actions map[string]string) string {
 	functionName := strings.Replace(cmd.Name(), "-", "__", -1)
-	return fmt.Sprintf(`from shlex import split
-import re
-import pathlib
-import subprocess
-import xonsh
-from xonsh.completers._aliases import _add_one_completer
-from xonsh.completers.path import complete_dir, complete_path
-from xonsh.completers.tools import RichCompletion
-
-def _%v_completer(prefix, line, begidx, endidx, ctx):
+	return fmt.Sprintf(`def _%v_completer(prefix, line, begidx, endidx, ctx):
     """carapace completer for %v"""
     if not line.startswith('%v '):
         return # not the expected command to complete
+
+    from shlex import split
+    from xonsh.completers.tools import RichCompletion
     
     full_words=split(line + "_") # ensure last word is empty when ends with space
     full_words[-1]=full_words[-1][0:-1]
@@ -35,9 +29,11 @@ def _%v_completer(prefix, line, begidx, endidx, ctx):
     result = {}
 
     def _%v_callback():
-        cb, _ = subprocess.Popen(['%v', '_carapace', 'xonsh', '_', *words],
-                                     stdout=subprocess.PIPE,
-                                     stderr=subprocess.PIPE).communicate()
+        from subprocess import Popen, PIPE
+        from xonsh.completers.tools import RichCompletion
+        cb, _ = Popen(['%v', '_carapace', 'xonsh', '_', *words],
+                                     stdout=PIPE,
+                                     stderr=PIPE).communicate()
         cb = cb.decode('utf-8')
         if cb == "":
             return {}
@@ -52,6 +48,8 @@ def _%v_completer(prefix, line, begidx, endidx, ctx):
 
     result = set(map(lambda x: RichCompletion(x[:len(x)-(len(suffix)+suffix.count(' '))], display=x.display, description=x.description, prefix_len=len(current)+current.count(' ')), result))
     return result
+
+from xonsh.completers._aliases import _add_one_completer
 _add_one_completer('%v', _%v_completer, 'start')
-`, functionName, cmd.Name(), cmd.Name(), cmd.Name(), uid.Executable(), cmd.Name(), cmd.Name(), functionName)
+`, functionName, cmd.Name(), cmd.Name(), functionName, uid.Executable(), functionName, cmd.Name(), functionName)
 }


Test patch:
diff --git a/example/cmd/_test/xonsh.py b/example/cmd/_test/xonsh.py
index 328d4d8..18c217b 100644
--- a/example/cmd/_test/xonsh.py
+++ b/example/cmd/_test/xonsh.py
@@ -1,16 +1,10 @@
-from shlex import split
-import re
-import pathlib
-import subprocess
-import xonsh
-from xonsh.completers._aliases import _add_one_completer
-from xonsh.completers.path import complete_dir, complete_path
-from xonsh.completers.tools import RichCompletion
-
 def _example_completer(prefix, line, begidx, endidx, ctx):
     """carapace completer for example"""
     if not line.startswith('example '):
         return # not the expected command to complete
+
+    from shlex import split
+    from xonsh.completers.tools import RichCompletion
     
     full_words=split(line + "_") # ensure last word is empty when ends with space
     full_words[-1]=full_words[-1][0:-1]
@@ -23,9 +17,11 @@ def _example_completer(prefix, line, begidx, endidx, ctx):
     result = {}
 
     def _example_callback():
-        cb, _ = subprocess.Popen(['example', '_carapace', 'xonsh', '_', *words],
-                                     stdout=subprocess.PIPE,
-                                     stderr=subprocess.PIPE).communicate()
+        from subprocess import Popen, PIPE
+        from xonsh.completers.tools import RichCompletion
+        cb, _ = Popen(['example', '_carapace', 'xonsh', '_', *words],
+                                     stdout=PIPE,
+                                     stderr=PIPE).communicate()
         cb = cb.decode('utf-8')
         if cb == "":
             return {}
@@ -40,5 +36,7 @@ def _example_completer(prefix, line, begidx, endidx, ctx):
 
     result = set(map(lambda x: RichCompletion(x[:len(x)-(len(suffix)+suffix.count(' '))], display=x.display, description=x.description, prefix_len=len(current)+current.count(' ')), result))
     return result
+
+from xonsh.completers._aliases import _add_one_completer
 _add_one_completer('example', _example_completer, 'start')