Skip to content

Instantly share code, notes, and snippets.

@jonwalstedt
Last active January 4, 2016 20:39
Show Gist options
  • Select an option

  • Save jonwalstedt/8675108 to your computer and use it in GitHub Desktop.

Select an option

Save jonwalstedt/8675108 to your computer and use it in GitHub Desktop.
Helper to get current filename, row and method when logging in javascript and coffeescript, also prints for php and python
import sublime, sublime_plugin, os, re
class LogHelperCommand(sublime_plugin.TextCommand):
def run(self, edit):
(row,col) = self.view.rowcol(self.view.sel()[0].begin())
syntax = self.view.settings().get('syntax').split('/')[2].lower().replace(".tmlanguage", "")
text = ""
s = ""
offset = 0
filename = os.path.basename(self.view.file_name())
current_row = str(row+1)
clean_name = re.compile('^\s*(public\s+|private\s+|protected\s+|static\s+|function\s+|def\s+)+', re.I)
for region in self.view.sel():
region_row, region_col = self.view.rowcol(region.begin())
function_regions = self.view.find_by_selector('meta.function')
if function_regions:
for r in reversed(function_regions):
row, col = self.view.rowcol(r.begin())
if row <= region_row:
lines = self.view.substr(r).splitlines()
name = clean_name.sub('', lines[0])
s += name.strip()
found = True
break
file_and_row = filename+', row '+current_row+', ' + s
if syntax == "python":
text = 'print("'+file_and_row+': " + )'
offset = 1
elif syntax == "php":
text = 'echo "'+file_and_row+': " . ;'
offset = 1
elif syntax == "javascript":
text = 'console.log("'+file_and_row+': ", );'
offset = 2
elif syntax == "coffeescript":
text = 'console.log "'+file_and_row+': ", '
offset = 0
else:
text = file_and_row
self.view.insert(edit, self.view.sel()[0].begin(), text)
# Updated row and col after insertion
(row,col) = self.view.rowcol(self.view.sel()[0].begin())
# Move cursor to the right position
pt = self.view.text_point(row, col-offset)
self.view.sel().clear()
self.view.sel().add(sublime.Region(pt))
{ "keys": ["super+alt+l"], "command": "log_helper" },
Example output:
in coffeescript
console.log "Gallery.coffee, row 93, onGalleryOver: ",
in javascript
console.log("video.js, row 2279, getVideoTagSettings: function(): ", );
in php
echo "functions.php, row 117, getVideo(): " . ;
in python
print("logfilname.py, row 51, run(self, edit):: " + )
any other syntax will just output filname and row
_gallery.scss, row 56,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment