Permalink
Browse files

Support csscomb.js

  • Loading branch information...
1 parent ebecd95 commit b91b6cc896a4b4877139469205a02d6c39a3b83e @tonyganch tonyganch committed Dec 31, 2013
Showing with 375 additions and 0 deletions.
  1. 0 .no-sublime-package
  2. +113 −0 CSScomb.py
  3. +6 −0 CSScomb.sublime-commands
  4. +17 −0 CSScomb.sublime-settings
  5. +6 −0 Context.sublime-menu
  6. +3 −0 Default.sublime-keymap
  7. +90 −0 Main.sublime-menu
  8. +45 −0 README.md
  9. +59 −0 csscomb.js
  10. +3 −0 messages.json
  11. +25 −0 messages/install.txt
  12. +8 −0 package.json
View
No changes.
View
@@ -0,0 +1,113 @@
+# coding: utf-8
+
+import os
+import platform
+import sublime
+import sublime_plugin
+from subprocess import Popen, PIPE
+
+# monkeypatch `Region` to be iterable
+sublime.Region.totuple = lambda self: (self.a, self.b)
+sublime.Region.__iter__ = lambda self: self.totuple().__iter__()
+
+COMB_PATH = os.path.join(sublime.packages_path(), os.path.dirname(os.path.realpath(__file__)), 'csscomb.js')
+
+class CssCombCommand(sublime_plugin.TextCommand):
+ def run(self, edit):
+ syntax = self.get_syntax()
+ if not syntax:
+ return
+
+ config_path = self.get_setting('custom_config_path')
+ if not config_path and self.view.file_name() != None:
+ config_path = self.get_config_path()
+
+ if not self.has_selection():
+ 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:
+ self.view.replace(edit, region, combed)
+
+ def comb(self, css, syntax, config_path):
+ try:
+ p = Popen(['node', COMB_PATH] + [syntax, config_path],
+ stdout=PIPE, stdin=PIPE, stderr=PIPE,
+ env=self.get_env(), shell=self.is_windows())
+ except OSError:
+ raise Exception('Couldn\'t find Node.js. Make sure it\'s in your $PATH by running `node -v` in your command-line.')
+ stdout, stderr = p.communicate(input=css.encode('utf-8'))
+ if stdout:
+ return stdout.decode('utf-8')
+ else:
+ sublime.error_message('CSScomb error:\n%s' % stderr.decode('utf-8'))
+
+ def get_config_path(self, config_path=''):
+ if not config_path:
+ config_path = os.path.dirname(self.view.file_name()) + '/.csscomb.json'
+
+ if os.path.exists(config_path):
+ return config_path
+
+ if os.path.dirname(config_path) == os.path.expanduser('~'):
+ return ''
+
+ config_path = os.path.dirname(os.path.dirname(config_path)) + '/.csscomb.json'
+ return self.get_config_path(config_path)
+
+ def get_env(self):
+ return {
+ 'PATH': self.get_setting('PATH'),
+ 'NODE_PATH': self.get_setting('NODE_PATH')
+ }
+
+ def get_setting(self, key):
+ settings = self.view.settings().get('CSScomb')
+ if settings is None:
+ settings = sublime.load_settings('CSScomb.sublime-settings')
+ return settings.get(key)
+
+ def get_syntax(self):
+ if self.is_css():
+ return 'css'
+ if self.is_scss():
+ return 'scss'
+ if self.is_less():
+ return 'less'
+ if self.is_unsaved_buffer_without_syntax():
+ return 'css'
+ return False
+
+ def has_selection(self):
+ for sel in self.view.sel():
+ start, end = sel
+ if start != end:
+ return True
+ return False
+
+ def is_windows(self):
+ return platform.system() == 'Windows'
+
+ def is_unsaved_buffer_without_syntax(self):
+ return self.view.file_name() == None and self.is_plaintext() == True
+
+ def is_plaintext(self):
+ return self.view.settings().get('syntax') == 'Packages/Text/Plain text.tmLanguage'
+
+ def is_css(self):
+ return self.view.settings().get('syntax') == 'Packages/CSS/CSS.tmLanguage'
+
+ def is_scss(self):
+ return self.view.settings().get('syntax') == 'Packages/SCSS/SCSS.tmLanguage'
+
+ def is_less(self):
+ return self.view.settings().get('syntax') == 'Packages/LESS/LESS.tmLanguage'
+
@@ -0,0 +1,6 @@
+[
+ {
+ "caption": "Run CSScomb",
+ "command": "css_comb"
+ }
+]
@@ -0,0 +1,17 @@
+{
+ // You must set those two variables to make the plugin work correctly.
+
+ // On Unix run from command-line: `echo $PATH`, on Windows: `echo %PATH%`.
+ // Copy the result here:
+ "PATH": "/usr/local/bin",
+
+ // On Unix run from command-line: `echo $NODE_PATH`, on Windows: `echo
+ // %NODE_PATH%`. Copy the result here:
+ "NODE_PATH": "/usr/local/lib/node_modules",
+
+ // If you want to use custom config, put file named `.csscomb.json` to the
+ // project's root or your HOME directory. If for some reason you would like
+ // to set custom path to configuration file, paste it here.
+ // For example, "/Users/csscomb/project/config/csscomb.json"
+ "custom_config_path": ""
+}
@@ -0,0 +1,6 @@
+[
+ {
+ "caption": "Run CSScomb",
+ "command": "css_comb"
+ }
+]
@@ -0,0 +1,3 @@
+[
+ { "keys": ["ctrl+shift+c"], "command": "css_comb" }
+]
View
@@ -0,0 +1,90 @@
+[
+ {
+ "id": "tools",
+ "caption": "Tools",
+ "children":
+ [
+ {
+ "id": "css_comb",
+ "caption": "Run CSScomb",
+ "command": "css_comb"
+ }
+ ]
+ },
+ {
+ "caption": "Preferences",
+ "mnemonic": "n",
+ "id": "preferences",
+ "children":
+ [
+ {
+ "caption": "Package Settings",
+ "mnemonic": "P",
+ "id": "package-settings",
+ "children":
+ [
+ {
+ "caption": "CSScomb",
+ "children":
+ [
+ {
+ "command": "open_file",
+ "args": {
+ "file": "${packages}/CSScomb/README.md"
+ },
+ "caption": "README"
+ },
+ { "caption": "-" },
+ {
+ "command": "open_file",
+ "args": {
+ "file": "${packages}/CSScomb/CSScomb.sublime-settings"
+ },
+ "caption": "Settings – Default"
+ },
+ {
+ "command": "open_file",
+ "args": {
+ "file": "${packages}/User/CSScomb.sublime-settings"
+ },
+ "caption": "Settings – User"
+ },
+ { "caption": "-" },
+ {
+ "command": "open_file",
+ "args": {
+ "file": "${packages}/CSScomb/Default.sublime-keymap"
+ },
+ "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"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+]
View
@@ -0,0 +1,45 @@
+# CSScomb for Sublime Text 2 & 3
+
+## About
+
+CSScomb is a coding style formatter for CSS.
+You can easily write your own configuration to make your stylesheets beautiful
+and consistent.
+
+## The Requirements
+
+You need Node.js to make this plugin work.
+
+Make sure paths are set correctly in settings file:
+
+1. Open default settings: `Preferences > Package Settings > CSScomb > Sort Order - Default`
+2. Copy whole file content.
+3. Open user-defined settings: `Preferences > Package Settings > CSScomb > Sort Order - User`
+4. Paste in this file.
+5. Follow instructions in file comments.
+
+## Plugin usage
+
+Select code and press <kbd>ctrl</kbd>+<kbd>shift</kbd>+<kbd>c</kbd>
+
+Tip: Combine expand selection by indentation <kbd>ctrl</kbd>+<kbd>shift</kbd>+<kbd>j</kbd> with <kbd>ctrl</kbd>+<kbd>shift</kbd>+<kbd>c</kbd> to quickly
+select all rules for current css selector and sort them with csscomb.
+
+## Custom configuration
+
+To use your own configuration file do one of the following:
+
+1. Put `.csscomb.json` file in the project root or your HOME directory.
+2. Set path to config file in user settings:
+ `{ "custom_config_path": "~/Sites/csscomb.json" }`
+3. Set path in Sublime project settings:
+ `"CSScomb": { "custom_config_path": "~/Sites/.csscomb.json" },`
+
+## Issues & bugs
+
+[Plugin tracker](https://github.com/csscomb/csscomb-for-sublime)
+[CSScomb tracker](https://github.com/csscomb/csscomb.js/issues)
+
+## Authors
+
+Sublime plugin: [i-akhmadullin](https://github.com/i-akhmadullin)
View
@@ -0,0 +1,59 @@
+var str = '';
+
+process.stdin.resume();
+process.stdin.setEncoding('utf8');
+
+process.stdin.on('data', function (data) {
+ str += data;
+});
+
+process.stdin.on('end', function () {
+ var npm;
+ try {
+ npm = require('npm');
+ npm.load({ prefix: __dirname, loglevel: 'error' }, function(err, npm) {
+ try {
+ checkAndProcess();
+ } catch (e) {
+ installAndProcess();
+ }
+ });
+ } catch (e) {
+ process.stderr.write('Could not load npm. Please make sure your paths are set correctly in settings.');
+ }
+
+ // Check if installed version is outdated:
+ function checkAndProcess() {
+ var installedVersion = require('./node_modules/csscomb/package.json').version,
+ requiredVersion = require('./package.json').dependencies.csscomb;
+ if (installedVersion !== requiredVersion) installAndProcess();
+ else processString();
+ }
+
+ function installAndProcess() {
+ // Disable install logs:
+ console.log = function() {};
+ // Install csscomb package:
+ npm.commands.install(['csscomb'], function() {
+ processString();
+ });
+ }
+
+ function processString() {
+ var Comb = require('./node_modules/csscomb/lib/csscomb'),
+ comb = new Comb(),
+ syntax = process.argv[2],
+ configPath = process.argv[3],
+ config, combed;
+
+ if (configPath) {
+ config = require(configPath);
+ } else {
+ config = comb.getConfig('csscomb');
+ }
+
+ combed = comb.configure(config).processString(str, syntax);
+ process.stdout.write(combed);
+ }
+});
+
View
@@ -0,0 +1,3 @@
+{
+ "install": "messages/install.txt"
+}
View
@@ -0,0 +1,25 @@
+CSScomb
+=============
+
+CSScomb is a coding style formatter for CSS.
+You can easily write your own configuration to make your stylesheets beautiful
+and consistent.
+
+WARNING: Make sure paths are set correctly in settings file:
+
+1. Open default settings: `Preferences > Package Settings > CSScomb > Sort Order - Default`
+2. Copy whole file content.
+3. Open user-defined settings: `Preferences > Package Settings > CSScomb > Sort Order - User`
+4. Paste in this file.
+5. Follow instructions in file comments.
+
+There are a number of ways to configure CSScomb:
+
+1. Put `.csscomb.json` file in the project root or your HOME directory.
+2. Set path to config file in user settings:
+ `{ "custom_config_path": "~/Sites/csscomb.json" }`
+3. Set path in Sublime project settings:
+ `"CSScomb": { "custom_config_path": "~/Sites/.csscomb.json" },`
+
+For more information read docs: https://github.com/csscomb/csscomb.js
+
View
@@ -0,0 +1,8 @@
+{
+ "name": "csscomb-for-sublime",
+ "version": "0.0.1",
+ "dependencies": {
+ "csscomb": "2.0.1"
+ },
+ "license": "MIT"
+}

0 comments on commit b91b6cc

Please sign in to comment.