Permalink
Browse files

Copying new Ruby implementation of “Insert Call to Super” command to …

…trunk.

Fixes ticket 6CB113FC as well as some other edge conditions.


git-svn-id: http://svn.textmate.org/trunk/Bundles/Objective-C.tmbundle@8243 dfb7d73b-c2ec-0310-8fea-fb051d288c6d
  • Loading branch information...
1 parent c76af62 commit 6b245ac35a12ad0cea4eb9c2a3c5cc346e44cf94 @ciaran ciaran committed Oct 2, 2007
Showing with 39 additions and 67 deletions.
  1. +39 −11 Commands/Insert Call to Super.plist
  2. +0 −56 Support/insert_super.py
@@ -5,22 +5,50 @@
<key>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
- <string># Inserts a [super method] call suitable for the current method.
-# Inserted as a snippet with the incoming arguments used as defaults.
-#
-# The default behavior is to add brackets if there is no "[" to the left
-# of the caret (ignoring the tab trigger). This can be changed by changing
-# from "automatic" to one of "always" and "never". The last argument
-# is appended at the end of the result (some prefer the empty string, others
-# a semicolon.
-#
+ <string>#!/usr/bin/env ruby
+
+# Inserts a [super method] call suitable for the current method.
+#
+# The command is inserted as a snippet with each parameter as a tabstop
+# An opening bracket is inserted if necessary, and a semicolon is
+# added after the closing bracket if there is nothing else on the line.
+#
# The command uses heuristics to find which method implementation is
# being edited. It should be reasonably tolerant to various coding styles,
# including different bracket and indentation styles.
-python "${TM_BUNDLE_SUPPORT}/insert_super.py" automatic ''
-#python "${TM_BUNDLE_SUPPORT}/insert_super.py" always ';'
+proto_re = /
+ ^\s* # Start of the line and optional space
+ [+-]\s* # a plus or minus for method specifier
+ \([^)]+\) # the return type in brackets
+ ((?:\n|[^{;])*)
+ (?m:[^;]*?)
+ \{
+ /x
+
+previous_lines = STDIN.readlines[1..ENV['TM_LINE_NUMBER'].to_i - 1]
+invocation_line = previous_lines[-1]
+
+proto = previous_lines.join.scan(proto_re)[-1]
+
+exit if proto.nil? or proto.empty?
+
+last_proto_sel_with_types = proto[0].strip.sub(/\s+/, '').sub(%r{\s*//.*$}, '').gsub(/\n\s*/, ' ')
+
+tabstop = 0
+
+last_proto_sel_with_types.gsub!(/\([^)]+\)\s*(([A-Za-z0-9_][A-Za-z0-9_]*))/) do |match|
+ %Q{${#{tabstop += 1}:#$1}}
+end
+
+print '[' if invocation_line[ENV["TM_LINE_INDEX"].to_i - 1] != ?[
+print 'super '
+print last_proto_sel_with_types
+print "]"
+print ";" if invocation_line =~ /^\s+$/
</string>
+ <key>fallbackInput</key>
+ <string>scope</string>
<key>input</key>
<string>document</string>
<key>name</key>
@@ -1,56 +0,0 @@
-#!/usr/bin/env python
-
-"""Inserts a [super method] call suitable for the current method.
- Inserted as a snippet with the incoming arguments used as defaults.
-
- The default behavior is to add brackets if there is no "[" to the left
- of the caret (ignoring the tab trigger). This can be changed by modifying
- the useBrackets line below.
-
- The command uses heuristics to find which method implementation is
- being edited. It should be reasonably tolerant to various coding styles,
- including different bracket and indentation styles.
-"""
-
-import sys, os, re, itertools
-
-try:
- useBrackets = sys.argv[1]
-except IndexError:
- useBrackets = "automatic" # "always", "never"
-
-try:
- postfix = sys.argv[2]
-except (KeyError,), ex:
- postfix = ""
-
-protoRe = re.compile(r"^\s*(\+|-)\s*\([^)]+\)((\n|[^{])*)[^;]\{", re.M)
-
-nlines = int(os.environ["TM_LINE_NUMBER"])-1
-interestingLines = [l for (count, l) in itertools.takewhile(lambda (n, l): n <= nlines, enumerate(sys.stdin))]
-invokationLine = interestingLines[-1]
-
-if useBrackets == "automatic":
- needsBracket = invokationLine[int(os.environ["TM_LINE_INDEX"])-1] != '['
-elif useBrackets == "always":
- needsBracket = True
-else:
- needsBracket = False
-
-protos = protoRe.findall(''.join(interestingLines))
-
-if len(protos) == 0:
- sys.exit(1)
-
-lastProtoSelWithTypes = re.sub(r'\s+', ' ', protos[-1][1])
-
-counter = itertools.count(1)
-def replFunc(match):
- return '${%i:%s}' % (counter.next(), match.groups()[0])
-
-methodCall = re.sub(r'\([^)]+\)\s*(([A-Za-z0-9_][A-Za-z0-9_]*))', replFunc, lastProtoSelWithTypes)
-
-brackets = needsBracket and ('[', ']') or ('', '')
-outString = '%ssuper %s%s%s' % (brackets[0], methodCall.strip(), brackets[1], postfix)
-
-sys.stdout.write(outString)

0 comments on commit 6b245ac

Please sign in to comment.