From abe947a8dd06585039e8bcb04ddf6f500aee4f35 Mon Sep 17 00:00:00 2001 From: Huntout Zhang Date: Wed, 26 Mar 2014 17:30:49 +0800 Subject: [PATCH] restore selection and position after format --- CSScomb.py | 27 ++++++++++++++++++--------- package.json | 2 +- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/CSScomb.py b/CSScomb.py index 60a4b8c..d500325 100644 --- a/CSScomb.py +++ b/CSScomb.py @@ -18,6 +18,9 @@ def run(self, edit): if not syntax: return + previous_selection = [(region.a, region.b) for region in self.view.sel()] + previous_position = self.view.viewport_position() + config_path = self.get_setting('custom_config_path') if not config_path and self.view.file_name() != None: config_path = self.get_config_path() @@ -26,16 +29,22 @@ def run(self, edit): region = sublime.Region(0, self.view.size()) originalBuffer = self.view.substr(region) combed = self.comb(originalBuffer, syntax, config_path) - if combed: - self.view.replace(edit, region, combed) - return - for region in self.view.sel(): - if region.empty(): - continue - originalBuffer = self.view.substr(region) - combed = self.comb(originalBuffer, syntax, config_path) - if combed: + if combed != originalBuffer: self.view.replace(edit, region, combed) + else: + for region in self.view.sel(): + if region.empty(): + continue + originalBuffer = self.view.substr(region) + combed = self.comb(originalBuffer, syntax, config_path) + if combed != originalBuffer: + self.view.replace(edit, region, combed) + + self.view.set_viewport_position((0, 0,), False) + self.view.set_viewport_position(previous_position, False) + self.view.sel().clear() + for a, b in previous_selection: + self.view.sel().add(sublime.Region(a, b)) def comb(self, css, syntax, config_path): try: diff --git a/package.json b/package.json index 19b6b61..a862843 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "sublime-csscomb", "version": "0.1.3", "dependencies": { - "csscomb": "2.0.4" + "csscomb": "~2.0.4" }, "license": "MIT" }