Skip to content

Instantly share code, notes, and snippets.

@cbess
Last active December 24, 2015 22:39
Show Gist options
  • Save cbess/6874694 to your computer and use it in GitHub Desktop.
Save cbess/6874694 to your computer and use it in GitHub Desktop.
TextMate command to convert `frame` dot notation to equivalent CGGeometry function calls
#!/usr/bin/python
# Created by C. Bess (2013)
# Converts the frame dot notation to CGGeometry function calls
#
# ref: http://manual.macromates.com/en/environment_variables.html
#
# Usage:
# After selecting the text that contains the target text, use
# 'Select Bundle Item' menu item, then find this command and press enter
import os
import re
import sys
# matches: "clipRect2.origin.y" => ('clipRect2', 'origin', 'y')
CG_REGEX = re.compile(r'(\w+)\.(origin|size)\.(\w+)')
input_text = os.environ.get('TM_SELECTED_TEXT', '')
# input_text = os.environ.get('TM_CURRENT_LINE', '')
# Sample text:
# boxRect = CGRectMake(0, clipRect2.origin.y - 0.2, clipRect.origin.x, clipRect.size.height);
# iterate to output conversions
def replace_matches(match):
parts = match.groups()
# does it have enough parts
if len(parts) != 3:
return
struct = parts[0]
func_type = parts[1]
axis = parts[2]
# build new func call
func_str = 'CGRectGet'
if func_type == 'origin':
pass
elif func_type == 'size':
pass
if axis == 'x':
func_str += 'MinX'
elif axis == 'y':
func_str += 'MinY'
elif axis == 'width':
func_str += 'Width'
elif axis == 'height':
func_str += 'Height'
func_str += '(%s)' % struct
return func_str
# sys.stdout.write(func_str)
# convert it
# match = CG_REGEX.search(input_text, match.end())
output_text = CG_REGEX.sub(replace_matches, input_text)
sys.stdout.write(output_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment