Jakube

Multiple input completion Ultisnips

May 3rd, 2021
685
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. global !p
  2. def complete(t, opts):
  3.     if t:
  4.         opts = [opt[len(t):] for opt in opts if opt.startswith(t)]
  5.     if len(opts) == 1:
  6.         return opts[0]
  7.     if len(opts) == 0:
  8.         return ""
  9.     return "(" + "|".join(opts) + ")"
  10.  
  11. simple_var_types = sorted(["int", "long long", "string", "float", "double", "bool", "char"])
  12. pair_var_types = sorted(["pair<int, int>", "pair<long, long>"])
  13. var_types = simple_var_types + pair_var_types
  14.  
  15. import re
  16. def read_cin(var_type, names):
  17.     if t:
  18.         opts = [opt for opt in var_types if opt.startswith(var_type)]
  19.         if len(opts) == 1 and opts[0] in simple_var_types + pair_var_types:
  20.             var_type = opts[0]
  21.             result = "cin"
  22.             for name in re.split("\s*,\s*", names):
  23.                 if var_type in simple_var_types:
  24.                     result += " >> {}".format(name)
  25.                 if var_type in pair_var_types:
  26.                     result += " >> {}.first >> {}.second".format(name, name)
  27.             return result + ";"
  28.     return "cin >> ;"
  29. endglobal
  30.  
  31. snippet read "read values from stdin" b
  32. ${1:i}`!p snip.rv = complete(t[1], var_types)` ${2:n};
  33. `!p snip.rv = read_cin(t[1], t[2])`
  34. $0
  35. endsnippet
  36.  
  37. snippet readvl "read vector from stdin" b
  38. vector<${1:i}`!p snip.rv = complete(t[1], var_types)`> ${2:v}(${3:n});
  39. for (int i = 0; i < $3; i++) {
  40.     `!p snip.rv = read_cin(t[1], t[2] + "[i]")`
  41. }
  42. $0
  43. endsnippet
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×