From 7dbd61cab5708f5b726904392bbc2f0cf53ad3f4 Mon Sep 17 00:00:00 2001 From: Bryan Hoekstra Date: Thu, 24 Dec 2015 08:17:51 +1300 Subject: [PATCH] Add comment strip --- README.md | 23 ++++++++++++++++++++--- extension.js | 46 +++++++++++++++++++++++++++++++++------------- package.json | 7 +++++-- 3 files changed, 58 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index eb5652d..0fe2810 100644 --- a/README.md +++ b/README.md @@ -2,17 +2,34 @@ [![Build Status](https://api.travis-ci.org/HookyQR/VSCodeBeautify.svg?branch=master)](https://travis-ci.org/HookyQR/VSCodeBeautify) -VS Code uses js-beautify internally, bit it lacks the ability to modify the style you wish to use. This extension enables running [js-beautify](http://jsbeautifier.org/) in VS Code, _AND_ honouring any `.jsbeautifyrc` file in the open file's path tree to load *your* code styling. Run with **⌘⇧P** `Beautify`. +VS Code uses js-beautify internally, bit it lacks the ability to modify the style you wish to use. This extension enables running [js-beautify](http://jsbeautifier.org/) in VS Code, _AND_ honouring any `.jsbeautifyrc` file in the open file's path tree to load *your* code styling. Run with **F1** `Beautify`. -This package now includes hints when editing your `.jsbeautifyrc`. Only the first file found will be used. If the format is bad, the default js-beautify settings will be used. +This package includes hints when editing your `.jsbeautifyrc`. Only the first file found will be used. If the format is bad, the default js-beautify settings will be used, but a warning will be issued to let you know. Comments in your settings file are acceptable (they're removed before the file is parsed). The embedded schema for `.jsbeautifyrc` has also been published at [JSON Schema Store](http://schemastore.org) which allows users of VSCode 0.10.3 to add it manually to their user or workspace settings: -Also runs http and css beautify from the same package, as determined by the file extension. If the file is unsaved, or the type is undetermined, you'll be prompted for which beautifier to use. +```json +"json.schemas": [ + { + "fileMatch": ["**/.jsbeautifyrc"], + "url": "http://json.schemastore.org/jsbeautifyrc" + } +] +``` + +Also runs http and css beautify from the same package, as determined by the file extension. The schema indicates which beautifier each of the settings pertains to. + +If the file is unsaved, or the type is undetermined, you'll be prompted for which beautifier to use. Extra (permanent) file extension may be added under user or workspace settings. + Embedded version of js-beautify is v1.5.10. ## Changes: +### 0.0.5: 24 Dec 2015 +* Schema published at http://json.schemastore.org/jsbeautifyrc. +* Added README details for schema install for users of VSCode < v0.10.5 +* Added comments remover before JSON parse. Fixes [Issue #2: .jsbeautifyrc file not being used](https://github.com/HookyQR/VSCodeBeautify/issues/2) + ### 0.0.4: 19 Dec 2015 * Changed default (unknown) processing to ask you what you want to use. * Fixed [Issue #1: No handler found for the command: 'HookyQR.beautify'](https://github.com/HookyQR/VSCodeBeautify/issues/1) diff --git a/extension.js b/extension.js index 8081d57..3d4ae51 100644 --- a/extension.js +++ b/extension.js @@ -13,9 +13,29 @@ function findRecursive(dir, fileName) { } return result; } + +var dropWithRegEx = function(text, re) { + if (!re.global) //I'm not doing that for ever + return text; + var oText = ""; + var match = re.exec(text); + var lastEnd = 0; + while (match) { + if (lastEnd < match.index) + oText += text.slice(lastEnd, match.index); + lastEnd = match.index + match[0].length; + match = re.exec(text); + } + if (lastEnd < text.length) oText += text.slice(lastEnd, text.length); + return oText; +} +var dropMultiLineComments = inText => dropWithRegEx(inText, /\/\*.*\*\//g); +var dropSingleLineComments = inText => dropWithRegEx(inText, /\/\/.*(?:[\r\n]|$)/g); +var dropComments = inText => dropSingleLineComments(dropMultiLineComments(inText)); + //register on activation function activate(context) { - + var doBeautify = function(active, doc, opts) { var original = doc.getText(); var type = doc.isUntitled ? "" : doc.fileName.split('.') @@ -52,13 +72,10 @@ function activate(context) { return; } } - if (cfg.HTMLfiles.indexOf(type) + 1) { - result = beautify.html(original, opts); - } else if (cfg.CSSfiles.indexOf(type) + 1) { - result = beautify.css(original, opts); - } else if (cfg.JSfiles.indexOf(type) + 1) { - result = beautify.js(original, opts); - } else { + if (cfg.HTMLfiles.indexOf(type) + 1) result = beautify.html(original, opts); + else if (cfg.CSSfiles.indexOf(type) + 1) result = beautify.css(original, opts); + else if (cfg.JSfiles.indexOf(type) + 1) result = beautify.js(original, opts); + else { //Ask what they want to do: vscode.window.showQuickPick([{ label: "JS", @@ -73,8 +90,7 @@ function activate(context) { }) .then(function(choice) { if (!choice || !choice.label) return; - result=beautify[choice.label.toLowerCase()](original, opts); - + result = beautify[choice.label.toLowerCase()](original, opts); active.edit(editor => editor.replace(range, result)); }); return; @@ -82,7 +98,8 @@ function activate(context) { //and make the change: active.edit(editor => editor.replace(range, result)); }; - + //it's ok to build and pass the re from outside of here, we always run + //to completion. var disposable = vscode.commands.registerCommand('HookyQR.beautify', function() { var active = vscode.window.activeTextEditor; if (!active) return; @@ -97,11 +114,14 @@ function activate(context) { //walk to find a .jsbeautifyrc if (beautFile) fs.readFile(beautFile, function(ee, d) { - if (ee && !d) d = "{}"; + if (!d) d = "{}"; var opts = {}; try { - opts = JSON.parse(d.toString()); + var unCommented = dropComments(d.toString()); + opts = JSON.parse(unCommented); } catch (e) { + //put a warning in here + vscode.window.showWarningMessage("Found a .jsbeautifyrc file, but it didn't parse correctly."); opts = {}; //just use the default opts } doBeautify(active, doc, opts); diff --git a/package.json b/package.json index 336071e..35708a5 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.4", + "version": "0.0.5", "publisher": "HookyQR", "engines": { "vscode": "^0.10.1" @@ -60,5 +60,8 @@ "repository": { "type": "git", "url": "https://github.com/HookyQR/VSCodeBeautify" - } + }, + "bugs":{ + "url":"https://github.com/HookyQR/VSCodeBeautify/issues" + } } \ No newline at end of file