import sublime, sublime_plugin

def pyramid_sort(txt):
	txt = list(filter(lambda s: s.strip(), txt))
	txt.sort(key = lambda s: len(s))
	return txt

class PyramidSortCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		regions = [s for s in self.view.sel() if not s.empty()]
		if regions:			
			for r in regions:
				txt = self.view.substr(r)
				lines = txt.splitlines()
				lines = pyramid_sort(lines)
				self.view.replace(edit, r, u"\n".join(lines))


import sublime, sublime_plugin

def pyramid_sort(txt):
	txt = list(filter(lambda s: s.strip(), txt))
	txt.sort(key = lambda s: len(s))
	return txt

class PyramidSortCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		regions = [s for s in self.view.sel() if not s.empty()]
		if regions:			
			for r in regions:
				lr = self.view.line(r)
				txt = self.view.substr(lr)
				lines = txt.splitlines()
				lines = pyramid_sort(lines)
				self.view.replace(edit, lr, u"\n".join(lines))

Revert "removed grab line from region, gives some unexpected behaviour. Instead just replace exactly what is marked"
Revert "removed grab line from region, gives some unexpected behaviour. Instead just replace exactly what is marked"

This reverts commit 9c944db3affc8181146fa27d8483a58d2731756b.

+ 
  import sublime, sublime_plugin
  
  def pyramid_sort(txt):
  	txt = list(filter(lambda s: s.strip(), txt))
  	txt.sort(key = lambda s: len(s))
  	return txt
  
  class PyramidSortCommand(sublime_plugin.TextCommand):
  	def run(self, edit):
  		regions = [s for s in self.view.sel() if not s.empty()]
  		if regions:			
  			for r in regions:
+ 				lr = self.view.line(r)
- 				txt = self.view.substr(r)
+ 				txt = self.view.substr(lr)
  				lines = txt.splitlines()
  				lines = pyramid_sort(lines)
- 				self.view.replace(edit, r, u"\n".join(lines))
+ 				self.view.replace(edit, lr, u"\n".join(lines))
  
Revert "removed grab line from region, gives some unexpected behaviour. Instead just replace exactly what is marked"
## Code Before:
import sublime, sublime_plugin

def pyramid_sort(txt):
	txt = list(filter(lambda s: s.strip(), txt))
	txt.sort(key = lambda s: len(s))
	return txt

class PyramidSortCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		regions = [s for s in self.view.sel() if not s.empty()]
		if regions:			
			for r in regions:
				txt = self.view.substr(r)
				lines = txt.splitlines()
				lines = pyramid_sort(lines)
				self.view.replace(edit, r, u"\n".join(lines))

## Instruction:
Revert "removed grab line from region, gives some unexpected behaviour. Instead just replace exactly what is marked"
## Code After:

import sublime, sublime_plugin

def pyramid_sort(txt):
	txt = list(filter(lambda s: s.strip(), txt))
	txt.sort(key = lambda s: len(s))
	return txt

class PyramidSortCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		regions = [s for s in self.view.sel() if not s.empty()]
		if regions:			
			for r in regions:
				lr = self.view.line(r)
				txt = self.view.substr(lr)
				lines = txt.splitlines()
				lines = pyramid_sort(lines)
				self.view.replace(edit, lr, u"\n".join(lines))
