From c2c3111bea04c96565762f4c14c08e8c6dba57de Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 18 Apr 2014 23:51:49 -0700 Subject: [PATCH] One commit is all we'll need here, right? A single take, just like Calculon. --- .gitignore | 1 + README.md | 34 ++++++++++++++++++++++++++++++++++ SuperPython.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 SuperPython.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7e99e36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..fb814ed --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +SuperPython +=========== + +Adds tab-completion to Python's somewhat verbose `super()` construct. Just hit `tab` after a `super` keyword and a snippet will be inserted contained the likely current class and method name, with the existing arguments already filled. + +Example: +```python +class AHappyClass(JustSwellBase): + def __init__(self, required_arg, and_another, *anything_else, **enough_already): + super# + +# Becomes: + +class AHappyClass(JustSwellBase): + def __init__(self, required_arg, and_another, *anything_else, **enough_already): + super(AHappyClass, self).__init__(required_arg, and_another, *anything_else, **enough_already) + +``` + +SubleText's own parsing is used to guess the "current" class name, method name and arguments, which should be fine almost all the time, but may in rare cases pick up the wrong thing, say, if there's an innter class or method definition between the call to `super` and the real method signature. + +Installation +------------ + +You know the drill. To install this plugin, you have two options: + +1. **Package Control** (recommended). If you have [Sublime Package + Control](https://sublime.wbond.net/) installed, simply search for + `SuperPython` to install. + +2. **Manual**. Clone source code to Sublime Text `Packages` folder: +```bash +$ git clone https://github.com/rubyruy/SuperPython +``` \ No newline at end of file diff --git a/SuperPython.py b/SuperPython.py new file mode 100644 index 0000000..b1fee29 --- /dev/null +++ b/SuperPython.py @@ -0,0 +1,30 @@ +import bisect +import sublime, sublime_plugin + +def _find_closest_scope(view, scope, target): + matches = view.find_by_selector(scope) + closest_insrt = bisect.bisect_left(matches, target) + return view.substr(matches[closest_insrt - 1]) + +class SuperComplete(sublime_plugin.EventListener): + def on_query_completions(self, view, prefix, locations): + if prefix == 'super' or prefix == 'supe' \ + and 'source.python' in view.scope_name(locations[0]): + + target = sublime.Region(locations[0], locations[0]) + fn_name = _find_closest_scope( + view, 'entity.name.function.python', target) + cls_name = _find_closest_scope( + view, 'entity.name.type.class.python', target) + + args = _find_closest_scope( + view, 'meta.function.parameters.python', target) + + self_name, other_args = args.split(',', 1) + + return [('auto-super()', 'super(%s, %s).%s(${1:%s})' % ( + cls_name, self_name, fn_name, other_args.strip()))] + + + +