Skip to content

Instantly share code, notes, and snippets.

@hugoware
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugoware/50fe607cbe7e39399fda to your computer and use it in GitHub Desktop.
Save hugoware/50fe607cbe7e39399fda to your computer and use it in GitHub Desktop.
# Sublime Plugin that will take the selected text(s)
# and replace it with a variable name you provide. The name and
# values are then prepended to the top of the file
import sublime, sublime_plugin, re
# handles asking for what to replace the selection with
class PromoteCssCommand( sublime_plugin.WindowCommand ):
def run( self ):
view = self.window.active_view()
# make sure something is selected
selected = view.sel()
region = selected[0]
replace = view.substr( region )
# if there isn't text, give up
if len( replace ) == 0: return
# save this value for later
self.replace = replace
# ask what to use instead
self.window.show_input_panel( 'Enter property name', '', self.on_done, None, None )
# after confirming the inpu
def on_done( self, text ):
# mske sure something was used
if len( text ) == 0: return
# request the change
view = self.window.active_view()
view.run_command("promote_css_apply", { "key": text, "value": self.replace })
# performs the actual replacement of text
class PromoteCssApplyCommand( sublime_plugin.TextCommand ):
def run( self, edit, key, value ):
# use the file name to test for a semicolon
syntax = self.view.settings().get('syntax')
match = re.search('sass', syntax, flags= re.IGNORECASE )
include_semicolon = ';'
if match : include_semicolon = ''
# insert the new def
self.view.insert( edit, 0, '$'+ key +' : '+ value + include_semicolon +'\n');
# replace each selection
selected = self.view.sel()
for region in selected:
self.view.replace( edit, region, '$'+ key )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment