import sublime
import sublime_plugin
import json
from os.path import dirname, realpath, join

try:
	# Python 2
	from node_bridge import node_bridge
except:
	from .node_bridge import node_bridge

# monkeypatch `Region` to be iterable
sublime.Region.totuple = lambda self: (self.a, self.b)
sublime.Region.__iter__ = lambda self: self.totuple().__iter__()

BIN_PATH = join(sublime.packages_path(), dirname(realpath(__file__)), 'cssfmt.js')

class CssfmtCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		if not self.has_selection():
			region = sublime.Region(0, self.view.size())
			originalBuffer = self.view.substr(region)
			fixed = self.fix(originalBuffer)
			if fixed:
				self.view.replace(edit, region, fixed)
			return
		for region in self.view.sel():
			if region.empty():
				continue
			originalBuffer = self.view.substr(region)
			fixed = self.fix(originalBuffer)
			if fixed:
				self.view.replace(edit, region, fixed)

	def fix(self, data):
		try:
			return node_bridge(data, BIN_PATH, [json.dumps({
				'filepath': self.view.file_name()
			})])
		except Exception as e:
			sublime.error_message('CSSfmt\n%s' % e)

	def has_selection(self):
		for sel in self.view.sel():
			start, end = sel
			if start != end:
				return True
		return False

@@ -192,208 +192,24 @@
 ge%0A%0A
-# monkeypatch %60Region%60 to be iterable%0Asublime.Region.totuple = lambda self: (self.a, self.b)%0Asublime.Region.__iter__ = lambda self: self.totuple().__iter__()%0A%0ABIN_PATH = join(sublime.packages_path(), 
+BIN_PATH = join(
 dirn
@@ -318,16 +318,17 @@
  edit):%0A
+%0A
 %09%09if not
@@ -350,16 +350,66 @@
 tion():%0A
+%09%09%09sublime.status_message('CSSfmt: format file')%0A%0A
 %09%09%09regio
@@ -489,34 +489,38 @@
 str(region)%0A%09%09%09f
-ix
+ormatt
 ed = self.fix(or
@@ -506,34 +506,37 @@
 rmatted = self.f
-ix
+ormat
 (originalBuffer)
@@ -535,34 +535,38 @@
 lBuffer)%0A%09%09%09if f
-ix
+ormatt
 ed:%0A%09%09%09%09self.vie
@@ -586,26 +586,30 @@
 t, region, f
-ix
+ormatt
 ed)%0A%09%09%09retur
@@ -610,16 +610,17 @@
 %09return%0A
+%0A
 %09%09for re
@@ -644,16 +644,71 @@
 .sel():%0A
+%09%09%09sublime.status_message('CSSfmt: format selections')%0A
 %09%09%09if re
@@ -783,18 +783,22 @@
 on)%0A%09%09%09f
-ix
+ormatt
 ed = sel
@@ -800,18 +800,21 @@
 = self.f
-ix
+ormat
 (origina
@@ -829,18 +829,22 @@
 %0A%09%09%09if f
-ix
+ormatt
 ed:%0A%09%09%09%09
@@ -876,18 +876,22 @@
 egion, f
-ix
+ormatt
 ed)%0A%0A%09de
@@ -897,10 +897,13 @@
 ef f
-ix
+ormat
 (sel
@@ -1118,103 +1118,65 @@
 :%0A%09%09
-for sel in self.view.sel():%0A%09%09%09start, end = sel%0A%09%09%09if start != end:%0A%09%09%09%09return True%0A%09%09return False
+return any(not region.empty() for region in self.view.sel())
 %0A

Fix patching