Skip to content

Instantly share code, notes, and snippets.

@symmetriq
Last active November 13, 2017 01:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save symmetriq/13c5a2e4699e34518dff to your computer and use it in GitHub Desktop.
Save symmetriq/13c5a2e4699e34518dff to your computer and use it in GitHub Desktop.
BBEdit: Toggle Parentheses
#!/usr/bin/env ruby
#-------------------------------------------------------------------------------
# Toggle Parentheses
#-------------------------------------------------------------------------------
# Jason Sims <jason@symmetriq.com>
#-------------------------------------------------------------------------------
#
# For languages with optional parentheses, such as Ruby and CoffeeScript.
#
# Wraps a selection with parentheses if they're not already present.
# Removes parentheses and adds leading space if the selection starts and ends
# with parentheses.
#
#-------------------------------------------------------------------------------
#
# Installation in BBEdit:
#
# 1. Place this file in your "Text Filters" directory:
# ~/Library/Application Support/BBEdit/Text Filters/
# or
# ~/Dropbox/Application Support/BBEdit/Text Filters/
#
# 2. Assign a keyboard shortcut to "Toggle Parentheses" in:
# BBEdit → Preferences → Menus & Shortcuts → Text → Apply Text Filter
#
#-------------------------------------------------------------------------------
# More scripts & snippets here:
# https://gist.github.com/symmetriq
#-------------------------------------------------------------------------------
input = ARGF.set_encoding('UTF-8').read
if input[0] == '(' and input[-1] == ')'
# has parentheses; remove them (and add leading space)
print " #{input[1..-2]}"
else
# no parentheses; add them (and strip whitespace)
print "(#{input.strip})"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment