Skip to content

Instantly share code, notes, and snippets.

@quidmonkey
Last active December 19, 2015 04:29
Show Gist options
  • Save quidmonkey/5897867 to your computer and use it in GitHub Desktop.
Save quidmonkey/5897867 to your computer and use it in GitHub Desktop.
A Sublime Text 2 Plugin that will write a console.log of the selected Javascript on the line above or below. Suggested user key-binding is to use Ctrl+C+Up to create a console.log of the selected text above the current line, and Ctrl+C+Down to log to the line below. To install this plugin, create a jsLog/ directory in your Sublime Text 2 Package…
[{
"id": "jslog",
"caption": "Javascript Log",
"command": "js_log"
}]
[
{ "keys": ["ctrl+c+down"], "command": "js_log", "args": {"direction": "down"} },
{ "keys": ["ctrl+c+up"], "command": "js_log", "args": {"direction": "up"} }
]
import sublime_plugin
# Extends TextCommand so that run() receives a View to modify.
class jsLogCommand(sublime_plugin.TextCommand):
def run(self, edit, direction):
# walk through each region in the selection
selection = self.view.sel()[0]
# grab selected text and format into a console.log
line = self.view.line(selection)
selected = self.view.substr(selection)
indent = self.get_indentation(selection)
if direction == 'down':
# log below
log = "\n" + indent + "console.log(\'" + selected + "\', " + selected + ");"
# add console.log to end of line
self.view.insert(edit, line.end(), log)
elif direction == 'up':
# log above
log = indent + "console.log(\'" + selected + "\', " + selected + ");\n"
# add console.log to beginning of line
self.view.insert(edit, line.begin(), log)
def get_indentation(self, selection):
(row, col) = self.view.rowcol(selection.begin())
indent_region = self.view.find('^\s+', self.view.text_point(row, 0))
if self.view.rowcol(indent_region.begin())[0] == row:
return self.view.substr(indent_region)
else:
return ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment