Skip to content

Commit

Permalink
Add .jsbeautifyrc hints
Browse files Browse the repository at this point in the history
  • Loading branch information
HookyQR committed Dec 19, 2015
1 parent d296605 commit f040b1f
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 59 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
*.vsix
.vscodeignore
typings
.vscode
.vscode
.jshintrc
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Extra file extenstion may be added under user or workspace settings.
Embeded version of js-beautify is v1.5.10.

## Changes:
### 0.0.3: 19 Dec 2015
* _Tries_ to mark any elements in `json.schema` settings as JSON, and thus beautify as JS.
* Added schema for `.jsbeautifyrc` file.
* Added language type so `.jsbeautifyrc` is recognised as JSON.

### 0.0.2: 17 Dec 2015
* Add options for other file extensions.

54 changes: 41 additions & 13 deletions extension.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use strict";
var vscode = require('vscode');
var beautify = require('js-beautify');
var path = require('path');
Expand All @@ -15,27 +16,54 @@ function findRecursive(dir, fileName) {
//register on activation
function activate(context) {

var doBeautify = function (active, doc, opts) {
var doBeautify = function(active, doc, opts) {
var better = doc.getText();
var type = doc.isUntitled ? "js" : doc.fileName.split('.').pop().toLowerCase();
var type = doc.isUntitled ? "js" : doc.fileName.split('.')
.pop()
.toLowerCase();
var cfg = vscode.workspace.getConfiguration('beautify');
if ( cfg.HTMLfiles.indexOf(type)+1){
better = beautify.html(better, opts);
//if a type is set on the window, use that
console.log(vscode.window.activeTextEditor);
//check if the file is in the users json schema set
var jsSchema = vscode.workspace.getConfiguration('json')
.schemas;
var range;
if (jsSchema) {
var matcher = [];
var extMatch = n => ({
pattern: n.startsWith("**/") ? n : ("**/" + n)
});
jsSchema.forEach(schema => {
if (typeof schema.fileMatch === 'string') {
matcher.push(extMatch(schema.fileMatch));
} else {
var t = schema.fileMatch.map(extMatch);
matcher = matcher.concat(t);
}
});
if (vscode.languages.match(matcher, doc)) {
//beautify as javascript
better = beautify.js(better, opts);
//get the whole file:
range = new vscode.Range(new vscode.Position(0, 0), doc.positionAt(Infinity));
//and make the change:
active.edit(editor => editor.replace(range, better));
}
}
else if (cfg.CSSfiles.indexOf(type)+1){
if (cfg.HTMLfiles.indexOf(type) + 1) {
better = beautify.html(better, opts);
} else if (cfg.CSSfiles.indexOf(type) + 1) {
better = beautify.css(better, opts);
}
else if (cfg.JSfiles.indexOf(type)+1){
} else if (cfg.JSfiles.indexOf(type) + 1) {
better = beautify.js(better, opts);
}
else return;
} else return;
//get the whole file:
var range = new vscode.Range(new vscode.Position(0, 0), doc.positionAt(Infinity));
range = new vscode.Range(new vscode.Position(0, 0), doc.positionAt(Infinity));
//and make the change:
active.edit(editor => editor.replace(range, better));
};

var disposable = vscode.commands.registerCommand('HookyQR.beautify', function () {
var disposable = vscode.commands.registerCommand('HookyQR.beautify', function() {
var active = vscode.window.activeTextEditor;
if (!active) return;
var doc = active.document;
Expand All @@ -48,7 +76,7 @@ function activate(context) {
if (base) beautFile = findRecursive(base, ".jsbeautifyrc");

//walk to find a .jsbeautifyrc
if (beautFile) fs.readFile(beautFile, function (ee, d) {
if (beautFile) fs.readFile(beautFile, function(ee, d) {
if (ee && !d) d = "{}";
var opts = {};
try {
Expand All @@ -62,4 +90,4 @@ function activate(context) {
});
context.subscriptions.push(disposable);
}
exports.activate = activate;
exports.activate = activate;
9 changes: 0 additions & 9 deletions jsconfig.json

This file was deleted.

19 changes: 16 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "beautify",
"displayName": "beautify",
"description": "Beautify code in place for VS Code",
"version": "0.0.2",
"version": "0.0.3",
"publisher": "HookyQR",
"engines": {
"vscode": "^0.10.1"
Expand All @@ -13,8 +13,21 @@
"activationEvents": [
"onCommand:HookyQR.beautify"
],
"license": "MIT",
"main": "./extension",
"extensionDependencies": [
"vscode.json"
],
"contributes": {
"languages": [{
"id": "json",
"aliases": ["JSON"],
"extensions": [".jsbeautifyrc"]
}],
"jsonValidation": [{
"fileMatch": ".jsbeautifyrc",
"url": "./schema/beautifyrc.json"
}],
"commands": [{
"command": "HookyQR.beautify",
"title": "Beautify"
Expand All @@ -25,12 +38,12 @@
"properties": {
"beautify.JSfiles": {
"type": "array",
"default": ["js","json","jsbeautifyrc","jshintrc"],
"default": ["js", "json", "jsbeautifyrc", "jshintrc"],
"description": "File extensions that can be minified as javascript or JSON."
},
"beautify.HTMLfiles": {
"type": "array",
"default": ["htm","html"],
"default": ["htm", "html"],
"description": "File extensions that can be minified as HTML."
},
"beautify.CSSfiles": {
Expand Down
162 changes: 162 additions & 0 deletions schema/beautifyrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
{
"title": "JSON schema for beautifyrc",
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"indent_size": {
"description": "Indent size. [JS,CSS,HTML]",
"type": "integer",
"default": 4
},
"indent_char": {
"description": "Indentation character. [JS,CSS,HTML]",
"type": "string",
"default": " ",
"maxLength": 1
},
"indent_with_tabs": {
"description": "Indent with tabs, overrides 'indent_size' and 'indent_char' [JS,CSS,HTML]",
"type": "boolean",
"default": false
},
"eol": {
"description": "Character(s) to use as line terminators. [JS,CSS,HTML]",
"type": "string",
"default": "\\n"
},
"preserve_newlines": {
"description": "Preserve line-breaks. [JS,HTML]",
"type": "boolean",
"default": true
},
"max_preserve_newlines": {
"description": "Number of line-breaks to be preserved in one chunk. [JS,HTML]",
"type": "integer",
"default": 10
},
"indent_level": {
"description": "Initial indentation level. [JS]",
"type": "integer",
"default": 0
},
"space_in_paren": {
"description": "Add padding spaces within parentheses, ie. f( a, b ). [JS]",
"type": "boolean",
"default": false
},
"jslint_happy": {
"description": "Enable jslint-stricter mode. (Forces 'space_after_anon_function') [JS]",
"type": "boolean",
"default": false
},
"space_after_anon_function": {
"description": "Add a space before an anonymous function's parens, ie. function (). [JS]",
"type": "boolean",
"default": false
},
"brace_style": {
"description": "[collapse|expand|end-expand|none] [JS,HTML]",
"type": "string",
"default": "collapse",
"enum": [
"collapse", "expand", "end-expand", "none"
]
},
"break_chained_methods": {
"description": "Break chained method calls across subsequent lines. [JS]",
"type": "boolean",
"default": false
},
"keep_array_indentation": {
"description": "Preserve array indentation. [JS]",
"type": "boolean",
"default": false
},
"keep_function_indentation": {
"description": "Preserve function indentation. [JS]",
"type": "boolean",
"default": false
},
"space_before_conditional": {
"description": "Ensure a space before conditional statement. [JS]",
"type": "boolean",
"default": true
},
"unescape_strings": {
"description": "Decode printable characters encoded in xNN notation. [JS]",
"type": "boolean",
"default": false
},
"wrap_line_length": {
"description": "Wrap lines at next opportunity after N characters. [JS,HTML]",
"type": "integer",
"default": 0
},
"wrap_attributes": {
"description": "Wrap attributes to new lines. [HTML]",
"type": "string",
"default": "auto",
"enum": [
"auto", "force"
]
},
"wrap_attributes_indent_size": {
"description": "Indent wrapped attributes to after N characters. Defaults to 'indent_size'. [HTML]",
"type": "number"
},
"end_with_newline": {
"description": "Ensure newline at end of file. [JS,HTML]",
"type": "boolean",
"default": false
},
"indent_inner_html": {
"description": "Indent <head> and <body> sections. [HTML]",
"type": "boolean",
"default": false
},
"indent_scripts": {
"description": "[keep|separate|normal] [HTML]",
"type": "string",
"default": "normal",
"enum": [
"keep", "separate", "normal"
]
},
"unformatted": {
"description": "List of tags that should not be reformatted. [HTML]",
"type": "array",
"items": {
"type": "string"
},
"default": ["inline"]
},
"extra_liners":{
"description": "List of tags that should have an extra newline before them. [HTML]",
"type": "array",
"items": {
"type": "string"
},
"default": ["head","body","/html"]
},
"comma_first":{
"description": "Put commas at the beginning of new line instead of end. [JS]",
"type":"boolean",
"default": false
},
"e4x":{
"description": "Pass E4X xml literals through untouched. [JS]",
"type":"boolean",
"default":false
},
"newline_between_rules":{
"description": "Add a newline between CSS rules. [CSS]",
"type":"boolean",
"default": false
},
"selector_separator_newline":{
"description": "Add a newline between multiple selectors. [CSS]",
"type":"boolean",
"default": true
}
}
}
33 changes: 0 additions & 33 deletions vsc-extension-quickstart.md

This file was deleted.

0 comments on commit f040b1f

Please sign in to comment.