Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
miripiruni committed Apr 24, 2012
0 parents commit 3c77439
Show file tree
Hide file tree
Showing 14 changed files with 1,687 additions and 0 deletions.
92 changes: 92 additions & 0 deletions CSScomb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import sublime, sublime_plugin
from csscomb import BaseSort

class BaseSorter(sublime_plugin.TextCommand):
"""Base Sorter"""

def __init__(self, view):
self.view = view
# self.settings = sublime.load_settings("Minifier.sublime-settings")

def run(self, edit):

selections = self.get_selections()

threads = []
for sel in selections:
selbody = self.view.substr(sel)

thread = BaseSort(sel,selbody)

threads.append(thread)
thread.start()

selections.clear()
self.handle_threads(edit, threads, selections, offset=0, i=0)

def get_selections(self):
selections = self.view.sel()

# check if the user has any actual selections
has_selections = False
for sel in selections:
if sel.empty() == False:
has_selections = True

# if not, add the entire file as a selection
if not has_selections:
full_region = sublime.Region(0, self.view.size())
selections.add(full_region)

return selections

def handle_threads(self, edit, threads, selections, offset = 0, i = 0):

next_threads = []
for thread in threads:
if thread.is_alive():
next_threads.append(thread)
continue
if thread.result == False:
continue
self.handle_result(edit, thread, selections, offset)
threads = next_threads

if len(threads):
sublime.set_timeout(lambda: self.handle_threads(edit, threads, selections, offset, i), 100)
return

self.view.end_edit(edit)
sublime.status_message('Successfully sorted')


def handle_result(self, edit, thread, selections, offset):
sel = thread.sel
original = thread.original
# print original
result = thread.result
# print result

if thread.error is True:
sublime.error_message(result)
return
elif result is None:
sublime.error_message("There was an error sorting CSS.")
return

return thread


class CssSorter(BaseSorter):

def handle_result(self, edit, thread, selections, offset):
result = super(CssSorter, self).handle_result(edit, thread, selections, offset)

editgroup = self.view.begin_edit('csscomb')

sel = thread.sel
result = thread.result
# if offset:
# sel = sublime.Region(thread.sel.begin() + offset, thread.sel.end() + offset)

self.view.replace(edit, sel, result)
6 changes: 6 additions & 0 deletions CSScomb.sublime-commands
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"caption": "Sort via CSScomb",
"command": "css_sorter"
}
]
6 changes: 6 additions & 0 deletions Context.sublime-menu
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"caption": "Sort via CSScomb",
"command": "css_sorter"
}
]
3 changes: 3 additions & 0 deletions Default (Linux).sublime-keymap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
{ "keys": ["ctrl+shift+c"], "command": "css_sorter" }
]
3 changes: 3 additions & 0 deletions Default (OSX).sublime-keymap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
{ "keys": ["ctrl+shift+c"], "command": "css_sorter" }
]
3 changes: 3 additions & 0 deletions Default (Windows).sublime-keymap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
{ "keys": ["ctrl+shift+c"], "command": "css_sorter" }
]
85 changes: 85 additions & 0 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
[
{
"id": "tools",
"caption": "Tools",
"children":
[
{
"id": "css_comb",
"caption": "Sort via CSScomb",
"command": "css_sorter"
}
]
},
{
"caption": "Preferences",
"mnemonic": "n",
"id": "preferences",
"children":
[
{
"caption": "Package Settings",
"mnemonic": "P",
"id": "package-settings",
"children":
[
{
"caption": "CSScomb",
"children":
[
{ "caption": "-" },
{
"command": "open_file",
"args": {
"file": "${packages}/CSScomb/Default (OSX).sublime-keymap",
"platform": "OSX"
},
"caption": "Key Bindings – Default"
},
{
"command": "open_file",
"args": {
"file": "${packages}/CSScomb/Default (Linux).sublime-keymap",
"platform": "Linux"
},
"caption": "Key Bindings – Default"
},
{
"command": "open_file",
"args": {
"file": "${packages}/CSScomb/Default (Windows).sublime-keymap",
"platform": "Windows"
},
"caption": "Key Bindings – Default"
},
{
"command": "open_file",
"args": {
"file": "${packages}/User/Default (OSX).sublime-keymap",
"platform": "OSX"
},
"caption": "Key Bindings – User"
},
{
"command": "open_file",
"args": {
"file": "${packages}/User/Default (Linux).sublime-keymap",
"platform": "Linux"
},
"caption": "Key Bindings – User"
},
{
"command": "open_file",
"args": {
"file": "${packages}/User/Default (Windows).sublime-keymap",
"platform": "Windows"
},
"caption": "Key Bindings – User"
}
]
}
]
}
]
}
]
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# CSScomb for Sublime Text 2

## About

Tool for sorting CSS properties in specific order.

* Sorting CSS properties. The order of properties in the help of professionals
* Setting the order of CSS properties. Use the order to which you are accustomed to
* Parsing CSS in the tags <style>, style="..." attribute. CSScomb find a CSS-code to other languages ​​and will sort it
* Formatting style sheets does not change. Work as a singleline and multiline CSS
* Separation of CSS properties for the group. Separate groups of empty string if you want
* Full support CSS2/CSS2.1/CSS3/CSS4 ;) CSScomb ready for the advanced CSS-code

The algorithm of CSScomb simulates web-technologists actions upon working with
CSS-code to the limit. Usually to re-order code you move lines over each other
considering comments in the code, multilines records of property values, hacks
and everything that could be found in the real file. CSScomb reproduces these
actions for you. This means that the parser "thinks" as a person editing the
text, not as a blind robot parsing CSS.

For more info, online demo and tests see [http://csscomb.com/](csscomb.com)

## Issues & bugs

[CSScomb tracker](https://github.com/miripiruni/CSSComb/issues)


## Authors

CSScomb core: [miripiruni](mailto:mail@csscomb.ru)
Sublime plugin: [i-akhmadullin](https://github.com/i-akhmadullin)
1 change: 1 addition & 0 deletions csscomb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from basesort import BaseSort
36 changes: 36 additions & 0 deletions csscomb/basesort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sublime
import sublime_plugin
import threading
import sys,subprocess
from os import path

__file__ = path.normpath(path.abspath(__file__))
__path__ = path.dirname(__file__)
libs_path = path.join(__path__, 'libs')
csscomb_path = path.join(libs_path,"call_string.php")

class BaseSort(threading.Thread):

def __init__(self, sel, original):
self.sel = sel
self.original = original
self.result = None
self.error = None
threading.Thread.__init__(self)

def exec_request(self):
myprocess = subprocess.Popen(['php',csscomb_path,self.original], shell=False, stdout=subprocess.PIPE)

This comment has been minimized.

Copy link
@i-akhmadullin

i-akhmadullin Apr 25, 2012

Member

В этом методе можно сделать так, чтобы под windows на время сортировки не появлялось всплывающее окно. Примерно как здесь. Пользуюсь на win7, окошко не всплывает, проблем пока не было. Стоит ли делать ради этой микрооптимизации пуллреквест?

This comment has been minimized.

Copy link
@miripiruni

miripiruni via email Apr 25, 2012

Author Contributor
(sout,serr) = myprocess.communicate()
myprocess.wait()

if len(sout) > 0:
return sout
else:
return None

def run(self):
try:
self.result = self.exec_request()
except (OSError) as (e):
self.error = True
self.result = 'Sorter Error: attempt to sort non-existent file'
7 changes: 7 additions & 0 deletions csscomb/libs/call.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

/* Обертка для вызова CSScomb */
$csscomb = new csscomb();
$sort_type = is_file($argv[2]) ? file_get_contents($argv[2]) : $argv[2];
$input = $csscomb->csscomb(file_get_contents($argv[1]), false, $sort_type);
file_put_contents($argv[1], $input);
7 changes: 7 additions & 0 deletions csscomb/libs/call_string.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
require_once 'csscomb.php';
/* Обертка для вызова CSScomb */
$csscomb = new csscomb();
/*$sort_type = is_file($argv[2]) ? file_get_contents($argv[2]) : $argv[2];*/
$input = $csscomb->csscomb($argv[1], false, null);
echo $input;
Loading

0 comments on commit 3c77439

Please sign in to comment.