From f040b1feeef08236ebd1f0183f720c349ba4ada1 Mon Sep 17 00:00:00 2001 From: Bryan Hoekstra Date: Sat, 19 Dec 2015 20:14:01 +1300 Subject: [PATCH] Add .jsbeautifyrc hints --- .gitignore | 3 +- README.md | 5 ++ extension.js | 54 +++++++++--- jsconfig.json | 9 -- package.json | 19 ++++- schema/beautifyrc.json | 162 ++++++++++++++++++++++++++++++++++++ vsc-extension-quickstart.md | 33 -------- 7 files changed, 226 insertions(+), 59 deletions(-) delete mode 100644 jsconfig.json create mode 100644 schema/beautifyrc.json delete mode 100644 vsc-extension-quickstart.md diff --git a/.gitignore b/.gitignore index f19cfd2..28a2e04 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ node_modules *.vsix .vscodeignore typings -.vscode \ No newline at end of file +.vscode +.jshintrc \ No newline at end of file diff --git a/README.md b/README.md index 2e9c5cf..24b0251 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/extension.js b/extension.js index 37eb7ea..a55526e 100644 --- a/extension.js +++ b/extension.js @@ -1,3 +1,4 @@ +"use strict"; var vscode = require('vscode'); var beautify = require('js-beautify'); var path = require('path'); @@ -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; @@ -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 { @@ -62,4 +90,4 @@ function activate(context) { }); context.subscriptions.push(disposable); } -exports.activate = activate; \ No newline at end of file +exports.activate = activate; diff --git a/jsconfig.json b/jsconfig.json deleted file mode 100644 index 026f315..0000000 --- a/jsconfig.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "compilerOptions": { - "target": "ES6", - "noLib": true - }, - "exclude": [ - "node_modules" - ] -} \ No newline at end of file diff --git a/package.json b/package.json index ad6f46b..df444db 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" @@ -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": { diff --git a/schema/beautifyrc.json b/schema/beautifyrc.json new file mode 100644 index 0000000..9d6dd57 --- /dev/null +++ b/schema/beautifyrc.json @@ -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 and 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 + } + } +} \ No newline at end of file diff --git a/vsc-extension-quickstart.md b/vsc-extension-quickstart.md deleted file mode 100644 index 40679aa..0000000 --- a/vsc-extension-quickstart.md +++ /dev/null @@ -1,33 +0,0 @@ -# Welcome to your first VS Code Extension - -## What's in the folder -* This folder contains all of the files necessary for your extension -* `package.json` - this is the manifest file in which you declare your extension and command. -The sample plugin registers a command and defines its title and command name. With this information -VS Code can show the command in the command palette. It doesn’t yet need to load the plugin. -* `extension.js` - this is the main file where you will provide the implementation of your command. -The file exports one function, `activate`, which is called the very first time your extension is -activated (in this case by executing the command). Inside the `activate` function we call `registerCommand`. -We pass the function containing the implementation of the command as the second parameter to -`registerCommand`. - -## Get up and running straight away -* press `F5` to open a new window with your extension loaded -* run your command from the command palette by pressing (`Ctrl+Shift+P` or `Cmd+Shift+P` on Mac) and typing `Hello World` -* set breakpoints in your code inside extension.ts to debug your extension -* find output from your extension in the debug console - -## Make changes -* you can relaunch the extension from the debug toolbar after changing code in `extension.js` -* you can also reload (`Ctrl+R` or `Cmd+R` on Mac) the VS Code window with your extension to load your changes - -## Explore the API -* you can open the full set of our API when you open the file `node_modules/vscode/vscode.d.ts` - -## Run tests -* open the debug viewlet (`Ctrl+Shift+D` or `Cmd+Shift+D` on Mac) and from the launch configuration dropdown pick `Launch Tests` -* press `F5` to run the tests in a new window with your extension loaded -* see the output of the test result in the debug console -* make changes to `test/extension.test.js` or create new test files inside the `test` folder - * by convention, the test runner will only consider files matching the name pattern `**.test.js` - * you can create folders inside the `test` folder to structure your tests any way you want \ No newline at end of file