Skip to content

Instantly share code, notes, and snippets.

@daanzu
Last active April 24, 2016 02:39
Show Gist options
  • Save daanzu/4b0992715d461ded8b5f61fe776740d0 to your computer and use it in GitHub Desktop.
Save daanzu/4b0992715d461ded8b5f61fe776740d0 to your computer and use it in GitHub Desktop.
Sublime Text plugin to select block at cursor, in various languages
# Sublime Text plugin to select block at cursor, in various languages
# https://gist.github.com/daanzu/4b0992715d461ded8b5f61fe776740d0
# view.run_command("select_block")
# view.settings().get('syntax')
import sublime, sublime_plugin
class SelectBlockCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.run_command('expand_selection', {'to': 'indentation'})
selection = self.view.sel()
for region in selection:
syntax = self.view.settings().get('syntax')
# if 'Python.' in syntax: begin_offset, end_offset = 1, 0
# elif 'Ruby.' in syntax: begin_offset, end_offset = 1, 1
# elif 'Slim.' in syntax: begin_offset, end_offset = 1, 0
# elif 'JavaScript.' in syntax: begin_offset, end_offset = 1, 1
# else: begin_offset, end_offset = 0, 0
scope_name = self.view.scope_name(region.begin())
scope = scope_name.split()
if 'source.python' in scope: begin_offset, end_offset = 1, 0
elif 'source.ruby' in scope: begin_offset, end_offset = 1, 1
elif 'text.slim' in scope: begin_offset, end_offset = 1, 0
elif 'source.js' in scope: begin_offset, end_offset = 1, 1
else: begin_offset, end_offset = 0, 0
# print("begin_offset={} end_offset={} syntax={} scope={} scope_name={}".format(begin_offset, end_offset, syntax, scope, scope_name))
selection.add(self.view.full_line(sublime.Region(region.begin()-begin_offset, region.end()-1+end_offset)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment