This repository was archived by the owner on May 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPostCSSSorting.py
More file actions
102 lines (73 loc) · 2.82 KB
/
Copy pathPostCSSSorting.py
File metadata and controls
102 lines (73 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import sublime
import sublime_plugin
import json
from os.path import dirname, realpath, join, basename, splitext
from os import path
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__)), 'sorting.js')
CONFIG_NAMES = ['.postcss-sorting.json', 'postcss-sorting.json']
def local_postcss_settings():
if sublime.active_window().project_data():
for folder in sublime.active_window().project_data()['folders']:
for config_name in CONFIG_NAMES:
config_file = join(folder['path'], config_name)
if path.isfile(config_file):
with open(config_file) as data_file:
return json.load(data_file)
return {}
def sublime_project_settings(view):
settings = view.settings().get('PostCSSSorting')
if settings is None:
settings = {}
return settings
def get_setting(view, key):
setting = local_postcss_settings().get(key)
if setting is None:
setting = sublime_project_settings(view).get(key)
if setting is None:
setting = sublime.load_settings('PostCSSSorting.sublime-settings').get(key)
return setting
def is_supported_syntax(view):
syntax = splitext(basename(view.settings().get('syntax')))[0]
return syntax in ('CSS', 'PostCSS', 'SCSS')
class PostcsssortingCommand(sublime_plugin.TextCommand):
def run(self, edit):
if not self.has_selection():
region = sublime.Region(0, self.view.size())
originalBuffer = self.view.substr(region)
processed = self.sorting(originalBuffer)
if processed:
self.view.replace(edit, region, processed)
return
for region in self.view.sel():
if region.empty():
continue
originalBuffer = self.view.substr(region)
processed = self.sorting(originalBuffer)
if processed:
self.view.replace(edit, region, processed)
def sorting(self, data):
sorting_options = {}
if get_setting(self.view, 'order'):
sorting_options['order'] = get_setting(self.view, 'order')
if get_setting(self.view, 'properties-order'):
sorting_options['properties-order'] = get_setting(self.view, 'properties-order')
if get_setting(self.view, 'unspecified-properties-position'):
sorting_options['unspecified-properties-position'] = get_setting(self.view, 'unspecified-properties-position')
try:
return node_bridge(data, BIN_PATH, [json.dumps(sorting_options)])
except Exception as e:
sublime.error_message('PostCSS Sorting\n%s' % e)
def has_selection(self):
for sel in self.view.sel():
start, end = sel
if start != end:
return True
return False
class PostcsssortingPreSaveCommand(sublime_plugin.EventListener):
def on_pre_save(self, view):
if get_setting(view, 'sort-on-save') is True and is_supported_syntax(view):
view.run_command('postcsssorting')