Skip to content

Commit

Permalink
One commit is all we'll need here, right?
Browse files Browse the repository at this point in the history
A single take, just like Calculon.
  • Loading branch information
clarabstract committed Apr 19, 2014
0 parents commit c2c3111
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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#<tab>

# 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
```
30 changes: 30 additions & 0 deletions SuperPython.py
Original file line number Diff line number Diff line change
@@ -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()))]




0 comments on commit c2c3111

Please sign in to comment.